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.
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
AuthExpiredwhen 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 code | Means | What to do |
|---|---|---|
Draining | The server is restarting | Reconnect after the delay it gives, and resume from your cursors |
Backpressure | The client couldn't keep up with deliveries | Reconnect and resume from your cursors |
Timeout | Nothing was received for 90 seconds | Reconnect |
AuthExpired | The token ran out | Reconnect with a new token |
Replaced | A newer connection of the same identity took this one's place | Don't reconnect automatically: show "signed in elsewhere" |
AuthRevoked | Your backend kicked this identity or revoked this token | Don't reconnect automatically: sign the user out |
Suspended | The segment is off, or the workspace's package isn't active | Don't retry |
AuthInvalid | The token is wrong for this segment | Don'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
| Limit | Set by | Over it |
|---|---|---|
| Connections at once, per workspace | Your package | A grace window, then new connections get QuotaExceeded |
| Connections at once, per segment | The segment's settings | New connections get QuotaExceeded |
| Connections per identity | The segment's settings | The oldest is closed with Replaced (default), or the new one refused |
| Anonymous connections | The segment's settings | New anonymous connections get QuotaExceeded |
| New connections per minute | The segment's rate preset | RateLimited, with a delay |
| Subscriptions per connection | 1,000 | QuotaExceeded for the subscribe |
Open connections are never closed for going over a package allowance. See Limits.