An open API service indexing awesome lists of open source software.

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.

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)