- Docs
- Core Concepts
- Connection
- Connection Lifecycle
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:
| Event | When |
|---|---|
initialized | Client constructed |
connecting | Dial in progress (attempt in payload) |
opened | WebSocket transport open (before protocol ready) |
connected | Server ready (connectionId, connectionDetails) |
disconnected | Transport dropped |
closing | Intentional close in progress |
closed | Closed (code, reason) |
failed | Unrecoverable 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:
| Option | Default |
|---|---|
autoReconnect | true |
autoResubscribe | true |
maxReconnectAttempts | 10 |
initialReconnectDelayMs | 1000 |
maxReconnectDelayMs | 30000 |
reconnectBackoffMultiplier | 1.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();
Previous
Connection
Next
Channel