Improve WebSocket messages and reconnects - #3910
Open
scriptogre wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Re-implement htmx 2.0's WebSocket extension "queue messages while disconnected" behavior. Reconnect only when a close is retryable, and give incoming and outgoing messages one hookable lifecycle.
Changes
Code
1000stops by default. Per-elementhx-configoverrides globalhtmx.config.wsvalues.Name events by direction
htmx:before:ws:connectionhtmx:ws:before:connectionhtmx:after:ws:connectionhtmx:ws:after:connectionhtmx:before:ws:requesthtmx:ws:before:message:outgoinghtmx:after:ws:requesthtmx:ws:after:message:outgoinghtmx:before:ws:messagehtmx:ws:before:message:incominghtmx:after:ws:messagehtmx:ws:after:message:incomingAlign with
htmx:<extension>:<timing>:<noun>.Expose message-shaped hooks
Outgoing values move from a nested
bodyto top-level keys.headersis reserved for htmx metadata.Incoming hooks preserve native data and provide cached conversions:
Correlate replies with senders
One shared WebSocket can carry messages from many send elements.
hx-wsadds a uniqueHX-Message-IDto each outgoing message.The server can copy that ID into an incoming message:
The incoming message then uses the original sender for inherited attributes, relative targets, swaps, and events. The first matching incoming message consumes the association.
ws.pendingMessageTTLcontrols how long that sender mapping remains available.Docs
Docs cover the new behavior and Beta-to-RC1 migration, including:
HX-Request-IDtoHX-Message-IDws.pendingRequestTTLtows.pendingMessageTTL