Skip to main content

PolymarketWebSocketClient

Struct PolymarketWebSocketClient 

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

Provides a WebSocket client for the Polymarket CLOB API.

A single instance targets one channel (market or user). Use PolymarketWebSocketClient::new_market for public market data and PolymarketWebSocketClient::new_user for authenticated order/trade streams.

Implementations§

Source§

impl PolymarketWebSocketClient

Source

pub fn new_market( base_url: Option<String>, subscribe_new_markets: bool, transport_backend: TransportBackend, ) -> Self

Creates a new market-channel client (unauthenticated).

If base_url is None, the default production URL is used.

Source

pub fn new_user( base_url: Option<String>, credential: Credential, transport_backend: TransportBackend, ) -> Self

Creates a new user-channel client (authenticated).

If base_url is None, the default production URL is used.

Source

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

Establishes the WebSocket connection and spawns the message handler.

§Errors
Source

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

Disconnects the WebSocket connection.

Source

pub fn is_active(&self) -> bool

Returns true if the WebSocket is actively connected.

Source

pub fn url(&self) -> &str

Returns the URL this client connects to.

Source

pub fn subscription_count(&self) -> usize

Returns the number of active market asset subscriptions (pending + confirmed).

Source

pub fn is_authenticated(&self) -> bool

Returns true if the user channel has been authenticated.

Source

pub async fn subscribe_market(&self, asset_ids: Vec<String>) -> Result<()>

Subscribe to market data for the given asset IDs.

Sends a subscribe message immediately if connected; the IDs are also retained so they are re-sent automatically on reconnect.

§Errors

Returns an error if called on a user-channel client (incompatible channel).

Source

pub async fn unsubscribe_market(&self, asset_ids: Vec<String>) -> Result<()>

Remove asset IDs from the active subscription set.

The IDs are dropped from the reconnect set so they will not be re-subscribed after a reconnect. No wire message is sent.

§Errors

Returns an error if called on a user-channel client (incompatible channel).

Source

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

Authenticate and subscribe to the user channel.

§Errors

Returns an error if called on a market-channel client (no credentials available).

Source

pub fn clone_subscription_handle(&self) -> WsSubscriptionHandle

Returns a cloneable subscription handle for use in spawned tasks.

Source

pub fn take_message_receiver( &mut self, ) -> Option<UnboundedReceiver<PolymarketWsMessage>>

Takes the message receiver, leaving None in its place.

This is useful when the data client needs to spawn its own handler task that reads messages independently of the WS client. Subscription methods (subscribe_market, etc.) remain usable on &self.

Source

pub async fn next_message(&mut self) -> Option<PolymarketWsMessage>

Receives the next message from the WebSocket handler.

Returns None when the handler has disconnected or the receiver was not yet initialized (call connect first).

Trait Implementations§

Source§

impl Debug for PolymarketWebSocketClient

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

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
§

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