https://github.com/remast/go_websocket_turbo
Simple example for using Turbos Streams in Go with the Gorilla WebSocket toolkit.
https://github.com/remast/go_websocket_turbo
Last synced: about 1 year ago
JSON representation
Simple example for using Turbos Streams in Go with the Gorilla WebSocket toolkit.
- Host: GitHub
- URL: https://github.com/remast/go_websocket_turbo
- Owner: remast
- Created: 2020-12-30T20:25:16.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-14T07:43:58.000Z (almost 2 years ago)
- Last Synced: 2025-03-29T07:41:45.627Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 31
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Example for TurboStreams over WebSockets
Simple example for using [Turbo](https://turbo.hotwire.dev/)s [Stream](https://turbo.hotwire.dev/reference/streams)s in Go with the [Gorilla WebSocket](https://github.com/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:
```html
Turbo.connectStreamSource(new WebSocket("ws://" + document.location.host + "/ws"));
```
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 action with action `append` and broadcasts it to all subscribers. That way all subscribed users see the new message on the board.
The raw text message sent over the web socket is:
```json
{
"identifier":
"{\"channel\":\"Turbo::StreamsChannel\", \"signed_stream_name\":\"**mysignature**\"}",
"message":
"
My new Message
"
}
```
## Credits
Based on [Gorilla WebSocket Chat Example](https://github.com/gorilla/websocket/tree/master/examples/chat)