Skip to main content
Pass a NexusStreamObserver 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 NexusLogger output by substring match. The observer is opt-in. When 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

onFallback reasons

Threading contract

Observer methods MUST return promptly. The SDK calls them synchronously from the stream reader loop on the main thread. If your observer does anything substantial (network I/O, structured logging fan-out), copy the argument and dispatch the work to a future microtask.

Counter pattern

Active-connection state pattern

Maintain a “live” indicator backed by state. The observer updates state from outside React’s render cycle, so use a global store like Zustand / Jotai:
For a built-in alternative without a state library, use the existing useNexusStreamStatus() hook - it returns the same connected boolean derived from the streamConnected getter.