Feature Request: declarative connection to a Turbo Stream using the `turbo-stream` custom tag
See original GitHub issueCurrently, some JavaScript is needed to connect a Turbo Stream to an EventSource (SSE) or WebSocket (by the way, the docs aren’t very clear about how to do this).
It would be superb if it could be done without JavaScript, rather using an incantation of the turbo-stream
custom element.
Current imperative mechanism:
(window['EventSource'] && window['Turbo']) ?
Turbo.connectStreamSource(new EventSource('/my-turbo-stream')) :
console.warn('Turbo Streams over SSE not available');
Suggested declarative mechanism:
<turbo-stream src="/my-turbo-stream">
If the URL scheme was ws:
then Turbo would create a new WebSocket
, otherwise it would be an EventSource
.
The addition of a src
attribute to the custom turbo-stream
tag need not (but may) be conflated with the use of it in directives to altering the DOM as content arrives on the stream or as the result of a form submission (i.e. the existing capabilities of the custom tag).
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
[Feature Request] Custom actions for streams #247 - GitHub
It may be useful to send custom actions by turbo streams like this: which I'm currently using for reloading frames with an src...
Read more >Dynamic forms with Turbo - Thoughtbot
The request encodes an Accept: text/html, application/xhtml+xml HTTP header, and expects an HTML document in its response. When the frame ...
Read more >Come Alive with Turbo Streams - Turbo Handbook
Turbo Streams deliver page changes over WebSocket, SSE or in response to form submissions using just HTML and a set of CRUD-like actions....
Read more >Turbo Frames and Turbo Stream templates - Hotrails
This custom element has a unique id corresponding to the first argument we passed to the turbo_frame_tag helper. This <turbo-frame> HTML tag ......
Read more >Turbo Streams on Rails - Colby.so
For fancier use cases, you can broadcast changes over a WebSocket connection to keep every interested user updated without a refresh. No custom...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for sharing that code, I think we’re aligned now.
To re-contextualize a part of my original comment:
I think we’re still discussing the details of a backend agnostic version of the turbo-cable-stream-source element. Based on your code sample, I’m interpreting that you’re suggesting the current turbo-stream element be extended. Is that correct?
I think overloading the element to behave differently based on the presence or absence of a src attribute could be tricky. Maybe a new turbo-stream-source element (like the Rails version, but without the “cable”) could serve that single purpose.
Is there something about extending the semantics and behavior of the existing turbo-stream element that’s more compelling than introducing a new element?
I appreciate it might be tricky, indeed.
To me, it’s the same duality as “turbo-frame acting as a source” and “turbo-frame acting as a target”.
But if it were named slightly differently, that is less important than having it at all. Of course, as I’m not writing it, I defer to the developers to choose the smoothest implementation.
Much appreciated!
Also see https://link.medium.com/hcGxj0262jb for more (a lot more) context