Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection.
The chat and the charts data is provided in realtime using the SSE implemention of Gin Framework.
Nick | Message |
---|
func streamRoom(c *gin.Context) {
roomid := c.ParamValue("roomid")
listener := openListener(roomid)
statsTicker := time.NewTicker(1 * time.Second)
defer closeListener(roomid, listener)
defer statsTicker.Stop()
c.Stream(func(w io.Writer) bool {
select {
case msg := <-listener:
c.SSEvent("message", msg)
case <-statsTicker.C:
c.SSEvent("stats", Stats())
}
return true
})
}
function StartSSE(roomid) {
var source = new EventSource('/stream/'+roomid);
source.addEventListener('message', newChatMessage, false);
source.addEventListener('stats', stats, false);
}