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

FEATURE: Swap HTML on Server Sent Events (take 5) #194

Merged
merged 3 commits into from
Sep 14, 2020
Merged

FEATURE: Swap HTML on Server Sent Events (take 5) #194

merged 3 commits into from
Sep 14, 2020

Conversation

benpate
Copy link
Collaborator

@benpate benpate commented Sep 14, 2020

This PR implements the new SSE syntax proposed by @1cg, and fixes my previous PR that was built upon the wrong base branch I'll briefly summarize his spec here:

Swapping on Named Events

This PR adds a new swap on syntax to the hx-sse attribue that can be used to swap content into the DOM for any named SSE event.

  <div hx-sse="connect /event_stream">
    <div hx-sse="swap on chatter">
      ...
    </div>
  </div>

The swap on indicates that this element wants to listen for chatter messages and swap the body when they occur.

The hx-swap attribute can then be used to determine exactly how the swap is done, as with normal swaps.

Data-only Messages

In the case of a data-only message, the message type is message according to the SSE spec. Thus, the form swap on message should work the same as the named event implementation.

  <div hx-sse="connect /event_stream">
    <div hx-sse="swap on message">
      ...
    </div>
  </div>

Declaring and Using An SSE Endpoint On The Same Element

This PR supports both connecting and listening for an event (or events) in the same hx-sse declaration, by
allowing comma separated values:

  <div hx-sse="connect /event_stream, swap on message">
     ...
  </div>

Leave Triggers In Place

This PR does not change the way that SSE can be used to trigger additional events in HTMX. So the following, original code still works:

  <div hx-sse="connect /event_stream">
    <div hx-get="/chatroom" hx-trigger="sse:chatter">
      ...
    </div>
  </div>

Links

  1. MDN SSE Article - https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
  2. SSE Spec - https://html.spec.whatwg.org/multipage/server-sent-events.html
  3. Initial SSE improvement PR - FEATURE: Swap HTML on Server Sent Events (like WebSockets) #155
  4. Second SSE improvement PR - FEATURE: Swap HTML on Server Sent Events (like WebSockets) improved version #185
  5. Updated SSE syntax proposal - https://gist.github.com/1cg/5dd26530aeec92a9389b367747bbb5ee

Update base branch to bigskysoftware/htmx::dev
- implement new SSE syntax
- add comments to delimit the start and end of SSE code block
- update splitOnWhitespace to allow leading/trailing whitespace
- reorganize maybeCloseSSESource() to bottom of SSE code block
These three test cases demonstrate SSE using a remote service.
@1cg 1cg merged commit 3c64876 into bigskysoftware:dev Sep 14, 2020
@1cg
Copy link
Contributor

1cg commented Sep 14, 2020

OK, merged!

How about some docs?

@benpate
Copy link
Collaborator Author

benpate commented Sep 14, 2020

Sure. I'll put those together tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants