Skip to Content
SDKC# / .NETReal-time SSE

Automatic connection

InitAsync() always starts with a snapshot request that loads the current flags. When Options.Stream is true, the client then opens an SSE connection (/sdk/stream) and flags are updated in the background without polling.

using CanaryGate; var client = new CanaryGateClient("cg_key", new Options { Stream = true, }); await client.InitAsync(); // From this point on, client.GetFlag() always returns the latest // value without needing a manual refetch client.Disconnect();

If the connection drops, the client reconnects with exponential backoff (ReconnectDelay, capped at MaxReconnectDelay). When the cache is stale at reconnect time, the snapshot is re-fetched before stream events are processed. If no data arrives within HeartbeatTimeout, the connection is aborted and a reconnect is triggered.

IsStale()

Returns true when the last flags sync failed or the stream connection is down (for example, while reconnecting). Use it to detect a degraded state.

if (client.IsStale()) { // Data may be stale // Consider reloading or showing a warning to the user }

GetLastSyncAt()

Returns the ISO timestamp (string?) of the last successful sync, or null if InitAsync() has not completed yet.

var lastSync = client.GetLastSyncAt(); if (lastSync != null) { Console.WriteLine($"Flags updated at {lastSync}"); }

Disconnect()

Closes the stream connection (if any) and releases resources. Always call this on teardown:

// The client implements IDisposable, so you can also rely on using: using var client = new CanaryGateClient("cg_key", new Options { Stream = true }); await client.InitAsync();

After Disconnect(), GetFlag() continues to return the last known values, but the client no longer receives updates.

Last updated on