Skip to main content

WsClient

Struct WsClient 

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

Async WebSocket client for exchange streaming APIs.

§Backpressure Handling

This client uses bounded channels to prevent memory exhaustion in high-frequency trading scenarios. When the message channel is full, the configured BackpressureStrategy determines how to handle new messages:

  • DropOldest: Removes the oldest message to make room (default)
  • DropNewest: Discards the incoming message
  • Block: Waits until space is available (may stall the read loop)

Implementations§

Source§

impl WsClient

Source

pub fn new(config: WsConfig) -> WsClient

Creates a new WebSocket client instance.

The client uses bounded channels for message passing to prevent memory exhaustion. Channel capacities are configured via WsConfig.

Source

pub async fn set_event_callback( &self, callback: Arc<dyn Fn(WsEvent) + Send + Sync>, )

Sets the event callback for connection lifecycle events.

Source

pub async fn clear_event_callback(&self)

Clears the event callback.

Source

pub async fn set_cancel_token(&self, token: CancellationToken)

Sets the cancellation token for this client.

Source

pub async fn clear_cancel_token(&self)

Clears the cancellation token.

Source

pub async fn get_cancel_token(&self) -> Option<CancellationToken>

Returns a clone of the current cancellation token, if set.

Source

pub async fn connect(&self) -> Result<(), Error>

Establishes connection to the WebSocket server.

Source

pub async fn connect_with_cancel( &self, cancel_token: Option<CancellationToken>, ) -> Result<(), Error>

Establishes connection with cancellation support.

Source

pub async fn disconnect(&self) -> Result<(), Error>

Closes the WebSocket connection gracefully.

Source

pub async fn shutdown(&self)

Gracefully shuts down the WebSocket client.

Source

pub async fn reconnect(&self) -> Result<(), Error>

Attempts to reconnect to the WebSocket server.

Source

pub async fn reconnect_with_cancel( &self, cancel_token: Option<CancellationToken>, ) -> Result<(), Error>

Attempts to reconnect with cancellation support.

Source

pub fn reconnect_count(&self) -> u32

Returns the current reconnection attempt count.

Source

pub fn reset_reconnect_count(&self)

Resets the reconnection attempt counter.

Source

pub fn stats(&self) -> WsStatsSnapshot

Returns a snapshot of connection statistics.

Source

pub fn reset_stats(&self)

Resets all connection statistics.

Source

pub fn latency(&self) -> Option<i64>

Calculates current connection latency in milliseconds.

Source

pub fn dropped_messages(&self) -> u32

Returns the number of messages dropped due to backpressure.

This counter is incremented when the message channel is full and messages are dropped according to the configured backpressure strategy.

Source

pub fn reset_dropped_messages(&self)

Resets the dropped messages counter.

Source

pub fn create_auto_reconnect_coordinator( self: Arc<WsClient>, ) -> AutoReconnectCoordinator

Creates an automatic reconnection coordinator.

Source

pub async fn subscribe( &self, channel: String, symbol: Option<String>, params: Option<HashMap<String, Value>>, ) -> Result<(), Error>

Subscribes to a WebSocket channel.

Source

pub async fn unsubscribe( &self, channel: String, symbol: Option<String>, ) -> Result<(), Error>

Unsubscribes from a WebSocket channel.

Source

pub async fn receive(&self) -> Option<Value>

Receives the next available message.

Source

pub fn state(&self) -> WsConnectionState

Returns the current connection state.

Source

pub fn config(&self) -> &WsConfig

Returns a reference to the WebSocket configuration.

Source

pub fn set_state(&self, state: WsConnectionState)

Sets the connection state.

Source

pub fn is_connected(&self) -> bool

Checks whether the WebSocket is currently connected.

Source

pub fn is_subscribed(&self, channel: &str, symbol: Option<&String>) -> bool

Checks if subscribed to a specific channel.

Source

pub fn subscription_count(&self) -> usize

Returns the number of active subscriptions.

Source

pub fn remaining_capacity(&self) -> usize

Returns the remaining capacity for new subscriptions.

Source

pub fn subscriptions(&self) -> Vec<String>

Returns a list of all active subscription channel names.

Each subscription is identified by its channel name, optionally combined with a symbol in the format “channel:symbol” or just “channel”.

Source

pub async fn send(&self, message: Message) -> Result<(), Error>

Sends a raw WebSocket message.

This method uses a bounded channel for sending. If the write channel is full, it will wait until space is available.

Source

pub fn try_send(&self, message: Message) -> Result<(), Error>

Tries to send a raw WebSocket message without blocking.

Returns an error if the channel is full or closed.

Source

pub async fn send_text(&self, text: String) -> Result<(), Error>

Sends a text message.

Source

pub async fn send_json(&self, json: &Value) -> Result<(), Error>

Sends a JSON-encoded message.

Trait Implementations§

Source§

impl Debug for WsClient

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. 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> 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, 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