Core
withSSEReconnect
Configures automatic reconnection after an SSE stream fails.
@defjs/core · Function
Signature
function withSSEReconnect( options: ClientSSEOptions["reconnect"], ): ClientOption;
Reconnection is opt-in: without this option a failed stream is not retried. Passing undefined clears an earlier setting.
type SSEReconnect = {
attempts?: number;
delayMs?: number;
factor?: number;
jitter?: number;
maxDelayMs?: number;
shouldReconnect?: (context: {
attempt: number;
cause?: unknown;
lastEventId: string;
method: string;
open?: {
response: { status: number; statusText: string; url: string };
url: string;
};
}) => boolean | Promise<boolean>;
};attemptsis the retry budget for each outage. It defaults to3whenreconnectis set, and0disables retries. A successful handshake resets the budget, callbackattempt, and backoff exponent. Omitting it does not give unlimited retries within one outage. Repeated successful opens followed immediately by retryable stream failures can keep reconnecting at the current base delay (subject to jitter and caps), because each open resets the budget; useshouldReconnectto stop them. Normal EOF remains terminal.- Without
shouldReconnect, onlyGET,HEAD,OPTIONS,PUT, andDELETEreconnect. With it, its result decides for every method,POSTincluded;methodin the context tells endpoints apart. Returnfalseto stop for this failure. It runs before the attempts limit is checked. - A request with a
ReadableStreambody never reconnects, andshouldReconnectis not called for it. delayMs,factor,jitter(0 to 1), andmaxDelayMsshape the backoff.factordefaults to1andjitterto0.maxDelayMscaps the final delay after jitter and rounding. Without a configured cap, the platform timer limit of2_147_483_647ms still applies. A zero base stays zero; valid-number overflow saturates at the caps.delayMsis only the starting base delay (1000by default). A serverretry:field (digits only, in milliseconds) replaces it for later attempts.- The last event id is sent again as UTF-8 bytes in
Last-Event-IDon the next attempt. An emptyid:clears it; an ID containing NUL is ignored. - Reconnect options are validated before interceptors or Fetch:
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 returnREQ_OPTIONS_INVALIDwhen the operation runs.
Parameters
Prop
Type
options ClientSSEOptions["reconnect"]Retry budget, backoff, and shouldReconnect gate.
Returns
An option that sets config.sse.reconnect.