pub struct BitmexWebSocketClient { /* private fields */ }Expand description
Provides a WebSocket client for connecting to the BitMEX real-time API.
Key runtime patterns:
- Authentication handshakes are managed by the internal auth tracker, ensuring resubscriptions
occur only after BitMEX acknowledges
authKeymessages. - The subscription state maintains pending and confirmed topics so reconnection replay is deterministic and per-topic errors are surfaced.
Implementations§
Source§impl BitmexWebSocketClient
impl BitmexWebSocketClient
Sourcepub fn new(
url: Option<String>,
api_key: Option<String>,
api_secret: Option<String>,
account_id: Option<AccountId>,
heartbeat: u64,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Result<Self>
pub fn new( url: Option<String>, api_key: Option<String>, api_secret: Option<String>, account_id: Option<AccountId>, heartbeat: u64, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Result<Self>
Creates a new BitmexWebSocketClient instance.
§Errors
Returns an error if only one of api_key or api_secret is provided (both or neither required).
Sourcepub fn new_with_env(
url: Option<String>,
api_key: Option<String>,
api_secret: Option<String>,
account_id: Option<AccountId>,
heartbeat: u64,
environment: BitmexEnvironment,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Result<Self>
pub fn new_with_env( url: Option<String>, api_key: Option<String>, api_secret: Option<String>, account_id: Option<AccountId>, heartbeat: u64, environment: BitmexEnvironment, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Result<Self>
Creates a new BitmexWebSocketClient with environment variable credential resolution.
If api_key or api_secret are not provided, they will be loaded from
environment variables based on the environment:
- Testnet:
BITMEX_TESTNET_API_KEY,BITMEX_TESTNET_API_SECRET - Mainnet:
BITMEX_API_KEY,BITMEX_API_SECRET
§Errors
Returns an error if only one of api_key or api_secret is provided.
Sourcepub fn from_env() -> Result<Self>
pub fn from_env() -> Result<Self>
Creates a new authenticated BitmexWebSocketClient using environment variables.
§Errors
Returns an error if environment variables are not set or credentials are invalid.
Sourcepub fn api_key_masked(&self) -> Option<String>
pub fn api_key_masked(&self) -> Option<String>
Returns a masked version of the API key for logging purposes.
Sourcepub fn account_id(&self) -> AccountId
pub fn account_id(&self) -> AccountId
Returns the account ID.
Sourcepub fn set_account_id(&mut self, account_id: AccountId)
pub fn set_account_id(&mut self, account_id: AccountId)
Sets the account ID.
Sourcepub fn cache_instruments(&self, instruments: &[InstrumentAny])
pub fn cache_instruments(&self, instruments: &[InstrumentAny])
Bulk-replaces the instrument cache with the given instruments.
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Upserts a single instrument into the cache.
Sourcepub fn get_instrument(&self, symbol: &Ustr) -> Option<InstrumentAny>
pub fn get_instrument(&self, symbol: &Ustr) -> Option<InstrumentAny>
Retrieves an instrument from the cache by symbol.
Sourcepub async fn connect(&mut self) -> Result<(), BitmexWsError>
pub async fn connect(&mut self) -> Result<(), BitmexWsError>
Connect to the BitMEX WebSocket server.
§Errors
Returns an error if the WebSocket connection fails or authentication fails (if credentials provided).
Sourcepub async fn wait_until_active(
&self,
timeout_secs: f64,
) -> Result<(), BitmexWsError>
pub async fn wait_until_active( &self, timeout_secs: f64, ) -> Result<(), BitmexWsError>
Wait until the WebSocket connection is active.
§Errors
Returns an error if the connection times out.
Sourcepub fn stream(&mut self) -> impl Stream<Item = BitmexWsMessage> + use<>
pub fn stream(&mut self) -> impl Stream<Item = BitmexWsMessage> + use<>
Provides the internal stream as a channel-based stream.
§Panics
This function panics:
- If the websocket is not connected.
- If
streamhas already been called somewhere else (stream receiver is then taken).
Sourcepub async fn close(&mut self) -> Result<(), BitmexWsError>
pub async fn close(&mut self) -> Result<(), BitmexWsError>
Sourcepub async fn subscribe(&self, topics: Vec<String>) -> Result<(), BitmexWsError>
pub async fn subscribe(&self, topics: Vec<String>) -> Result<(), BitmexWsError>
Subscribe to the specified topics.
§Errors
Returns an error if the WebSocket is not connected or if sending the subscription message fails.
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Get the current number of active subscriptions.
pub fn get_subscriptions(&self, instrument_id: InstrumentId) -> Vec<String>
Sourcepub async fn subscribe_instruments(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_instruments(&self) -> Result<(), BitmexWsError>
Subscribe to instrument updates for all instruments on the venue.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_instrument(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_instrument( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to instrument updates (mark/index prices) for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_book(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_book( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to order book updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_book_25(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_book_25( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to order book L2 (25 levels) updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_book_depth10(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_book_depth10( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to order book depth 10 updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_quotes( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to quote updates for the specified instrument.
Note: Index symbols (starting with ‘.’) do not have quotes and will be silently ignored.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_trades(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_trades( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to trade updates for the specified instrument.
Note: Index symbols (starting with ‘.’) do not have trades and will be silently ignored.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_mark_prices(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_mark_prices( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to mark price updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_index_prices(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_index_prices( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to index price updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_funding_rates(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn subscribe_funding_rates( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Subscribe to funding rate updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn subscribe_bars(
&self,
bar_type: BarType,
) -> Result<(), BitmexWsError>
pub async fn subscribe_bars( &self, bar_type: BarType, ) -> Result<(), BitmexWsError>
Subscribe to bar updates for the specified bar type.
§Errors
Returns an error if the WebSocket is not connected or if the subscription fails.
Sourcepub async fn unsubscribe_instruments(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_instruments(&self) -> Result<(), BitmexWsError>
Unsubscribe from instrument updates for all instruments on the venue.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_instrument(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_instrument( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from instrument updates (mark/index prices) for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_book(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_book( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from order book updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_book_25(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_book_25( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from order book L2 (25 levels) updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_book_depth10(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_book_depth10( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from order book depth 10 updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_quotes( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from quote updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from trade updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_mark_prices(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_mark_prices( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from mark price updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_index_prices(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_index_prices( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from index price updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_funding_rates(
&self,
instrument_id: InstrumentId,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_funding_rates( &self, instrument_id: InstrumentId, ) -> Result<(), BitmexWsError>
Unsubscribe from funding rate updates for the specified instrument.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_bars(
&self,
bar_type: BarType,
) -> Result<(), BitmexWsError>
pub async fn unsubscribe_bars( &self, bar_type: BarType, ) -> Result<(), BitmexWsError>
Unsubscribe from bar updates for the specified bar type.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn subscribe_orders(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_orders(&self) -> Result<(), BitmexWsError>
Subscribe to order updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected, not authenticated, or if the subscription fails.
Sourcepub async fn subscribe_executions(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_executions(&self) -> Result<(), BitmexWsError>
Subscribe to execution updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected, not authenticated, or if the subscription fails.
Sourcepub async fn subscribe_positions(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_positions(&self) -> Result<(), BitmexWsError>
Subscribe to position updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected, not authenticated, or if the subscription fails.
Sourcepub async fn subscribe_margin(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_margin(&self) -> Result<(), BitmexWsError>
Subscribe to margin updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected, not authenticated, or if the subscription fails.
Sourcepub async fn subscribe_wallet(&self) -> Result<(), BitmexWsError>
pub async fn subscribe_wallet(&self) -> Result<(), BitmexWsError>
Subscribe to wallet updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected, not authenticated, or if the subscription fails.
Sourcepub async fn unsubscribe_orders(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_orders(&self) -> Result<(), BitmexWsError>
Unsubscribe from order updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_executions(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_executions(&self) -> Result<(), BitmexWsError>
Unsubscribe from execution updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_positions(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_positions(&self) -> Result<(), BitmexWsError>
Unsubscribe from position updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_margin(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_margin(&self) -> Result<(), BitmexWsError>
Unsubscribe from margin updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Sourcepub async fn unsubscribe_wallet(&self) -> Result<(), BitmexWsError>
pub async fn unsubscribe_wallet(&self) -> Result<(), BitmexWsError>
Unsubscribe from wallet updates for the authenticated account.
§Errors
Returns an error if the WebSocket is not connected or if the unsubscription fails.
Trait Implementations§
Source§impl Clone for BitmexWebSocketClient
impl Clone for BitmexWebSocketClient
Source§fn clone(&self) -> BitmexWebSocketClient
fn clone(&self) -> BitmexWebSocketClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for BitmexWebSocketClient
impl !RefUnwindSafe for BitmexWebSocketClient
impl Send for BitmexWebSocketClient
impl Sync for BitmexWebSocketClient
impl Unpin for BitmexWebSocketClient
impl UnsafeUnpin for BitmexWebSocketClient
impl !UnwindSafe for BitmexWebSocketClient
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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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