Connection Lifecycle

A Socket connection moves through a small set of SDK events from construction to ready, then reconnects automatically when the transport drops.

Events

Listen on socket.connection:

EventWhen
initializedClient constructed
connectingDial in progress (attempt in payload)
openedWebSocket transport open (before protocol ready)
connectedServer ready (connectionId, connectionDetails)
disconnectedTransport dropped
closingIntentional close in progress
closedClosed (code, reason)
failedUnrecoverable failure (error, context)
socket.connection.on("connected", ({ connectionId }) => {
  console.log("connected", connectionId);
});

socket.connection.on("failed", ({ error }) => {
  console.error(error);
});

Subscribe to channels after connected (or rely on autoResubscribe after reconnect).

Manual connect / disconnect

const socket = new QPub.Socket({
  apiKey: process.env.QPUB_API_KEY,
  autoConnect: false,
});

await socket.connection.connect();
socket.connection.disconnect();

Reconnect

Defaults:

OptionDefault
autoReconnecttrue
autoResubscribetrue
maxReconnectAttempts10
initialReconnectDelayMs1000
maxReconnectDelayMs30000
reconnectBackoffMultiplier1.5

After reconnect, the SDK resubscribes to active channels when autoResubscribe is true.

Ping

const rttMs = await socket.connection.ping();

Uses protocol ping/pong. pingTimeoutMs defaults to 10000.

Helpers

socket.connection.isConnected();