pub struct WebsocketStreams { /* private fields */ }Implementations§
Source§impl WebsocketStreams
impl WebsocketStreams
Sourcepub fn subscribe_on_ws_events<F>(&self, callback: F) -> Subscription
pub fn subscribe_on_ws_events<F>(&self, callback: F) -> Subscription
Subscribes to WebSocket events with a provided callback function.
§Arguments
callback- A mutable function that takes aWebsocketEventand isSendand'static.
§Returns
A Subscription that can be used to manage the event subscription.
§Examples
let subscription = websocket_streams.subscribe_on_ws_events(|event| {
// Handle WebSocket event
});
Sourcepub fn unsubscribe_from_ws_events(&self, subscription: Subscription)
pub fn unsubscribe_from_ws_events(&self, subscription: Subscription)
Unsubscribes from WebSocket events for a given Subscription.
§Arguments
subscription- TheSubscriptionto unsubscribe from WebSocket events.
§Examples
let subscription = websocket_streams.subscribe_on_ws_events(|event| {
// Handle WebSocket event
});
websocket_streams.unsubscribe_from_ws_events(subscription);
Sourcepub async fn disconnect(&self) -> Result<()>
pub async fn disconnect(&self) -> Result<()>
Disconnects the WebSocket connection.
§Returns
A Result indicating whether the disconnection was successful.
Returns an error if the disconnection fails.
§Errors
Returns an anyhow::Error if the connection fails.
§Examples
let websocket_streams = WebSocketStreams::new(…);
websocket_streams.disconnect().await?;
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Sourcepub async fn ping_server(&self)
pub async fn ping_server(&self)
Sends a ping to the WebSocket server to maintain the connection.
§Examples
websocket_streams.ping_server().await;
This method sends a ping request to the WebSocket server to keep the connection alive and check the server’s responsiveness.
Sourcepub fn subscribe(&self, streams: Vec<String>, id: Option<u32>)
pub fn subscribe(&self, streams: Vec<String>, id: Option<u32>)
Subscribes to specified WebSocket streams.
§Arguments
streams- A vector of stream names to subscribe toid- An optional identifier for the subscription request
§Examples
websocket_streams.subscribe(vec![“btcusdt@trade".to_string()], None);
This method initiates an asynchronous subscription to the specified WebSocket streams.
The subscription is performed in a separate task using spawn.
Sourcepub fn unsubscribe(&self, streams: Vec<String>, id: Option<u32>)
pub fn unsubscribe(&self, streams: Vec<String>, id: Option<u32>)
Unsubscribes from specified WebSocket streams.
§Arguments
streams- A vector of stream names to unsubscribe fromid- An optional identifier for the unsubscription request
§Examples
websocket_streams.unsubscribe(vec![“btcusdt@trade".to_string()], None);
This method initiates an asynchronous unsubscription from the specified WebSocket streams.
The unsubscription is performed in a separate task using spawn.
Sourcepub async fn is_subscribed(&self, stream: &str) -> bool
pub async fn is_subscribed(&self, stream: &str) -> bool
Checks if the current WebSocket stream is subscribed to a specific stream.
§Arguments
stream- The name of the stream to check for subscription
§Returns
A boolean indicating whether the stream is currently subscribed
§Examples
let is_subscribed = websocket_streams.is_subscribed("btcusdt@trade").await;
This method checks the subscription status of a specific WebSocket stream.
Sourcepub async fn aggregate_trade_stream(
&self,
params: AggregateTradeStreamParams,
) -> Result<Arc<WebsocketStream<AggregateTradeStreamResponse>>>
pub async fn aggregate_trade_stream( &self, params: AggregateTradeStreamParams, ) -> Result<Arc<WebsocketStream<AggregateTradeStreamResponse>>>
Aggregate Trade Stream
Pushes aggregate trade updates for a symbol.
§Arguments
params:AggregateTradeStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AggregateTradeStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn all_book_ticker_stream(
&self,
params: AllBookTickerStreamParams,
) -> Result<Arc<WebsocketStream<AllBookTickerStreamResponse>>>
pub async fn all_book_ticker_stream( &self, params: AllBookTickerStreamParams, ) -> Result<Arc<WebsocketStream<AllBookTickerStreamResponse>>>
All Book Ticker Stream
Pushes best bid/ask updates for all symbols.
§Arguments
params:AllBookTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AllBookTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn all_mini_ticker_stream(
&self,
params: AllMiniTickerStreamParams,
) -> Result<Arc<WebsocketStream<AllMiniTickerStreamResponse>>>
pub async fn all_mini_ticker_stream( &self, params: AllMiniTickerStreamParams, ) -> Result<Arc<WebsocketStream<AllMiniTickerStreamResponse>>>
All Mini Ticker Stream
Pushes mini ticker statistics for all symbols.
§Arguments
params:AllMiniTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AllMiniTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn all_ticker_stream(
&self,
params: AllTickerStreamParams,
) -> Result<Arc<WebsocketStream<AllTickerStreamResponse>>>
pub async fn all_ticker_stream( &self, params: AllTickerStreamParams, ) -> Result<Arc<WebsocketStream<AllTickerStreamResponse>>>
All Ticker Stream
Pushes full ticker statistics for all symbols.
§Arguments
params:AllTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AllTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn all_tokens24h_ticker_stream(
&self,
params: AllTokens24hTickerStreamParams,
) -> Result<Arc<WebsocketStream<AllTokens24hTickerStreamResponse>>>
pub async fn all_tokens24h_ticker_stream( &self, params: AllTokens24hTickerStreamParams, ) -> Result<Arc<WebsocketStream<AllTokens24hTickerStreamResponse>>>
All Tokens 24h Ticker Stream
Pushes 24h ticker-like metrics for all tokens.
§Arguments
params:AllTokens24hTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AllTokens24hTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn book_ticker_stream(
&self,
params: BookTickerStreamParams,
) -> Result<Arc<WebsocketStream<BookTickerStreamResponse>>>
pub async fn book_ticker_stream( &self, params: BookTickerStreamParams, ) -> Result<Arc<WebsocketStream<BookTickerStreamResponse>>>
Book Ticker Stream
Pushes best bid/ask updates for a symbol.
§Arguments
params:BookTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::BookTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn contract_kline_stream(
&self,
params: ContractKlineStreamParams,
) -> Result<Arc<WebsocketStream<ContractKlineStreamResponse>>>
pub async fn contract_kline_stream( &self, params: ContractKlineStreamParams, ) -> Result<Arc<WebsocketStream<ContractKlineStreamResponse>>>
Contract Kline Stream
Pushes kline updates by contractAddress@chainId.
§Arguments
params:ContractKlineStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::ContractKlineStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn full_depth_stream(
&self,
params: FullDepthStreamParams,
) -> Result<Arc<WebsocketStream<FullDepthStreamResponse>>>
pub async fn full_depth_stream( &self, params: FullDepthStreamParams, ) -> Result<Arc<WebsocketStream<FullDepthStreamResponse>>>
Full Depth Stream
Returns all available depth, including UI and API orders.
§Arguments
params:FullDepthStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::FullDepthStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn kline_stream(
&self,
params: KlineStreamParams,
) -> Result<Arc<WebsocketStream<KlineStreamResponse>>>
pub async fn kline_stream( &self, params: KlineStreamParams, ) -> Result<Arc<WebsocketStream<KlineStreamResponse>>>
Kline Stream
Pushes kline updates for a symbol.
§Arguments
params:KlineStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::KlineStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn mini_ticker_stream(
&self,
params: MiniTickerStreamParams,
) -> Result<Arc<WebsocketStream<MiniTickerStreamResponse>>>
pub async fn mini_ticker_stream( &self, params: MiniTickerStreamParams, ) -> Result<Arc<WebsocketStream<MiniTickerStreamResponse>>>
Mini Ticker Stream
Pushes 24h rolling mini ticker statistics.
§Arguments
params:MiniTickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::MiniTickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn partial_depth_stream(
&self,
params: PartialDepthStreamParams,
) -> Result<Arc<WebsocketStream<PartialDepthStreamResponse>>>
pub async fn partial_depth_stream( &self, params: PartialDepthStreamParams, ) -> Result<Arc<WebsocketStream<PartialDepthStreamResponse>>>
Partial Depth Stream
Pushes partial depth updates (UI orders only).
§Arguments
params:PartialDepthStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::PartialDepthStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn ticker_stream(
&self,
params: TickerStreamParams,
) -> Result<Arc<WebsocketStream<TickerStreamResponse>>>
pub async fn ticker_stream( &self, params: TickerStreamParams, ) -> Result<Arc<WebsocketStream<TickerStreamResponse>>>
Ticker Stream
Pushes full 24h rolling ticker statistics.
§Arguments
params:TickerStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::TickerStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.
Sourcepub async fn trade_stream(
&self,
params: TradeStreamParams,
) -> Result<Arc<WebsocketStream<TradeStreamResponse>>>
pub async fn trade_stream( &self, params: TradeStreamParams, ) -> Result<Arc<WebsocketStream<TradeStreamResponse>>>
Trade Stream
Pushes raw trade updates for a symbol.
§Arguments
params:TradeStreamParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::TradeStreamResponse>> on success.
§Errors
Returns an anyhow::Error if the stream request fails, if parameters are invalid, or if parsing the response fails.
For full API details, see the Binance API Documentation.