Skip to main content

ManagedWebsocket

Struct ManagedWebsocket 

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

Auto-reconnecting WebSocket handle.

Send + Sync — safe to share across async tasks without a Mutex.

Subscribe/unsubscribe/order sends are fire-and-forget: the call queues a command to the background task and returns. Server acknowledgements arrive as WsEvent::Message on the event stream, matching standard CEX WS conventions.

Dropping the handle (or calling stop) terminates the background task immediately — even if it is mid-reconnect — via a separate cancellation signal that bypasses the command queue.

Implementations§

Source§

impl ManagedWebsocket

Source

pub async fn connect(client: &Client) -> Result<ManagedWebsocket, WSErrors>

Open a managed (auto-reconnecting) WebSocket connection for the given Client.

The returned handle manages reconnection with exponential backoff and replays subscriptions automatically.

§Example
let mut ws = ManagedWebsocket::connect(&client).call().await?;
ws.subscribe([Topic::agg_trade("BTC-USD")], None)?;

Uses tokio::spawn on native targets.

Source

pub async fn connect_with( client: &Client, config: ManagedWsConfig, ) -> Result<ManagedWebsocket, WSErrors>

Like connect but takes an explicit ManagedWsConfig.

Source

pub async fn recv(&mut self) -> Option<WsEvent>

Receive the next event from the WebSocket.

Returns None when the background task has stopped (after permanent disconnection or stop).

Source

pub fn subscribe( &self, topics: impl IntoIterator<Item = Topic>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Subscribe to topics. The subscription is tracked and replayed on reconnect.

This is fire-and-forget — it queues the command to the background task. The server’s subscribe acknowledgement arrives as a WsEvent::Message.

Source

pub fn subscribe_raw( &self, topics: impl IntoIterator<Item = String>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Subscribe using pre-serialized topic strings (e.g. "BTC-USD@aggTrade").

Prefer subscribe with typed Topic values from native Rust — this overload exists for binding layers (WASM/JS) that already hold string topics.

Source

pub fn unsubscribe( &self, topics: impl IntoIterator<Item = Topic>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Unsubscribe from topics. Removes them from the replay list.

Source

pub fn unsubscribe_raw( &self, topics: impl IntoIterator<Item = String>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Raw-string counterpart of unsubscribe.

Source

pub fn order_place( &self, tx: impl Into<String>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Place an order via WebSocket.

Source

pub fn order_cancel( &self, tx: impl Into<String>, id: Option<RequestId>, ) -> Result<(), ManagedWsError>

Cancel an order via WebSocket.

Source

pub fn place_order( &self, signed: &Transaction, id: Option<RequestId>, ) -> Result<(), WSErrors>

Place an order using a signed Transaction. Base64-encodes internally.

Returns a SDKResult-style error instead of ManagedWsError because encoding can fail independently of the channel state.

Source

pub fn cancel_order( &self, signed: &Transaction, id: Option<RequestId>, ) -> Result<(), WSErrors>

Cancel an order using a signed Transaction. Base64-encodes internally.

Source

pub fn stop(self)

Stop the managed WebSocket and its background task.

After this returns the background task has been signaled; it will terminate at its next await point without draining pending commands. The event stream will end (recv() returns None) shortly after.

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