Skip to main content

WebsocketStreams

Struct WebsocketStreams 

Source
pub struct WebsocketStreams { /* private fields */ }

Implementations§

Source§

impl WebsocketStreams

Source

pub fn subscribe_on_ws_events<F>(&self, callback: F) -> Subscription
where F: FnMut(WebsocketEvent) + Send + 'static,

Subscribes to WebSocket events with a provided callback function.

§Arguments
  • callback - A mutable function that takes a WebsocketEvent and is Send and '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 });

Source

pub fn unsubscribe_from_ws_events(&self, subscription: Subscription)

Unsubscribes from WebSocket events for a given Subscription.

§Arguments
  • subscription - The Subscription to unsubscribe from WebSocket events.
§Examples

let subscription = websocket_streams.subscribe_on_ws_events(|event| { // Handle WebSocket event }); websocket_streams.unsubscribe_from_ws_events(subscription);

Source

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?;

Source

pub async fn is_connected(&self) -> bool

Checks if the WebSocket connection is currently active.

§Returns

A bool indicating whether the WebSocket connection is established and connected.

§Examples

let is_active = websocket_streams.is_connected().await; if is_active { // WebSocket connection is active }

Source

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.

Source

pub fn subscribe(&self, streams: Vec<String>, id: Option<u32>)

Subscribes to specified WebSocket streams.

§Arguments
  • streams - A vector of stream names to subscribe to
  • id - 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.

Source

pub fn unsubscribe(&self, streams: Vec<String>, id: Option<u32>)

Unsubscribes from specified WebSocket streams.

§Arguments
  • streams - A vector of stream names to unsubscribe from
  • id - 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.

Source

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.

Source

pub async fn aggregate_trade_stream( &self, params: AggregateTradeStreamParams, ) -> Result<Arc<WebsocketStream<AggregateTradeStreamResponse>>>

Aggregate Trade Stream

Pushes aggregate trade updates for a symbol.

§Arguments
§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.

Source

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
§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.

Source

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
§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.

Source

pub async fn all_ticker_stream( &self, params: AllTickerStreamParams, ) -> Result<Arc<WebsocketStream<AllTickerStreamResponse>>>

All Ticker Stream

Pushes full ticker statistics for all symbols.

§Arguments
§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.

Source

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
§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.

Source

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
§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.

Source

pub async fn contract_kline_stream( &self, params: ContractKlineStreamParams, ) -> Result<Arc<WebsocketStream<ContractKlineStreamResponse>>>

Contract Kline Stream

Pushes kline updates by contractAddress@chainId.

§Arguments
§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.

Source

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
§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.

Source

pub async fn kline_stream( &self, params: KlineStreamParams, ) -> Result<Arc<WebsocketStream<KlineStreamResponse>>>

Kline Stream

Pushes kline updates for a symbol.

§Arguments
§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.

Source

pub async fn mini_ticker_stream( &self, params: MiniTickerStreamParams, ) -> Result<Arc<WebsocketStream<MiniTickerStreamResponse>>>

Mini Ticker Stream

Pushes 24h rolling mini ticker statistics.

§Arguments
§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.

Source

pub async fn partial_depth_stream( &self, params: PartialDepthStreamParams, ) -> Result<Arc<WebsocketStream<PartialDepthStreamResponse>>>

Partial Depth Stream

Pushes partial depth updates (UI orders only).

§Arguments
§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.

Source

pub async fn ticker_stream( &self, params: TickerStreamParams, ) -> Result<Arc<WebsocketStream<TickerStreamResponse>>>

Ticker Stream

Pushes full 24h rolling ticker statistics.

§Arguments
§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.

Source

pub async fn trade_stream( &self, params: TradeStreamParams, ) -> Result<Arc<WebsocketStream<TradeStreamResponse>>>

Trade Stream

Pushes raw trade updates for a symbol.

§Arguments
§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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more