go-app icon indicating copy to clipboard operation
go-app copied to clipboard

How to Communicate between Frontend and Backend

Open zx42 opened this issue 3 years ago • 13 comments

Sorry for the rookie questions:

  1. 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.
  2. Is there a simple convenient way to stream data from the back end to the front end?
    • I assume a web socket might work.

zx42 avatar Sep 14 '21 15:09 zx42

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)
	}
}

maxence-charriere avatar Sep 14 '21 15:09 maxence-charriere

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

maxence-charriere avatar Sep 14 '21 15:09 maxence-charriere

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

pizzalord22 avatar Sep 22 '21 10:09 pizzalord22

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 ?

maxence-charriere avatar Sep 22 '21 10:09 maxence-charriere

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))
    }

pizzalord22 avatar Sep 22 '21 13:09 pizzalord22

What package are you using for websocket? Also look like the address in dial is not correct.

maxence-charriere avatar Sep 22 '21 18:09 maxence-charriere

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

pizzalord22 avatar Sep 23 '21 06:09 pizzalord22

You might want to try nhooyr.io/websocket

fbaube avatar Oct 01 '21 16:10 fbaube

@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.

AdamRGrey avatar Nov 16 '21 16:11 AdamRGrey

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.

image

oderwat avatar Nov 16 '21 16:11 oderwat

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.

maxence-charriere avatar Nov 17 '21 02:11 maxence-charriere

I have had good results using github.com/dennwc/dom/net/ws in webassenbly.

fbaube avatar Nov 17 '21 16:11 fbaube

@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?

oderwat avatar Nov 17 '21 18:11 oderwat