StreamObserver via NexusConfig.streamObserver to receive structured callbacks for every SSE lifecycle transition. The observer is the canonical way to wire SDK state into ops dashboards, custom telemetry, or load-test tooling - far more reliable than scraping console output by substring match.
The observer is opt-in. When NexusConfig.streamObserver is undefined the SDK uses NOOP_STREAM_OBSERVER at zero overhead.
The interface
{ onEvent: (n) => count(n) } and ignore the rest.
Hook semantics
What counts as a “whitelisted event”
onEvent fires only for events on the SDK’s documented event-type list:
resyncconfig.created/config.updated/config.deletedflag.created/flag.updated/flag.toggled/flag.deletedsecret.created/secret.updated/secret.deleted
auth_expiring control event) do NOT trigger onEvent.
onFallback reasons
FALLBACK_REASON_RATE_LIMITED - dual semantics (v0.3.1+)
As of v0.3.1, 'rate-limited' fires in both of the following situations:
- Terminate case (no
Retry-After) - the server returned429without a parseableRetry-Afterheader. The SDK closes the stream, setsclient.streamStatusto'fallback', and does NOT reconnect. This is the legacy behaviour, preserved as-is. - Cooperative-reconnect case (with
Retry-After) - the server returned429with a parseableRetry-Afterheader (clamped to[5 s, 5 min]). The SDK firesonFallback('rate-limited')for the metric, then schedules an automatic reconnect after the indicated delay. The Retry-After delay does NOT count towardMAX_FAILURES.
Quarantine
429 (v0.4.0+, body {"error":"quarantined",...}) does NOT fire onFallback - it fires onQuarantined instead. The stream reconnects automatically at expiresAt.onFallback('rate-limited') is per rate-limit episode - not a one-shot “the SDK has given up” signal. If you need to distinguish the two cases, inspect client.streamStatus shortly after the callback returns: it will be 'fallback' in the terminate case and either 'connected' or transitioning back to 'connected' in the cooperative-reconnect case.
Threading contract
If your observer does anything substantial (network I/O, mutex contention, disk writes), copy the argument and dispatch the work viasetImmediate, queueMicrotask, or your own queue before returning:
Counter pattern
Active-connection gauge pattern
onFallback is included because in the terminate cases the SDK will not reconnect on its own - the gauge should stay false until you call connectStream() again. In the v0.3.1+ cooperative rate-limited path (server sent a Retry-After), the SDK will reconnect automatically and onConnected will fire again when the stream comes back.
Independence from the logger
The observer is independent of any logging output. The two are complementary:- The SDK’s
console.*output carries human-readable text for operators. StreamObservercarries structured metrics for tooling.
