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<String>)
pub fn subscribe(&self, streams: Vec<String>, id: Option<String>)
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).await;
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<String>)
pub fn unsubscribe(&self, streams: Vec<String>, id: Option<String>)
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).await;
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 agg_trade(
&self,
params: AggTradeParams,
) -> Result<Arc<WebsocketStream<AggTradeResponse>>>
pub async fn agg_trade( &self, params: AggTradeParams, ) -> Result<Arc<WebsocketStream<AggTradeResponse>>>
WebSocket Aggregate Trade Streams
The Aggregate Trade Streams push trade information that is aggregated for a single taker order.
§Arguments
params:AggTradeParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AggTradeResponse>> 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_market_rolling_window_ticker(
&self,
params: AllMarketRollingWindowTickerParams,
) -> Result<Arc<WebsocketStream<Vec<AllMarketRollingWindowTickerResponseInner>>>>
pub async fn all_market_rolling_window_ticker( &self, params: AllMarketRollingWindowTickerParams, ) -> Result<Arc<WebsocketStream<Vec<AllMarketRollingWindowTickerResponseInner>>>>
WebSocket All Market Rolling Window Statistics Streams
Rolling window ticker statistics for all market symbols, computed over multiple windows. Note that only tickers that have changed will be present in the array.
§Arguments
params:AllMarketRollingWindowTickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<Vec<models::AllMarketRollingWindowTickerResponseInner>>> 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(
&self,
params: AllMiniTickerParams,
) -> Result<Arc<WebsocketStream<Vec<AllMiniTickerResponseInner>>>>
pub async fn all_mini_ticker( &self, params: AllMiniTickerParams, ) -> Result<Arc<WebsocketStream<Vec<AllMiniTickerResponseInner>>>>
WebSocket All Market Mini Tickers Stream
24hr rolling window mini-ticker statistics for all symbols that changed in an array. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs. Note that only tickers that have changed will be present in the array.
§Arguments
params:AllMiniTickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<Vec<models::AllMiniTickerResponseInner>>> 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 avg_price(
&self,
params: AvgPriceParams,
) -> Result<Arc<WebsocketStream<AvgPriceResponse>>>
pub async fn avg_price( &self, params: AvgPriceParams, ) -> Result<Arc<WebsocketStream<AvgPriceResponse>>>
WebSocket Average Price
Average price streams push changes in the average price over a fixed time interval.
§Arguments
params:AvgPriceParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::AvgPriceResponse>> 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 block_trade(
&self,
params: BlockTradeParams,
) -> Result<Arc<WebsocketStream<BlockTradeResponse>>>
pub async fn block_trade( &self, params: BlockTradeParams, ) -> Result<Arc<WebsocketStream<BlockTradeResponse>>>
WebSocket Block Trade Streams
§Arguments
params:BlockTradeParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::BlockTradeResponse>> 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(
&self,
params: BookTickerParams,
) -> Result<Arc<WebsocketStream<BookTickerResponse>>>
pub async fn book_ticker( &self, params: BookTickerParams, ) -> Result<Arc<WebsocketStream<BookTickerResponse>>>
WebSocket Individual Symbol Book Ticker Streams
Pushes any update to the best bid or ask’s price or quantity in real-time for a specified symbol.
Multiple <symbol>@bookTicker streams can be subscribed to over one connection.
§Arguments
params:BookTickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::BookTickerResponse>> 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 diff_book_depth(
&self,
params: DiffBookDepthParams,
) -> Result<Arc<WebsocketStream<DiffBookDepthResponse>>>
pub async fn diff_book_depth( &self, params: DiffBookDepthParams, ) -> Result<Arc<WebsocketStream<DiffBookDepthResponse>>>
WebSocket Diff. Depth Stream
Order book price and quantity depth updates used to locally manage an order book.
§Arguments
params:DiffBookDepthParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::DiffBookDepthResponse>> 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(
&self,
params: KlineParams,
) -> Result<Arc<WebsocketStream<KlineResponse>>>
pub async fn kline( &self, params: KlineParams, ) -> Result<Arc<WebsocketStream<KlineResponse>>>
WebSocket Kline/Candlestick Streams for UTC
The Kline/Candlestick Stream push updates to the current klines/candlestick every second in UTC+0 timezone
§Arguments
params:KlineParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::KlineResponse>> 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_offset(
&self,
params: KlineOffsetParams,
) -> Result<Arc<WebsocketStream<KlineOffsetResponse>>>
pub async fn kline_offset( &self, params: KlineOffsetParams, ) -> Result<Arc<WebsocketStream<KlineOffsetResponse>>>
WebSocket Kline/Candlestick Streams with timezone offset
The Kline/Candlestick Stream push updates to the current klines/candlestick every second in UTC+8 timezone
§Arguments
params:KlineOffsetParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::KlineOffsetResponse>> 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(
&self,
params: MiniTickerParams,
) -> Result<Arc<WebsocketStream<MiniTickerResponse>>>
pub async fn mini_ticker( &self, params: MiniTickerParams, ) -> Result<Arc<WebsocketStream<MiniTickerResponse>>>
WebSocket Individual Symbol Mini Ticker Stream
24hr rolling window mini-ticker statistics. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
§Arguments
params:MiniTickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::MiniTickerResponse>> 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_book_depth(
&self,
params: PartialBookDepthParams,
) -> Result<Arc<WebsocketStream<PartialBookDepthResponse>>>
pub async fn partial_book_depth( &self, params: PartialBookDepthParams, ) -> Result<Arc<WebsocketStream<PartialBookDepthResponse>>>
WebSocket Partial Book Depth Streams
Top <levels> bids and asks, pushed every second. Valid <levels> are 5, 10, or 20.
§Arguments
params:PartialBookDepthParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::PartialBookDepthResponse>> 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 reference_price(
&self,
params: ReferencePriceParams,
) -> Result<Arc<WebsocketStream<ReferencePriceResponse>>>
pub async fn reference_price( &self, params: ReferencePriceParams, ) -> Result<Arc<WebsocketStream<ReferencePriceResponse>>>
WebSocket Reference Price Streams
§Arguments
params:ReferencePriceParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::ReferencePriceResponse>> 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 rolling_window_ticker(
&self,
params: RollingWindowTickerParams,
) -> Result<Arc<WebsocketStream<RollingWindowTickerResponse>>>
pub async fn rolling_window_ticker( &self, params: RollingWindowTickerParams, ) -> Result<Arc<WebsocketStream<RollingWindowTickerResponse>>>
WebSocket Individual Symbol Rolling Window Statistics Streams
Rolling window ticker statistics for a single symbol, computed over multiple windows.
§Arguments
params:RollingWindowTickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::RollingWindowTickerResponse>> 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(
&self,
params: TickerParams,
) -> Result<Arc<WebsocketStream<TickerResponse>>>
pub async fn ticker( &self, params: TickerParams, ) -> Result<Arc<WebsocketStream<TickerResponse>>>
WebSocket Individual Symbol Ticker Streams
24hr rolling window ticker statistics for a single symbol. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
§Arguments
params:TickerParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::TickerResponse>> 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(
&self,
params: TradeParams,
) -> Result<Arc<WebsocketStream<TradeResponse>>>
pub async fn trade( &self, params: TradeParams, ) -> Result<Arc<WebsocketStream<TradeResponse>>>
WebSocket Trade Streams
The Trade Streams push raw trade information; each trade has a unique buyer and seller.
§Arguments
params:TradeParamsThe parameters for this operation.
§Returns
Arc<WebsocketStream<models::TradeResponse>> 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.