Skip to content

Add resumable websockets - #6929

Open
SteffenDE wants to merge 5 commits into
sd-transport-sessionfrom
sd-resumable-websocket
Open

SteffenDE wants to merge 5 commits into
sd-transport-sessionfrom
sd-resumable-websocket

Conversation

@SteffenDE

Copy link
Copy Markdown
Member

Part 4(a) of #6885 (comment).

@SteffenDE
SteffenDE added this pull request to stack #6927 November 26, 2026 12:10
@SteffenDE SteffenDE changed the title Sd resumable websocket Add resumable websockets Nov 26, 2026
@SteffenDE
SteffenDE force-pushed the sd-resumable-websocket branch from 3d62936 to 9bdc3ca Compare November 30, 2026 16:36
@SteffenDE
SteffenDE force-pushed the sd-resumable-websocket branch 2 times, most recently from 726bf0a to e97f8bf Compare December 2, 2026 17:32
Only connect/3 checks a socket, when the client connects. Long polling
keeps the socket when a request fails, and the client resumes it with
its next poll, which phoenix.js marks with phx:resume. The session now
passes the params and connect_info of that poll to the new optional
Phoenix.Socket.resume/3 callback, for example to verify that the user
is still logged in. Returning :error stops the socket, so the client
connects again. Custom transports can implement the new optional
Phoenix.Socket.Transport.handle_resume/2 callback.

The poll that resumes answers right away when the session has no
messages, as the client waits for it before it reports that it
recovered. The session answers requests in order and only answers a
flush when it has messages, so its echo of a request sent after the
flush tells that it has none.

A client that resumes also sends the private topic of its token. A
session rejects a topic that is not its own, as another session can
get the pid of a session that stopped, and keeps running for its own
client.

LongPoll now reads the params and auth token of the socket again for a
poll that resumes, so the socket checks its current credentials.
With the new :resumable WebSocket option, clients that opt in with the
resumable: true option of the phoenix.js Socket resume their socket over
a new WebSocket after the connection failed, without rejoining channels.
:resumable can also be an MFA that decides for each new connection.

The socket runs in a Phoenix.Transports.Session, like a long-poll
socket. The WebSocket process attaches to it as a pipe. Both sides
number their messages, keep them until the other side confirmed them,
and send the missed ones again after resuming. The client resumes with
the token of the session and the number of the last message it
received, which invokes Phoenix.Socket.resume/3. Heartbeats, their
replies, and ping and pong frames belong to the current WebSocket, so
neither side numbers or replays them.

The new ResumableWebSocket transport resumes the session with backoff,
reports interruptions with onSuspend and onResume, and resumes instead
of reconnecting when the heartbeat times out. When the session is gone,
or the server does not keep the socket, it closes so that the socket
connects again.

A session continues with the transport that started it. Long-poll
requests only prove that the client has the token, so a session that a
WebSocket started rejects them with 410, and a WebSocket cannot resume
a long-poll session. The session keeps running for its own client.

Parts of this come from #6887 and #6905.
@SteffenDE
SteffenDE force-pushed the sd-resumable-websocket branch from e97f8bf to 5ed4530 Compare December 2, 2026 17:51
Only nodes before Phoenix v1.8.18 and 1.8.18 nodes serving older clients
send unnumbered requests. Older clients have no WebSocket session token,
so the session stops for such a request like for any other.

This branch has not been deployed

No deployments
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.

1 participant