Skip to main content
The SDK can hold a persistent GET /v1/stream connection open. Whenever the Nexus backend emits a change event (config.updated, flag.toggled, secret.created, resync, …), the SDK triggers an immediate full sync - so the cache is updated within milliseconds of the change instead of waiting for the next TTL tick.

FPM warning

connectStream() blocks the calling PHP process indefinitely. Never call it from a PHP-FPM request handler. Blocking a worker means it cannot serve any further HTTP requests until the method returns (which may be never). Use connectStream() only in long-running CLI scripts, queue consumers (php artisan queue:work, php bin/console messenger:consume), or Swoole/ReactPHP-based workers.

Enabling it

Call connectStream() after NexusClient::create returns:
The method blocks the outer reconnect loop. When $maxErrors consecutive transport errors occur, the SDK falls back to TTL polling, sleeps the reconnect cooldown schedule, then retries the SSE connection automatically. The process never needs to be restarted or supervised externally for this to work.

Custom max errors

What happens under the hood

  1. The SDK opens an HTTP streaming response to <baseUrl>/v1/stream with Accept: text/event-stream and either X-Nexus-API-Key: <key> or Authorization: Bearer <session_jwt> (when WIF is enabled).
  2. The server immediately sends a resync event, which causes the SDK to call sync() to align with the current state.
  3. The connection stays open. Every change event triggers another full sync.
  4. While the stream is up, TTL polling continues as a safety net.

Events table

The SDK does not apply event payloads directly to the cache - every event triggers a full re-sync that returns the current authoritative state. This avoids any drift between the SDK’s view and the backend.

Reconnection and backoff

Any transport error (server restart, TCP drop, network blip) triggers a reconnect with exponential backoff: The backoff counter resets to zero after a successful connection is re-established.

Max errors behaviour

After $maxErrors consecutive transport errors, the SDK falls back to TTL polling and enters the reconnect cooldown schedule. It then retries SSE automatically - no manual restart or wrapper loop is needed:
  • Your application is never notified; reads continue to be served from the cache during the cooldown.
  • The only visible effect during the cooldown is that propagation latency increases from milliseconds to the TTL.
  • Once the cooldown elapses, the SDK re-opens the SSE stream and resumes sub-millisecond propagation.
The reconnect cooldown schedule is controlled by $sseReconnectCooldown (v0.6.0+). The default schedule is [5, 10, 20, 40, 60] minutes - each successive fallback waits longer before retrying. Pass an int for a fixed interval or an array for a custom schedule (the last value is reused indefinitely).

auth_expiring event (WIF)

When WIF is enabled, the backend emits an auth_expiring SSE control event approximately 5 minutes before the session JWT expires. The SDK reacts by triggering a non-blocking session refresh. When the server force-closes the stream at expiry, the SDK reconnects automatically with the freshly-issued bearer token.

Disabling SSE

Just don’t call connectStream(). The SDK runs in pure TTL-polling mode. For most PHP-FPM applications this is the correct choice - the TTL is short enough that changes appear within one polling interval.

Laravel queue worker example

Run with php artisan nexus:daemon under a process supervisor (Supervisor, systemd) that restarts on exit.