You can use Comet for realtime peer-to-peer message push or broadcasting.
The Comet API provide two different protocols for receiver and sender. Message receiver using websocket protocol, as well as Messsage sender using HTTP protocol.
The design is very simple and light-weighted. When a web browser client connected, the Comet server launch a goroutine simultaneously and monitor it. Monitor corresponding to the client send a heartbeat message per 5 second in order to check the connection. Once the connection break, the Comet will remove this client. So, in this way, the frontend will never pay attention to the Comet server and never should send heartbeat to server. Control is in server side completely.
$ go get github.com/olzhy/comet
$ go run main.go
You can use JavaScript WebSocket API to establish a connection to Comet server. The address is:
ws://localhost:8080/comet?user_id=:user_id
Please give a arbitrary valid :user_id param, such as x.
You can send a message to the browser user client in step 2. Method is post, body is 'aplication/json' format. user_id and message param required. such as:
curl -d '{"user_id": "x", "message": "test"}' http://localhost:8080/messages
Then the user x you specified will receive the test message.
For more details, please visit my blog.
You can test WebSocket connection and message receive in this page.
http://localhost:8080/index/