Skip to main content

Module ws

Module ws 

Source
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 implementing StreamParser trait
  • connection_manager - Connection sharding and management
  • handlers - Message routing and dispatch
  • subscriptions - 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 backoff
  • Resync - State out of sync, need to refresh (e.g., orderbook)
  • Reconnect - Connection lost, need to reconnect
  • Fatal - 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§

BinanceWs
Binance WebSocket client wrapper
ListenKeyManager
Listen key manager with multi-market type support
MessageRouter
Message router for handling WebSocket connections and message routing
ReconnectConfig
Reconnect configuration
Subscription
Subscription metadata
SubscriptionHandle
A handle to an active subscription that automatically unsubscribes when dropped.
SubscriptionManager
Subscription manager

Enums§

SubscriptionType
Subscription types supported by the Binance WebSocket API.

Functions§

normalize_symbol
Normalizes a symbol to Binance’s expected format.