Manage connection reconnection and suspension
masterThe Connection object automatically attempts to reconnect when the connection is lost and resubscribes to event listeners upon reconnection.
Suspend Reconnection
To prevent automatic reconnection until a specific condition is met, pass a promise to suspendReconnectUntil(). Messages sent while suspended will be queued and sent once the connection is re-established. If the first reconnect attempt fails, queued messages are rejected.
connection.suspendReconnectUntil(
new Promise((resolve) => {
// Resolve this to allow reconnection
resolve();
}),
);Suspend Connection
To actively close the connection and wait for a promise to resolve before reconnecting, use suspend(). You can also use suspendReconnectUntil() followed by suspend().
connection.suspend(
new Promise((resolve) => {
resolve();
}),
);Connection Events
| Event | Data | Description |
|---|---|---|
ready | - | Fired when authentication is successful and the connection is ready. |
disconnected | - | Fired when the connection is lost. |
reconnect-error | Error code | Fired on fatal reconnection errors (e.g., ERR_INVALID_AUTH). |
Closing
Use connection.close() to close the connection without attempting to reconnect.