Skip to main content

WsClient

Struct WsClient 

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

WebSocket client for Hypercall.

Implementations§

Source§

impl WsClient

Source

pub fn new() -> Self

Create a new WebSocket client (not yet connected).

Source

pub fn with_config(config: WsClientConfig) -> Self

Create a WebSocket client with an explicit reconnection policy.

Source

pub fn with_permessage_deflate(self, enabled: bool) -> Self

Choose whether to offer RFC 7692 permessage-deflate when connecting.

Enabled by default. The server decides whether to accept, and the client works either way: compressed frames are inflated transparently, and the JSON messages are identical. Disable it to save the CPU spent inflating, or to rule compression out while debugging. The setting applies to the initial connection and every reconnect.

Source

pub fn with_batched_indicative(self, enabled: bool) -> Self

Choose whether WsClient::subscribe_indicative_market_data uses the batched indicative_market_data_v2 channel.

Enabled by default: fewer frames, rows up to one short batch window later, and much better compression. Disable it to receive one IndicativeMarketData frame per row from indicative_market_data.

Source

pub fn indicative_batched(&self) -> bool

Whether indicative subscriptions are currently delivered in batches: batching is enabled and no server has shown it lacks the v2 channel.

Source

pub async fn connect( &self, base_url: &str, wallet: Option<&AccountAddress>, ) -> Result<()>

Connect to the WebSocket endpoint.

If a wallet is provided, sends an Authenticate message after connecting and waits for the Authenticated confirmation before returning. After the initial connection succeeds, the client reconnects, reauthenticates, and restores desired subscriptions until WsClient::disconnect is called or the client is dropped.

Source

pub async fn connect_rfq_v2( &self, base_url: &str, grant: &RfqV2ReadGrant, ) -> Result<()>

Connect using a signed private RFQ read grant. The grant is sent again before subscriptions on reconnect. Once it expires, sign a fresh grant and create a new session; no unsigned wallet identity grants RFQ access.

Source

pub async fn ping(&self) -> Result<()>

Send an empty WebSocket ping frame.

Source

pub async fn ping_with_payload(&self, payload: impl Into<Vec<u8>>) -> Result<()>

Send a WebSocket ping frame with a payload.

WebSocket control frame payloads are limited to 125 bytes by the protocol.

Source

pub async fn pong_with_payload(&self, payload: impl Into<Vec<u8>>) -> Result<()>

Send a WebSocket pong frame with a payload.

This is rarely needed directly because WsClient automatically responds to server ping frames.

Source

pub async fn subscribe(&self, channels: Vec<&str>) -> Result<()>

Subscribe to channels.

Source

pub async fn subscribe_rfq_v2(&self) -> Result<()>

Send a subscription request for private RFQ V2 change hints. Ok means the frame was sent; observe Subscribed or Error for server acceptance. Use a periodic signed change-feed read to recover hints lost while a socket stays connected.

Source

pub async fn unsubscribe_rfq_v2(&self) -> Result<()>

Remove the RFQ V2 subscription, including its reconnect intent.

Source

pub async fn subscribe_with_options( &self, channel: &str, symbols: Option<Vec<String>>, expiry: Option<String>, option_type: Option<String>, ) -> Result<()>

Subscribe to a channel and retain its filters across reconnects.

Source

pub async fn subscribe_indicative_market_data( &self, symbols: Option<Vec<String>>, ) -> Result<()>

Subscribe to indicative market data and retain the subscription across reconnects.

By default this uses indicative_market_data_v2, whose IndicativeMarketDataBatch frames carry the rows of one short batch window. If the server confirms v2 without "batch": true (it predates the channel), the client switches to indicative_market_data, one IndicativeMarketData frame per row. Read rows with crate::indicative::indicative_rows_from_value, which handles both.

The server keeps one symbol filter per channel per connection: every Subscribe adds symbols to it, and a filtered subscribe narrows an unfiltered one. If you also subscribe to indicative_market_data directly with a different filter, a fallback onto v1 shares that one server filter with your direct subscription. Use one or the other per connection when exact filters matter.

Source

pub async fn unsubscribe_indicative_market_data(&self) -> Result<()>

Stop the subscriptions made by WsClient::subscribe_indicative_market_data, on whichever channel each resolved to after a fallback, and stop restoring them after reconnects. Subscriptions made with WsClient::subscribe_with_options on either indicative channel are left alone.

Source

pub async fn unsubscribe(&self, channels: Vec<&str>) -> Result<()>

Unsubscribe from channels and stop restoring them after reconnects.

Channels match exactly, and only subscriptions made directly (for example with WsClient::subscribe_with_options) are released. To stop a subscription made by WsClient::subscribe_indicative_market_data, which may have moved from indicative_market_data_v2 to indicative_market_data, use WsClient::unsubscribe_indicative_market_data. If both own the same channel and filter, the server subscription ends when both release it.

Source

pub async fn disconnect(&self)

Stop reconnecting and close the current socket.

Source

pub fn connection_state(&self) -> WsConnectionState

Return the current connection and recovery state.

Source

pub fn connection_state_receiver(&self) -> Receiver<WsConnectionState>

Watch connection and recovery state transitions.

Source

pub fn last_disconnect_reason(&self) -> Option<WsDisconnectReason>

Return the most recent established-session disconnect reason.

Source

pub fn mark_recovered(&self) -> Result<()>

Mark application-owned snapshot or REST recovery as complete.

Source

pub async fn get_pongs(&self) -> Vec<Vec<u8>>

Get all received WebSocket pong payloads.

Source

pub async fn clear_pongs(&self)

Clear all received WebSocket pong payloads.

Source

pub async fn wait_for_pong<F>( &self, check: F, timeout_ms: u64, ) -> Option<Vec<u8>>
where F: Fn(&[u8]) -> bool,

Wait for a pong payload matching a predicate.

Source

pub async fn get_messages(&self) -> Vec<Value>

Get all received messages.

Source

pub async fn clear_messages(&self)

Clear all received messages.

Source

pub async fn wait_for_message<F>( &self, check: F, timeout_ms: u64, ) -> Option<Value>
where F: Fn(&Value) -> bool,

Wait for a message matching a predicate.

Source

pub async fn count_messages<F>(&self, check: F) -> usize
where F: Fn(&Value) -> bool,

Count messages matching a predicate.

Trait Implementations§

Source§

impl Default for WsClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for WsClient

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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