Skip to navigation
connect(): Promise<void>

Establishes the WebSocket connection and authenticates the session.

Reconnection behavior

After a successful connection the SDK automatically reconnects whenever the socket closes unexpectedly (e.g. network change, server restart). Reconnection uses an exponential back-off strategy:

  • First retry after reconnectDelayMin (default 0.1 s).
  • Each subsequent retry doubles the delay up to reconnectDelayMax (default 3 s).
  • The delay resets to reconnectDelayMin once a connection succeeds.
  • A per-attempt connectionTimeout (default 10 s) aborts the attempt and schedules the next retry if the server does not respond.

The SDK also replaces a connection that has gone silent without closing. If the server’s keepalive pings stop for 15 seconds, the SDK pings the server itself. When that ping goes unanswered for 10 seconds, the SDK drops the socket and reconnects.

An active call survives a short drop. Once the session re-authenticates, the SDK resumes the call’s keepalive, so the server doesn’t end the call over a reply lost during the drop.

Calling disconnect() stops the reconnection loop entirely.

When the server refuses the credential

A reconnect that can’t resume the previous session authenticates from scratch. A fresh credential comes from your CredentialProvider: authenticate() when the session is Client Bound, otherwise refresh() when the provider implements it. Without either, the SDK reuses the current credential.

  • Client Bound session, or a credential past its expiry_at: the SDK gets a fresh credential before authenticating. A refusal is final.
  • Any other session: the SDK authenticates with the current credential. If the server refuses it, the SDK gets a fresh credential and retries once. A second refusal is final.

On a final refusal the SDK emits the error on errors$, stops reconnecting, and disconnects the client, so isConnected$ emits false. Call connect() again once a valid credential is available.

Message handling during temporary disconnections

While the socket is down, outgoing requests are held. The SDK sends them in order once the session re-authenticates on the new socket, so no outgoing RPC call is lost. A held request that has already failed for its caller, for example by timing out, is discarded instead of being sent late. Calling disconnect() discards every held request.

Incoming server-to-client messages that arrive while the socket is down are not buffered by the SDK — they are expected to be re-delivered by the server after the session is re-authenticated. Active RPC calls that were awaiting a response will time out (default 5 s) and reject with an RPCTimeoutError; callers should handle this and retry if appropriate.

To follow the connection from your app, subscribe to ready$. It emits false while the SDK reconnects and true once the session has re-authenticated. isConnected$ changes only when you call connect() or disconnect(), or when the SDK disconnects after a refused credential.

Returns

Promise<void>

Examples

await client.connect();