Skip to main content

CoinApiWebSocketClient

Struct CoinApiWebSocketClient 

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

Authenticated, data-only CoinAPI WebSocket client using one connection.

CoinAPI tiers can limit an organization to one concurrent connection. Subscription changes send a hello that replaces the whole session scope; changes applied together share one hello, and the client sends at most one every two seconds, including across connection recreation. Because CoinAPI combines every listed data type, symbol, and period, the client drops frames outside the subscribed pairs; the provider still bills for them. A scope can list only one book type. Removing the last subscription closes the connection, and the next subscription opens a new one. Reconnects replay the scope and emit CoinApiWsMessage::Reconnected.

Implementations§

Source§

impl CoinApiWebSocketClient

Source

pub fn new( api_key: SecretString, config: CoinApiWebSocketConfig, ) -> Result<(Self, UnboundedReceiver<CoinApiWsMessage>), Error>

Creates a client and the receiver for its messages without connecting.

§Errors

Returns an error if the credentials, endpoint, or proxy URL are invalid.

Source

pub async fn subscribe(&self, subscription: Subscription) -> Result<(), Error>

Adds a subscription, connecting on first use.

Returns once the new scope is queued for sending; the first data frame confirms it.

§Errors

Returns an error if validation, connecting, or sending fails. A book type other than the one already subscribed is a validation error. A failed send leaves the pair unsubscribed.

Source

pub async fn unsubscribe(&self, subscription: Subscription) -> Result<(), Error>

Removes a subscription.

CoinAPI does not acknowledge the replacement scope, so frames still in flight for the pair are dropped locally.

§Errors

Returns an error if validation or sending fails. A failed send keeps the pair subscribed.

Source

pub async fn apply( &self, changes: &[ScopeChange], ) -> Result<Vec<(ScopeChange, Error)>, Error>

Applies scope changes in order and sends one hello for the resulting scope.

Connects when a subscribe finds no connection, and closes the connection when the scope becomes empty, since an empty hello is a terminal protocol error. The hello waits until two seconds after the client’s previous one. Returns once the scope is queued for sending, with each change rejected by validation, including a book type other than the one already subscribed.

§Errors

Returns an error if connecting or sending fails; every change in the batch is reverted.

Source

pub async fn resubscribe( &self, subscription: Subscription, paused: impl FnOnce(), ) -> Result<(), Error>

Restarts a subscribed book pair’s stream so CoinAPI sends it a fresh snapshot.

CoinAPI replays nothing for a repeated scope, but restarts a pair’s sequence with a snapshot when the pair leaves the scope and returns. The client sends the scope without the pair’s symbol, then the full scope two seconds later, so other symbols keep streaming. When no other symbol is subscribed, the removal scope keeps the symbol’s other channels instead, or lists only its trades when the book is the whole scope; the client drops those unsubscribed trades. The connection stays open, since a one-connection tier rejects a second one.

Restarts on a connection start at least five seconds apart, because CoinAPI closes a connection that exceeds its per-IP hello rate. Subscription changes wait while a restart is pending. Calls paused as soon as a scope without the pair’s symbol is queued, since that pauses the symbol’s other channels; the call happens even if the restart is then cancelled. Returns once both scopes are queued for sending.

§Errors

Returns an error if the pair is not a subscribed book pair or sending fails.

Source

pub async fn close(&self)

Closes the connection and forgets its subscriptions; repeated calls are safe.

Dropping the client also closes the connection.

Trait Implementations§

Source§

impl Debug for CoinApiWebSocketClient

Source§

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

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
§

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

§

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.

§

impl<T> Instrument for T

§

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

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

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
§

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

§

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
§

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
§

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

§

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
§

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
§

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

impl<T> Ungil for T
where T: Send,

§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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