folkwaysDocs
Concepts

Connections

How a connection opens, stays alive, refreshes its token, closes and reconnects, and the limits on connections.

A client connects to the Realtime API over a WebSocket, or over HTTP where WebSockets are blocked. Either way it authenticates with a token. This page describes the WebSocket lifecycle; HTTP requests are stateless and carry the token on each request. See Transports.

The client sends Hello with its token within 5 seconds and gets Welcome with its identity, limits and features. It subscribes and gets an optional Gap, a SubAck and any replay or snapshot, then live Deliver frames. It publishes and gets a PubAck. It pings every 30 seconds and gets Pong; 90 seconds of silence closes it with Timeout. Sixty seconds before expiry it gets TokenExpiring, sends Reauth with a new token and gets OpOk. When the service ends a connection it sends Close with a code and a retry delay first.

Opening

The first thing a client sends is its token, within 5 seconds. The answer, Welcome, tells it:

  • its identity (for anonymous connections, anon: plus the session ID);
  • its session ID, unique to this connection;
  • the capabilities its token grants, so the app can hide what it may not do;
  • the limits that apply: payload size, subscriptions per connection, rate limits, heartbeat interval;
  • the segment's ID and environment, and which features are available (presence, personal topics).

Staying alive

The client sends a ping every 30 seconds. A connection that sends nothing for 90 seconds is closed with Timeout.

Token refresh

Sixty seconds before the token expires, the service warns the connection. The client fetches a new token from your backend and sends it on the same connection; subscriptions stay as they are.

  • The new token must be for the same segment and identity, or the connection closes with AuthInvalid.
  • An anonymous connection may refresh with a signed-in user's token: it keeps its subscriptions and acts as that user from then on. The reverse needs a new connection.
  • If the new token grants less, subscriptions it no longer covers end with Forbidden.
  • Without a refresh, the connection closes with AuthExpired when the token expires.

Closing

When the service closes a connection, it first says why, with an error code and, when it makes sense, how long to wait before reconnecting. A connection can also simply drop, with no code, when the network goes away; treat that as temporary and reconnect. What to do after a close depends on the code:

Close codeMeansWhat to do
DrainingThe server is restartingReconnect after the delay it gives, and resume from your cursors
BackpressureThe client couldn't keep up with deliveriesReconnect and resume from your cursors
TimeoutNothing was received for 90 secondsReconnect
AuthExpiredThe token ran outReconnect with a new token
ReplacedA newer connection of the same identity took this one's placeDon't reconnect automatically: show "signed in elsewhere"
AuthRevokedYour backend kicked this identity or revoked this tokenDon't reconnect automatically: sign the user out
SuspendedThe segment is off, or the workspace's package isn't activeDon't retry
AuthInvalidThe token is wrong for this segmentDon't retry with the same token

Reconnecting

Reconnect with exponential backoff and full jitter, waiting at least the delay the close gave. Then send the token again and subscribe to each topic after its saved cursor. There is no session resume: subscriptions are restored by subscribing again. The JavaScript SDK does all of this for you.

Limits on connections

LimitSet byOver it
Connections at once, per workspaceYour packageA grace window, then new connections get QuotaExceeded
Connections at once, per segmentThe segment's settingsNew connections get QuotaExceeded
Connections per identityThe segment's settingsThe oldest is closed with Replaced (default), or the new one refused
Anonymous connectionsThe segment's settingsNew anonymous connections get QuotaExceeded
New connections per minuteThe segment's rate presetRateLimited, with a delay
Subscriptions per connection1,000QuotaExceeded for the subscribe

Open connections are never closed for going over a package allowance. See Limits.

On this page