pub struct BinanceWs { /* private fields */ }Expand description
Binance WebSocket client wrapper
Implementations§
Source§impl BinanceWs
impl BinanceWs
Sourcepub fn new_with_config(config: BinanceWsConfig) -> Self
pub fn new_with_config(config: BinanceWsConfig) -> Self
Creates a new Binance WebSocket client with custom configuration.
§Arguments
config- Configuration options including URL, channel capacities, and backpressure strategy
§Example
use ccxt_exchanges::binance::ws::{BinanceWs, BinanceWsConfig, WsChannelConfig};
use ccxt_core::ws_client::BackpressureStrategy;
let config = BinanceWsConfig::new("wss://stream.binance.com:9443/ws".to_string())
.with_backpressure(BackpressureStrategy::DropOldest);
let ws = BinanceWs::new_with_config(config);Sourcepub fn new_with_auth(
url: String,
binance: Arc<Binance>,
market_type: MarketType,
) -> Self
pub fn new_with_auth( url: String, binance: Arc<Binance>, market_type: MarketType, ) -> Self
Creates a WebSocket client with a listen key manager for a specific market type
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnects from the WebSocket server
Sourcepub async fn shutdown(&self) -> Result<()>
pub async fn shutdown(&self) -> Result<()>
Gracefully shuts down the WebSocket client.
This method performs a complete shutdown sequence:
- Sets the shutting down flag to reject new subscriptions
- Stops the message router
- Stops listen key auto-refresh
- Clears all subscriptions
- Clears cached data
After calling this method, the client cannot be reused. This method is idempotent - calling it multiple times is safe.
Sourcepub fn is_shutting_down(&self) -> bool
pub fn is_shutting_down(&self) -> bool
Returns true if the client is shutting down or has shut down.
Sourcepub async fn connect_user_stream(&self) -> Result<()>
pub async fn connect_user_stream(&self) -> Result<()>
Connects to the user data stream
Sourcepub async fn close_user_stream(&self) -> Result<()>
pub async fn close_user_stream(&self) -> Result<()>
Closes the user data stream
Sourcepub async fn get_listen_key(&self) -> Option<String>
pub async fn get_listen_key(&self) -> Option<String>
Returns the active listen key, when available
Sourcepub async fn subscribe_ticker(&self, symbol: &str) -> Result<Receiver<Value>>
pub async fn subscribe_ticker(&self, symbol: &str) -> Result<Receiver<Value>>
Subscribes to the ticker stream for a symbol.
Returns a receiver that will receive ticker updates as JSON values. The caller is responsible for consuming messages from the receiver.
Sourcepub async fn subscribe_all_tickers(&self) -> Result<Receiver<Value>>
pub async fn subscribe_all_tickers(&self) -> Result<Receiver<Value>>
Subscribes to the 24-hour ticker stream for all symbols.
Returns a receiver that will receive ticker updates as JSON values.
Sourcepub async fn subscribe_trades(&self, symbol: &str) -> Result<Receiver<Value>>
pub async fn subscribe_trades(&self, symbol: &str) -> Result<Receiver<Value>>
Subscribes to real-time trade executions for a symbol.
Returns a receiver that will receive trade updates as JSON values.
Sourcepub async fn subscribe_agg_trades(
&self,
symbol: &str,
) -> Result<Receiver<Value>>
pub async fn subscribe_agg_trades( &self, symbol: &str, ) -> Result<Receiver<Value>>
Subscribes to the aggregated trade stream for a symbol.
Returns a receiver that will receive aggregated trade updates as JSON values.
Sourcepub async fn subscribe_orderbook(
&self,
symbol: &str,
levels: DepthLevel,
update_speed: UpdateSpeed,
) -> Result<Receiver<Value>>
pub async fn subscribe_orderbook( &self, symbol: &str, levels: DepthLevel, update_speed: UpdateSpeed, ) -> Result<Receiver<Value>>
Subscribes to the order book depth stream.
Returns a receiver that will receive order book updates as JSON values.
§Arguments
symbol- Trading pair symbol (e.g., “BTCUSDT” or “BTC/USDT”)levels- Depth level (L5, L10, or L20)update_speed- Update frequency (Ms100 or Ms1000)
§Example
use ccxt_exchanges::binance::ws::{DepthLevel, UpdateSpeed};
let mut rx = ws.subscribe_orderbook("BTCUSDT", DepthLevel::L20, UpdateSpeed::Ms100).await?;
while let Some(msg) = rx.recv().await {
println!("Order book update: {:?}", msg);
}Sourcepub async fn subscribe_orderbook_diff(
&self,
symbol: &str,
update_speed: Option<UpdateSpeed>,
) -> Result<Receiver<Value>>
pub async fn subscribe_orderbook_diff( &self, symbol: &str, update_speed: Option<UpdateSpeed>, ) -> Result<Receiver<Value>>
Subscribes to the diff order book stream.
Returns a receiver that will receive order book diff updates as JSON values.
§Arguments
symbol- Trading pair symbolupdate_speed- Optional update frequency
Sourcepub async fn subscribe_kline(
&self,
symbol: &str,
interval: &str,
) -> Result<Receiver<Value>>
pub async fn subscribe_kline( &self, symbol: &str, interval: &str, ) -> Result<Receiver<Value>>
Subscribes to Kline (candlestick) data for a symbol.
Returns a receiver that will receive kline updates as JSON values.
Sourcepub async fn subscribe_mini_ticker(
&self,
symbol: &str,
) -> Result<Receiver<Value>>
pub async fn subscribe_mini_ticker( &self, symbol: &str, ) -> Result<Receiver<Value>>
Subscribes to the mini ticker stream for a symbol.
Returns a receiver that will receive mini ticker updates as JSON values.
Sourcepub async fn subscribe_all_mini_tickers(&self) -> Result<Receiver<Value>>
pub async fn subscribe_all_mini_tickers(&self) -> Result<Receiver<Value>>
Subscribes to the mini ticker stream for all symbols.
Returns a receiver that will receive mini ticker updates as JSON values.
Sourcepub async fn unsubscribe(&self, stream: String) -> Result<()>
pub async fn unsubscribe(&self, stream: String) -> Result<()>
Cancels an existing subscription.
Only sends the UNSUBSCRIBE command to the server when the reference count reaches zero (no more active handles for this stream).
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Indicates whether the WebSocket connection is active
Sourcepub fn state(&self) -> WsConnectionState
pub fn state(&self) -> WsConnectionState
Returns the current connection state.
Sourcepub fn subscriptions(&self) -> Vec<String>
pub fn subscriptions(&self) -> Vec<String>
Returns the list of active subscriptions.
Returns a vector of subscription channel names that are currently active. This method retrieves the actual subscriptions from the underlying WsClient’s subscription manager, providing accurate state tracking.
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the number of active subscriptions.
Sourcepub fn health(&self) -> WsHealthSnapshot
pub fn health(&self) -> WsHealthSnapshot
Returns a health snapshot for monitoring the WebSocket connection.
This provides metrics useful for monitoring connection health:
- Latency from ping/pong
- Message counts (received and dropped)
- Connection uptime
- Reconnection count
Sourcepub async fn get_cached_ticker(&self, symbol: &str) -> Option<Ticker>
pub async fn get_cached_ticker(&self, symbol: &str) -> Option<Ticker>
Returns cached ticker snapshot
Sourcepub async fn get_all_cached_tickers(&self) -> HashMap<String, Ticker>
pub async fn get_all_cached_tickers(&self) -> HashMap<String, Ticker>
Returns all cached ticker snapshots