Go Example for TurboStreams over WebSockets
Simple example for using Turbos Streams in Go with the Gorilla WebSocket toolkit.
Run the sample using the following command:
$ go run *.go
To use the chat example, open http://localhost:8080/ in your browser.
Frontend
The frontend connects to the Turbo Stream using plain JavaScript like:
<script src="https://cdn.jsdelivr.net/npm/@hotwired/turbo@8.0.4/dist/turbo.es2017-umd.min.js" ></script>
<script>
Turbo.connectStreamSource(new WebSocket("ws://" + document.location.host + "/ws"));
</script>
After that the frontend is connected to the Turbo Stream and get's all messages. Every chat message is appended to the dom element with id board
.
This should work with html markup too but I have not gotten it working yet.
Server
The server receives the new chat message via web socket. Then it wraps the message as Turbo Stream…