Skip to main content

WebSocketClient

Struct WebSocketClient 

Source
pub struct WebSocketClient { /* private fields */ }
Expand description

WebSocket client for connecting to Binance streams.

Implementations§

Source§

impl WebSocketClient

Source

pub fn endpoint(&self) -> &str

Get the WebSocket endpoint URL.

Source

pub async fn connect(&self, stream: &str) -> Result<WebSocketConnection>

Connect to a single stream.

§Arguments
  • stream - Stream name (e.g., “btcusdt@aggTrade”)
§Example
let ws = client.websocket();
let stream = ws.agg_trade_stream("btcusdt");
let mut conn = ws.connect(&stream).await?;
Source

pub async fn connect_combined( &self, streams: &[String], ) -> Result<WebSocketConnection>

Connect to multiple streams (combined stream).

§Arguments
  • streams - List of stream names
§Example
let ws = client.websocket();
let streams = vec![
    ws.agg_trade_stream("btcusdt"),
    ws.agg_trade_stream("ethusdt"),
];
let mut conn = ws.connect_combined(&streams).await?;
Source

pub async fn connect_user_stream( &self, listen_key: &str, ) -> Result<WebSocketConnection>

Connect to a user data stream.

§Arguments
  • listen_key - Listen key obtained from user_stream().start()
§Example
let listen_key = client.user_stream().start().await?;
let mut conn = client.websocket().connect_user_stream(&listen_key).await?;
Source

pub async fn connect_with_reconnect( &self, stream: &str, ) -> Result<ReconnectingWebSocket>

Connect with auto-reconnection support.

Returns a ReconnectingWebSocket that automatically reconnects on disconnection with exponential backoff.

§Arguments
  • stream - Stream name (e.g., “btcusdt@aggTrade”)
§Example
let ws = client.websocket();
let stream = ws.agg_trade_stream("btcusdt");
let mut conn = ws.connect_with_reconnect(&stream).await?;

// Connection will auto-reconnect on failure
while let Some(event) = conn.next().await {
    println!("{:?}", event?);
}
Source

pub async fn connect_combined_with_reconnect( &self, streams: &[String], ) -> Result<ReconnectingWebSocket>

Connect to combined streams with auto-reconnection support.

Source

pub fn agg_trade_stream(&self, symbol: &str) -> String

Get the aggregate trade stream name for a symbol.

Stream: <symbol>@aggTrade

Source

pub fn trade_stream(&self, symbol: &str) -> String

Get the trade stream name for a symbol.

Stream: <symbol>@trade

Source

pub fn kline_stream(&self, symbol: &str, interval: KlineInterval) -> String

Get the kline/candlestick stream name for a symbol.

Stream: <symbol>@kline_<interval>

Source

pub fn mini_ticker_stream(&self, symbol: &str) -> String

Get the mini ticker stream name for a symbol.

Stream: <symbol>@miniTicker

Source

pub fn all_mini_ticker_stream(&self) -> String

Get the mini ticker stream for all symbols.

Stream: !miniTicker@arr

Source

pub fn ticker_stream(&self, symbol: &str) -> String

Get the 24hr ticker stream name for a symbol.

Stream: <symbol>@ticker

Source

pub fn all_ticker_stream(&self) -> String

Get the 24hr ticker stream for all symbols.

Stream: !ticker@arr

Source

pub fn book_ticker_stream(&self, symbol: &str) -> String

Get the book ticker stream name for a symbol.

Stream: <symbol>@bookTicker

Source

pub fn all_book_ticker_stream(&self) -> String

Get the book ticker stream for all symbols.

Stream: !bookTicker

Source

pub fn partial_depth_stream( &self, symbol: &str, levels: u8, fast: bool, ) -> String

Get the partial book depth stream name.

Stream: <symbol>@depth<levels> or <symbol>@depth<levels>@100ms

§Arguments
  • symbol - Trading pair symbol
  • levels - Depth levels (5, 10, or 20)
  • fast - If true, use 100ms update speed instead of 1000ms
Source

pub fn diff_depth_stream(&self, symbol: &str, fast: bool) -> String

Get the diff depth stream name.

Stream: <symbol>@depth or <symbol>@depth@100ms

§Arguments
  • symbol - Trading pair symbol
  • fast - If true, use 100ms update speed instead of 1000ms

Trait Implementations§

Source§

impl Clone for WebSocketClient

Source§

fn clone(&self) -> WebSocketClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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: 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: 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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