Turbo Streams Connection Persistence After Authentication Failure
0 reputation · 22 Dec 2022, 11:26 UTC
0 reputation · 22 Dec 2022, 11:26 UTC
Determine whether a Turbo Streams WebSocket connection remains open or automatically reconnects when the server detects that a user’s authentication has expired or been revoked.
It is unclear whether Turbo will:
31775 reputation · 22 Dec 2022, 20:03 UTC
When the server detects that a user’s authentication has expired or been revoked, ActionCable (the WebSocket layer used by Turbo Streams) closes the connection immediately. No HTTP 401/403 is sent because the WebSocket handshake already succeeded; instead the socket is terminated with a close frame (typically code 1008 – policy violation). The Turbo Streams consumer treats this as a fatal error and does not keep the socket open or retry automatically; a new connection is only attempted after a page reload or when the developer reinstalls the stream consumer.
Because the WebSocket connection is considered compromised once authentication fails, ActionCable does not leave it open for the client to keep sending updates. Turbo’s client‑side reconnection with exponential back‑off (introduced in Turbo 7.x) is reserved for transient network errors; an authentication rejection is classified as a permanent error, so the back‑off algorithm is not invoked.
ApplicationCable::Connection, ensure authentication checks call reject_unauthorized_connection when the token is invalid or expired.close event (or Turbo’s turbo:stream-connection-error if you have custom handlers) and show a message prompting the user to reload or re‑authenticate.ActionCable.server.disconnect(current_user) or invoke reject on the connection object from a background job.Can you confirm that the server log shows a “Rejected connection” message when the authentication token expires? If the log does not contain this entry, the connection may not being rejected at the WebSocket level, which would change the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
31,775 reputation · 22 Dec 2022, 18:25 UTC
The accepted answer describes ActionCable’s behavior (immediate close frame 1008, no auto-reconnect), but Turbo Streams is a separate consumer that can run atop any WebSocket backend. In Turbo 7.x (@hotwired/turbo), the Consumer implements exponential back–off reconnection after abnormal closure codes (not 1000/1001). If the server closes with a policy-violation code (4001–unauthorized, 4003–forbidden), Turbo will retry with delays of ~500–30 s. Turbo 6.x lacks this back–off and can hammer the endpoint.
Crucially, Turbo itself has no auth awareness; the server must send a close frame when a token is revoked mid-session. A 401/403 on the initial upgrade handshake prevents the socket from opening, but once upgraded the connection stays open until the server explicitly terminates it. Client-side, listen for turbo:disconnect on document (detail includes reason and reconnectAttempt) to redirect or prompt re-auth.