pub struct UserWsClient { /* private fields */ }Expand description
WebSocket client for streaming authenticated user events
This client connects to the Polymarket CLOB user WebSocket endpoint and streams real-time updates about the user’s trades and orders.
§Connection Management
The Polymarket WebSocket server will disconnect idle connections after 1-2 minutes.
For production use, it’s recommended to use ReconnectingStream
to automatically handle disconnections and reconnect with exponential backoff.
§Example with Auto-Reconnect
use polymarket_rs::websocket::{UserWsClient, ReconnectConfig, ReconnectingStream};
use polymarket_rs::types::ApiCreds;
use futures_util::StreamExt;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let creds = ApiCreds::new(
"api_key".to_string(),
"api_secret".to_string(),
"api_passphrase".to_string(),
);
let client = UserWsClient::new();
let config = ReconnectConfig {
initial_delay: Duration::from_secs(1),
max_delay: Duration::from_secs(30),
multiplier: 2.0,
max_attempts: None,
};
let creds_clone = creds.clone();
let mut stream = ReconnectingStream::new(config, move || {
let client = client.clone();
let creds = creds_clone.clone();
async move { client.subscribe_with_creds(&creds).await }
});
while let Some(event) = stream.next().await {
match event {
Ok(evt) => println!("Event: {:?}", evt),
Err(_) => continue, // Will auto-reconnect
}
}
Ok(())
}Implementations§
Source§impl UserWsClient
impl UserWsClient
Sourcepub fn with_url(ws_url: impl Into<String>) -> Self
pub fn with_url(ws_url: impl Into<String>) -> Self
Create a new user WebSocket client with a custom endpoint
Sourcepub async fn subscribe_with_creds(
&self,
creds: &ApiCreds,
) -> Result<Pin<Box<dyn Stream<Item = Result<UserWsEvent>> + Send>>>
pub async fn subscribe_with_creds( &self, creds: &ApiCreds, ) -> Result<Pin<Box<dyn Stream<Item = Result<UserWsEvent>> + Send>>>
Subscribe to user events with API credentials
Returns a stream of UserWsEvent items. The stream will yield events as they
are received from the WebSocket connection.
§Arguments
creds- API credentials (api_key, secret, passphrase)
§Events
The stream will yield two types of events:
UserWsEvent::Trade: Trade execution updatesUserWsEvent::Order: Order status updates
§Errors
Returns an error if:
- The WebSocket connection fails
- The authentication message cannot be sent
- Authentication fails (server will close the connection)
§Example
let creds = ApiCreds::new(
"your_api_key".to_string(),
"your_api_secret".to_string(),
"your_api_passphrase".to_string(),
);
let client = UserWsClient::new();
let mut stream = client.subscribe_with_creds(&creds).await?;
while let Some(event) = stream.next().await {
println!("Event: {:?}", event?);
}Sourcepub async fn subscribe(
&self,
api_key: String,
api_secret: String,
api_passphrase: String,
) -> Result<Pin<Box<dyn Stream<Item = Result<UserWsEvent>> + Send>>>
pub async fn subscribe( &self, api_key: String, api_secret: String, api_passphrase: String, ) -> Result<Pin<Box<dyn Stream<Item = Result<UserWsEvent>> + Send>>>
Subscribe to user events with authentication
Returns a stream of UserWsEvent items. The stream will yield events as they
are received from the WebSocket connection.
§Arguments
api_key- API key for authenticationapi_secret- API secret for authenticationapi_passphrase- API passphrase for authentication
§Events
The stream will yield two types of events:
UserWsEvent::Trade: Trade execution updatesUserWsEvent::Order: Order status updates
§Errors
Returns an error if:
- The WebSocket connection fails
- The authentication message cannot be sent
- Authentication fails (server will close the connection)
Trait Implementations§
Source§impl Clone for UserWsClient
impl Clone for UserWsClient
Source§fn clone(&self) -> UserWsClient
fn clone(&self) -> UserWsClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UserWsClient
impl Debug for UserWsClient
Auto Trait Implementations§
impl Freeze for UserWsClient
impl RefUnwindSafe for UserWsClient
impl Send for UserWsClient
impl Sync for UserWsClient
impl Unpin for UserWsClient
impl UnwindSafe for UserWsClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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