go-app
go-app copied to clipboard
How to Communicate between Frontend and Backend
Sorry for the rookie questions:
- Is there a simple convenient way to transfer data from the front end (go-app code in browser) to the back end?
- So far I have used REST requests to the back end http server but I was forced to run a second http service on another port because I could not add more http handlers for different URL paths on the main http service.
- Is there a simple convenient way to stream data from the back end to the front end?
- I assume a web socket might work.
Is there a simple convenient way to transfer data from the front end (go-app code in browser) to the back end? So far I have used rest requests to the back end http server but I was forced to run a second http service on another port
This is the correct way, just have to set up another HTTP handler:
func main() {
app.Route("/", &hello{})
app.Route("/hello", &hello{})
app.RunWhenOnBrowser()
http.Handle("/", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
})
http.Handle("/api/", YOUR_HTTP_HANDLER) // <--------- standard go http
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Fatal(err)
}
}
Is there a simple convenient way to stream data from the back end to the front end? I assume a web socket might work.
Web socket in fact works. It pretty simple to implement with go => https://pkg.go.dev/golang.org/x/net/websocket
I tried using websocket for communication but i keep getting the following error "Connection refused" when using the library other websocket clients work just fine, i also tried using the gorilla websocket library but using a different router then the default http library causes a lot of errors where files can't be found
Do you have some code to see what is happening? or log?
if you got the connection refused with the WebSocket lib mentioned above, did you make sure that the WebSocket-Origin
is set ?
server:
return func(w http.ResponseWriter, r *http.Request) {
log.Println("incoming request from:", r.RemoteAddr)
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Upgrade error: " + err.Error()))
return
}
connection.WriteMessage(websocket.TextMessage, []byte("test message"))
connection.WriteMessage(websocket.CloseMessage, []byte("test message"))
connection.Close()
}
client:
var d websocket.Dialer
d = websocket.Dialer{HandshakeTimeout: 30 * time.Second}
connection, _, err := d.Dial("ws://server/ws", nil)
if err != nil {
log.Println("ws connection error: ", err)
} else {
msgType, msg, err := connection.ReadMessage()
if err != nil {
log.Println("read message error: ", err)
}
if msgType == websocket.CloseMessage {
connection.Close()
}
log.Println("test value:", string(msg))
}
What package are you using for websocket? Also look like the address in dial is not correct.
i am using github.com/gorilla/websocket, i have remove the real ip, but i have tested it with an other client written in go and that one could connect and send / receive messages
You might want to try nhooyr.io/websocket
@pizzalord22 I don't know if it's the issue you're facing, but gorilla/websocket doesn't support wasm. nhooyr.io/websocket does, though.
I am using nhooyr.io/websocket
too. With good success. But I think I want to switch to a package that has less dependencies as it adds quite a lot.
Did you guys tried the go one => https://pkg.go.dev/golang.org/x/net/websocket
i found it pretty well built and extensible and work well with standard http.
I have had good results using github.com/dennwc/dom/net/ws in webassenbly.
@maxence-charriere I actually wanted to try it next. It tells users to try the other packages but maybe it is just complete enough for what we need.
@fbaube This seems not to be maintained and the latest changes were 3 years ago. It also means that you need to include all the other "dom/js" related stuff which may be of use but to me, they are not in combination with go-app. I actually wonder why people are using any other js frontend (vite came up) or "manual js dom" together with go-app. To me, this looks like mixing jQuery with VueJS and native Javascript. Can work but why?