question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: declarative connection to a Turbo Stream using the `turbo-stream` custom tag

See original GitHub issue

Currently, 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
seanpdoylecommented, Oct 3, 2021

Thank you for sharing that code, I think we’re aligned now.

To re-contextualize a part of my original comment:

If your use case is outside of Rails, I’m interpreting the scope of the Issue as much larger, including the introduction of a backend agnostic version of the turbo-cable-stream-source.

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?

0reactions
delitescerecommented, Oct 3, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found