Skip to main content
Pass a 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

Every method is optional - the SDK calls only the ones you set. You can write { 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:
  • resync
  • config.created / config.updated / config.deleted
  • flag.created / flag.updated / flag.toggled / flag.deleted
  • secret.created / secret.updated / secret.deleted
Unknown events (or the WIF 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:
  1. Terminate case (no Retry-After) - the server returned 429 without a parseable Retry-After header. The SDK closes the stream, sets client.streamStatus to 'fallback', and does NOT reconnect. This is the legacy behaviour, preserved as-is.
  2. Cooperative-reconnect case (with Retry-After) - the server returned 429 with a parseable Retry-After header (clamped to [5 s, 5 min]). The SDK fires onFallback('rate-limited') for the metric, then schedules an automatic reconnect after the indicated delay. The Retry-After delay does NOT count toward MAX_FAILURES.
Quarantine 429 (v0.4.0+, body {"error":"quarantined",...}) does NOT fire onFallback - it fires onQuarantined instead. The stream reconnects automatically at expiresAt.
In other words, 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

Observer methods are called synchronously from the SDK’s stream-reader event loop. They MUST return promptly.
If your observer does anything substantial (network I/O, mutex contention, disk writes), copy the argument and dispatch the work via setImmediate, 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.
  • StreamObserver carries structured metrics for tooling.