Expand description
Binance WebSocket implementation
Provides WebSocket real-time data stream subscriptions for the Binance exchange.
§Architecture
The WebSocket implementation is organized into several modules:
types- Configuration structures and enums (BinanceWsConfig,DepthLevel, etc.)parsers- Stream message parsers implementingStreamParsertraitconnection_manager- Connection sharding and managementhandlers- Message routing and dispatchsubscriptions- Subscription tracking and management
§Usage
ⓘ
use ccxt_exchanges::binance::ws::{BinanceWs, BinanceWsConfig, DepthLevel, UpdateSpeed};
// Create a WebSocket client
let ws = BinanceWs::new("wss://stream.binance.com:9443/ws".to_string());
// Subscribe to ticker updates
let ticker = ws.watch_ticker("BTC/USDT", MarketType::Spot).await?;
// Check connection health
let health = ws.health();
println!("Latency: {:?}ms", health.latency_ms);§Health Monitoring
Use the BinanceWs::health method to get connection metrics:
- Latency (from ping/pong)
- Message counts (received/dropped)
- Connection uptime
- Reconnection count
§Error Handling
Errors are classified into recovery strategies via WsErrorRecovery:
Retry- Transient errors, retry with backoffResync- State out of sync, need to refresh (e.g., orderbook)Reconnect- Connection lost, need to reconnectFatal- Unrecoverable, requires user intervention
Re-exports§
pub use connection_manager::BinanceConnectionManager;pub use parsers::BidAskParser;pub use parsers::MarkPriceParser;pub use parsers::OhlcvParser;pub use parsers::StreamParser;pub use parsers::TickerParser;pub use parsers::TradeParser;pub use types::BinanceWsConfig;pub use types::DepthLevel;pub use types::UpdateSpeed;pub use types::WsChannelConfig;pub use types::WsErrorRecovery;pub use types::WsHealthSnapshot;
Modules§
- connection_
manager - Connection manager module
- parsers
- Stream parsers for WebSocket messages Stream parsers for WebSocket message parsing
- types
- Types and configuration for WebSocket WebSocket types and configuration for Binance
Structs§
- Binance
Ws - Binance WebSocket client wrapper
- Listen
KeyManager - Listen key manager with multi-market type support
- Message
Router - Message router for handling WebSocket connections and message routing
- Reconnect
Config - Reconnect configuration
- Subscription
- Subscription metadata
- Subscription
Handle - A handle to an active subscription that automatically unsubscribes when dropped.
- Subscription
Manager - Subscription manager
Enums§
- Subscription
Type - Subscription types supported by the Binance WebSocket API.
Functions§
- normalize_
symbol - Normalizes a symbol to Binance’s expected format.