Using ExchangeSocketClient for real-time WebSocket data
ExchangeSocketClient is the main entry point for all WebSocket (real-time push) interactions. It provides the same exchange-property pattern as ExchangeRestClient, and adds unified subscribe methods that can target one exchange or many simultaneously. The client manages connection lifecycle automatically, including reconnection on drop.
Every SubscribeTo* method returns ExchangeResult<UpdateSubscription>, which you use to manage the active subscription. There are two overloads for each subscription type: one that targets a single named exchange, and one that fans out to multiple exchanges.
var socketClient = new ExchangeSocketClient();var symbol = new SharedSymbol(TradingMode.Spot, "ETH", "USDT");var result = await socketClient.SubscribeToTickerUpdatesAsync( "Binance", new SubscribeTickerRequest(symbol), data => Console.WriteLine($"[{data.Exchange}] {data.Data.Symbol}: {data.Data.LastPrice}"));if (!result.Success){ Console.WriteLine($"Subscription failed: {result.Error}"); return;}UpdateSubscription subscription = result.Data;
var socketClient = new ExchangeSocketClient();var symbol = new SharedSymbol(TradingMode.Spot, "ETH", "USDT");// Subscribe on Binance and OKX simultaneouslyvar results = await socketClient.SubscribeToTickerUpdatesAsync( new SubscribeTickerRequest(symbol), data => Console.WriteLine($"[{data.Exchange}] {data.Data.Symbol}: {data.Data.LastPrice}"), [Exchange.Binance, Exchange.OKX]);// Check each result individuallyforeach (var result in results){ if (!result.Success) Console.WriteLine($"{result.Exchange} subscription failed: {result.Error}");}
When exchanges is omitted (or null), the subscription is created on all exchanges that support the request type.
The UpdateSubscription object returned by each successful subscribe call lets you pause, resume, or cancel the stream.
// Unsubscribe from a single streamawait subscription.CloseAsync();// Unsubscribe and close every connection on the clientawait socketClient.UnsubscribeAllAsync();
The client exposes three read-only properties to help you observe runtime behaviour:
// Total active WebSocket connections across all exchange clientsint connections = socketClient.CurrentConnections;// Total active subscriptions across all connectionsint subscriptions = socketClient.CurrentSubscriptions;// Aggregate incoming data rate in kilobytes per seconddouble kbps = socketClient.IncomingKbps;
Just like the REST client, every supported exchange is directly accessible as a typed property, giving you full access to the exchange-native WebSocket API when the shared interfaces are not enough:
// Use the Binance-specific socket client directlyvar sub = await socketClient.Binance.SpotApi.ExchangeData .SubscribeToMiniTickerUpdatesAsync("ETHUSDT", data => { Console.WriteLine($"Binance ETH/USDT: {data.Data.LastPrice}"); });
WebSocket connections are managed by the underlying CryptoExchange.Net library. If a connection drops, the client automatically attempts to reconnect and re-establish all active subscriptions. Reconnect behaviour can be tuned via options:
var socketClient = new ExchangeSocketClient( globalOptions: options => { options.ReconnectInterval = TimeSpan.FromSeconds(5); });
User-data subscriptions on some exchanges (such as Binance) require a listen key. Obtain one via the REST client first and pass the result as listenKeyResults when subscribing.