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§
- Connection
Health Monitor - Monitors WebSocket connection health with periodic pings.
- Depth
Cache - A local order book cache that maintains bid/ask levels.
- Depth
Cache Config - Configuration for the depth cache manager.
- Depth
Cache Manager - Manages a local order book cache with WebSocket updates.
- Reconnect
Config - Configuration for auto-reconnection behavior.
- Reconnecting
WebSocket - A WebSocket connection with automatic reconnection support.
- User
Data Stream Manager - Manages a user data stream with automatic keep-alive.
- WebSocket
Client - WebSocket client for connecting to Binance streams.
- WebSocket
Connection - An active WebSocket connection.
- WebSocket
Event Stream - A
Streamwrapper for WebSocket events.
Enums§
- Connection
State - Connection state for reconnecting WebSocket.
- Depth
Cache State - State of the depth cache manager.