Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo stream source #415

Merged
merged 4 commits into from Jun 19, 2022
Merged

Turbo stream source #415

merged 4 commits into from Jun 19, 2022

Conversation

seanpdoyle
Copy link
Contributor

Improve test coverage for streams over SSE

Add a <form> element to the src/tests/fixtures/stream.html file so
that tests can exercise receiving <turbo-stream> elements
asynchronously over an EventSource instance that polls for Server-sent
Events.

Extend the src/tests/server.ts to account for the Accept: text/event-stream requests that are made by browsers via the new EventSource(...) instance.

Introduce <turbo-stream-source>

Closes #413

The <turbo-stream-source> element accepts a [src] attribute, and
uses that to connect Turbo to poll for streams published on the server
side.

When the element is connected to the document, the stream source is
connected. When the element is disconnected, the stream is disconnected.

When declared with an ws:// or wss:// URL, the underlying Stream
Source will be a WebSocket instance. Otherwise, the connection is
through an EventSource.

Since the document's <head> is persistent across navigations, the
<turbo-stream-source> is meant to be mounted within the <body>
element.

Typical full page navigations driven by Turbo will result in the
<body> being discarded and replaced with the resulting document. It's
the server's responsibility to ensure that the element is present on
each page that requires streaming.

Add a `<form>` element to the `src/tests/fixtures/stream.html` file so
that tests can exercise receiving `<turbo-stream>` elements
asynchronously over an `EventSource` instance that polls for Server-sent
Events.

Extend the `src/tests/server.ts` to account for the `Accept:
text/event-stream` requests that are made by browsers via the `new
EventSource(...)` instance.
Closes hotwired#413

The `<turbo-stream-source>` element accepts a `[src]` attribute, and
uses that to connect Turbo to poll for streams published on the server
side.

When the element is connected to the document, the stream source is
connected. When the element is disconnected, the stream is disconnected.

When declared with an `ws://` or `wss://` URL, the underlying Stream
Source will be a `WebSocket` instance. Otherwise, the connection is
through an `EventSource`.

Since the document's `<head>` is persistent across navigations, the
`<turbo-stream-source>` is meant to be mounted within the `<body>`
element.

Typical full page navigations driven by Turbo will result in the
`<body>` being discarded and replaced with the resulting document. It's
the server's responsibility to ensure that the element is present on
each page that requires streaming.
@delitescere
Copy link

Great!

I am not sure about the head / body assertion being made, nor the assumption about full page updates by Turbo (I certainly would never do that).

@dhh
Copy link
Member

dhh commented Oct 3, 2021

This is great, @seanpdoyle, we just need to pair it up with the existing tag that was put in turbo-rails. Or rather, make turbo-rails use this more generic approach directly.

@seanpdoyle
Copy link
Contributor Author

Unless I'm misunderstanding some fundamentals about the Action Cable source, that element isn't declared with an https:// or wss:// URL. Instead, it's declared with a channel name, which is passed directly to an already configured wss:// URL.

I understand the idea to unify the two to re-use the same mechanics or classes, but I don't see a path forward.

@dhh
Copy link
Member

dhh commented Jun 19, 2022

Ah, yes, fair enough. This is mostly then for use outside of Rails. Which is fine.

@dhh
Copy link
Member

dhh commented Jun 19, 2022

There's a few lint errors, if you want to sort.

@dhh dhh merged commit 631701d into hotwired:main Jun 19, 2022
@seanpdoyle seanpdoyle deleted the turbo-stream-source branch June 19, 2022 16:49
seanpdoyle added a commit to seanpdoyle/turbo-site that referenced this pull request Jun 19, 2022
Document the `<turbo-stream-source>` element introduced in
[hotwired/turbo#415][].

[hotwired/turbo#415]: hotwired/turbo#415
dhh added a commit to seanpdoyle/turbo that referenced this pull request Jul 15, 2022
* main:
  Drive Browser tests with `playwright` (hotwired#609)
  Allow Turbo Streams w/ GET via `data-turbo-stream` (hotwired#612)
  Only update history when Turbo visit is renderable (hotwired#601)
  Support development ChromeDriver version overrides (hotwired#606)
  Turbo stream source (hotwired#415)
  Expose Frame load state via `[complete]` attribute (hotwired#487)
dhh added a commit to feliperaul/turbo that referenced this pull request Jul 16, 2022
* main:
  Allow frames to scroll smoothly into view (hotwired#607)
  Export Type declarations for `turbo:` events (hotwired#452)
  Add .php as a valid isHTML extension (hotwired#629)
  Add original click event to 'turbo:click' details (hotwired#611)
  Drive Browser tests with `playwright` (hotwired#609)
  Allow Turbo Streams w/ GET via `data-turbo-stream` (hotwired#612)
  Only update history when Turbo visit is renderable (hotwired#601)
  Support development ChromeDriver version overrides (hotwired#606)
  Turbo stream source (hotwired#415)
  Expose Frame load state via `[complete]` attribute (hotwired#487)
  fix(ie/edge): form.method='delete', raises Invalid argument. (hotwired#586)
  Do not declare global types/constants (hotwired#524)
  Defensively create custom turbo elements (hotwired#483)
  Use `replaceChildren` in StreamActions.update (hotwired#534)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

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