Core
withWebSocketReconnect
Configures automatic reconnection when the socket closes for any reason other than a manual session.close().
@defjs/core · Function
Signature
function withWebSocketReconnect( options: ClientWebSocketOptions["reconnect"], ): ClientOption;
Reconnection is opt-in: without this option a close ends the session. Passing undefined clears an earlier setting. The option type is:
{
attempts?: number
delayMs?: number
factor?: number
jitter?: number
maxDelayMs?: number
shouldReconnect?: (context: { attempt: number; cause?: unknown; code?: number; reason?: string; wasClean?: boolean }) => boolean
}attemptsis the retry budget for each outage. It defaults to3, and0disables reconnection. Successfulopenresets the budget and backoff exponent; the next outage starts at retry 1. Omitting it does not give unlimited retries within one outage. A socket that successfully opens and is immediately closed again by the server can keep reconnecting at the base delay (subject to jitter and caps), because every open resets the budget; useshouldReconnectto stop it.delayMsdefaults to1000,factorto2,maxDelayMsto30000, andjitter(0 to 1) to0. Retry n starts withdelayMs * factor^(n-1), capped atmaxDelayMs; jitter and rounding follow, then the final delay is capped again atmaxDelayMsand2_147_483_647ms. A zero base stays zero; valid-number overflow saturates at those caps.shouldReconnectcan returnfalseto end the session instead. Without it every close is retried while the budget lasts. It is not called for a manualsession.close().- Reconnect options are validated before connecting:
attemptsmust be a non-negative safe integer;delayMsmust be finite and non-negative;maxDelayMsmust be a non-negative finite number or positiveInfinity, which removes the configured cap but not the platform limit of2_147_483_647ms;factormust be finite and positive;jittermust be finite and within 0–1. Invalid values make the operation fail withREQ_OPTIONS_INVALIDbefore connecting.attempts: 0disables subsequent retries, but all explicitly supplied reconnect fields are still validated before the initial connection. - A
reconnectin the request options replaces this one for that call.
Parameters
Prop
Type
options ClientWebSocketOptions["reconnect"]Retry budget, backoff, and shouldReconnect gate.
Returns
An option that sets config.webSocket.reconnect.