Skip to main content

Module websocket

Module websocket 

Source
Expand description

WebSocket client for real-time market data streaming.

This module provides a WebSocket client for connecting to Binance’s real-time data streams with support for:

  • Auto-reconnection with exponential backoff
  • Depth cache management (local order book)
  • User data stream keep-alive
  • Connection health monitoring

§Example

use binance_api_client::{Binance, WebSocketClient};
use futures::StreamExt;

#[tokio::main]
async fn main() -> binance_api_client::Result<()> {
    let client = Binance::new_unauthenticated()?;
    let ws = client.websocket();

    // Connect to a single stream
    let stream = ws.agg_trade_stream("btcusdt");
    let mut conn = ws.connect(&stream).await?;

    while let Some(event) = conn.next().await {
        match event? {
            WebSocketEvent::AggTrade(trade) => {
                println!("{}: {} @ {}", trade.symbol, trade.quantity, trade.price);
            }
            _ => {}
        }
    }

    Ok(())
}

Structs§

ConnectionHealthMonitor
Monitors WebSocket connection health with periodic pings.
DepthCache
A local order book cache that maintains bid/ask levels.
DepthCacheConfig
Configuration for the depth cache manager.
DepthCacheManager
Manages a local order book cache with WebSocket updates.
ReconnectConfig
Configuration for auto-reconnection behavior.
ReconnectingWebSocket
A WebSocket connection with automatic reconnection support.
UserDataStreamManager
Manages a user data stream with automatic keep-alive.
WebSocketClient
WebSocket client for connecting to Binance streams.
WebSocketConnection
An active WebSocket connection.
WebSocketEventStream
A Stream wrapper for WebSocket events.

Enums§

ConnectionState
Connection state for reconnecting WebSocket.
DepthCacheState
State of the depth cache manager.