ClientSSEOptions
Optional SSE settings applied when creating a client: fetch handle, invalid-event hook, and reconnect policy.
@defjs/core · Type
Signature
interface ClientSSEOptions { handle?: FetchHandle; onInvalidEvent?: (context: { reason: "missing-struct" | "validation-failed"; message: { id: string; event: string; data: string }; cause?: unknown; signal: AbortSignal; }) => unknown; reconnect?: { 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>; }; }
- handle — Fetch implementation that opens the stream. The default resolves
globalThis.fetchon each attempt, including replacements installed after import. Every SSE handle receives aRequest. Missing global Fetch fails default execution withENV_UNSUPPORTED; a custom handle remains usable. - onInvalidEvent — Called when an event has no matching struct and no
defaultstruct ('missing-struct'), or its data fails validation ('validation-failed'). The event is skipped. An error thrown by the hook does not close the stream. - reconnect — Opt-in. Without it the stream never reconnects.
attemptsdefaults to 3 per outage;attempts: 0disables retry. A successful handshake resets the budget, callbackattempt, and backoff exponent.delayMsis the first base delay, 1000 by default, and a server-sentretry:value replaces it.factordefaults to 1.jitteris a 0–1 multiplicative factor and defaults to 0.maxDelayMscaps the final delay after jitter and rounding; without a configured cap, the platform limit of2_147_483_647ms still applies. A zero base stays zero; valid-number overflow saturates at the caps. WithoutshouldReconnectonlyGET,HEAD,OPTIONS,PUT, andDELETEreconnect; with it, its result decides for every method and its context carriesmethod. AReadableStreamrequest body never reconnects. Reconnect sends the latest ID as UTF-8 bytes inLast-Event-ID; an empty ID clears it and an ID containing NUL is ignored.
Reconnect options are validated before interceptors or Fetch: attempts is a non-negative safe integer; delayMs must be finite and non-negative; maxDelayMs must be a non-negative finite number or positive Infinity, which removes the configured cap but not the platform limit of 2_147_483_647 ms; factor is finite and positive; jitter is finite and within 0–1. Invalid values return REQ_OPTIONS_INVALID when the operation runs.
Set these with withSSEHandle, withSSEOnInvalidEvent, and withSSEReconnect. Omitting withSSEReconnect disables retry.
Members
Prop
Type
handle? FetchHandle | undefinedFetch implementation used to open the event stream; defaults to resolving global fetch on each call.
onInvalidEvent? ((context: { reason: "missing-struct" | "validation-failed"; message: { id: string; event: string; data: string; }; cause?: unknown; signal: AbortSignal; }) => unknown) | undefinedCalled when an event has no matching struct or fails validation.
reconnect? { 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>; } | undefinedAutomatic reconnect policy after stream failures.