For use with hotwire turbo, this package allows you to quickly get up and running with responding to websockets with turbo-streams
turbostream.Logger(log.New(os.Stdout, "[turbostream]", log.Lshortfile))
hub = turbostream.NewHub()
go hub.Run()
r.HandleFunc("/ws", websocketHandler)
<snip>
func websocketHandler(w http.ResponseWriter, r *http.Request) {
turbostream.HandleWs(hub, session_id, w, r)
}
hub.Broadcast(turbostream.Message("append","example_div_id",fmt.Sprint(time.Now().Unix(),"<br>")))
hub.Subscribe(session_id,channel_name)
hub.Unsubscribe(session_id,channel_name)
hub.SendChannel(channel_id,message)
The session id can be any unique value for the cleint
err:= hub.Send(session_id,turbostream.Message("append","example_div_id","Hello!"))
if(err!=nil){
fmt.Fprintf(w, err.Error())
}