deepakssn deepakssn - 23 days ago 4
reST (reStructuredText) Question

Golang: Broadcast messages to websocket when a particular POST JSON is received

I have an app that is connected to my goserver using websocket . I used this code to create a websocket connection - https://github.com/gorilla/websocket/tree/master/examples/chat

http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})


When I receive a POST call I need to broadcast a message - "Order Received: " productNum to all the devices. productNum is received in the post request.

http.HandleFunc("/post",BroadcastMessage())


I want to know what I need to write in BroadcastMessage() to do this. Please help.

Answer

In BroadcastMessage, send a []byte to the hub's broadcast channel:

 hub.broadcast <- message
Comments