I am trying to figure out how to create a turbo stream that connects to a Spring Boot powered Websocket. I know what to render on the Spring Boot backend.
My problem is:
How do I get the html frontend to connect to the websocket with turbo using html or javascrip?
Do you have any pointers? That would be great and would spare me to figure it out using the rails code.
Hi, thanks that points to the right direction. Yet this still is using server side events and not websockets as shown in the sample video. What I’m trying to get working is with websockets so I will have to dig deeper.
i think on the client-side it would be the same, so just pass the websocket connection to the Turbo.connectStreamSource() function. Have you tried this?
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
Turbo.connectStreamSource(socket);
@remast FYI, I’m working on a SpringBoot translation of the hotwire demo chat. Only up to the stimulus controller step in the video (@5m46s in) but plan to work through the entire video.
I don’t know how the signed stream name is generated, but I did notice that it does not change when you refresh the page or restart the rails server.
The response (after the web socket handshake) from the Rail’s web socket server looks like plain JSON:
The message attribute is a <turbo-stream-action> XML element encoded as a JSON string.
So far, the only SpringBoot web socket examples I have seen use the Stomp protocol to communicate with the client, but I don’t think that will work with Turbo client—the rails server 101 response to the ws://localhost:3000/cable request includes the header Sec-WebSocket-Protocol: actioncable-v1-json, which matches up with the JSON I see on the network pane.
Ah thanks, that points in the right direction. And yes I also did not find a good vanilla websocket sample for Spring Boot and left it aside for the moment. I’ll get back to it when I find the time.