Skip to main content

Overview

The Rust SDK provides a Socket.IO-based WebSocketClient for:
  • orderbook and trade streams
  • market and price updates
  • authenticated order, fill, position, and transaction streams
  • automatic reconnect with subscription replay

Setup

Create a WebSocket client from the root SDK client:
use limitless_exchange_rust_sdk::Client;

let client = Client::new()?;
let ws = client.new_websocket_client(None);
Or configure it directly:
use std::sync::Arc;

use limitless_exchange_rust_sdk::{ConsoleLogger, LogLevel, WebSocketClient, WebSocketConfig};

let ws = WebSocketClient::new(Some(WebSocketConfig {
    logger: Some(Arc::new(ConsoleLogger::new(LogLevel::Info))),
    ..Default::default()
}));

Connect and Disconnect

ws.connect().await?;
println!("{:?}", ws.state());
println!("{}", ws.is_connected());

ws.disconnect().await?;

WebSocketConfig

FieldTypeDefaultDescription
urlStringwss://ws.limitless.exchangeWebSocket URL
api_keyOption<String>Reads LIMITLESS_API_KEY envAPI key for authenticated channels
hmac_credentialsOption<HmacCredentials>NoneHMAC credentials for authenticated channels
auto_reconnectbooltrueReconnect automatically after disconnects
reconnect_delay_msu641000Base reconnect delay
max_reconnect_attemptsu320Max reconnect attempts (0 means unlimited)
timeout_msu6410000Connect timeout
loggerOption<SharedLogger>NoneOptional logger

Event Handlers

Use on() for raw JSON handlers, once() for one-shot handlers, and off() to unregister:
let id = ws.on("orderbookUpdate", |data| {
    println!("{}", data);
});

let once_id = ws.once("trade", |data| {
    println!("first trade {}", data);
});

ws.off("trade", &[once_id]);
ws.off("orderbookUpdate", &[id]);

Typed handlers

The SDK also provides typed event handlers:
ws.on_orderbook_update(|update| {
    println!(
        "{} bids={} asks={}",
        update.market_slug,
        update.orderbook.bids.len(),
        update.orderbook.asks.len()
    );
});

ws.on_trade(|trade| {
    println!("{} {} {} @ {}", trade.market_slug, trade.side, trade.size, trade.price);
});

ws.on_order(|order| {
    println!("{} {}", order.order_id, order.status);
});

ws.on_fill(|fill| {
    println!("{} {}", fill.fill_id, fill.order_id);
});

ws.on_transaction(|tx| {
    println!("{:?} {}", tx.tx_hash, tx.status);
});

ws.on_market(|market| {
    println!("{} {:?}", market.market_slug, market.last_price);
});

ws.on_market_created(|event| {
    println!("created {} {}", event.slug, event.title);
});

ws.on_market_resolved(|event| {
    println!("resolved {} {}", event.slug, event.winning_outcome);
});

Subscribing to Channels

After connecting, subscribe to specific channels:
use limitless_exchange_rust_sdk::{SubscriptionChannel, SubscriptionOptions};

ws.connect().await?;

ws.subscribe(
    SubscriptionChannel::Orderbook,
    SubscriptionOptions {
        market_slugs: vec!["btc-above-100k-march-2025".to_string()],
        ..Default::default()
    },
)
.await?;

Public channels

VariantDescription
SubscriptionChannel::OrderbookOrderbook updates
SubscriptionChannel::TradesTrade events
SubscriptionChannel::MarketsMarket-level updates
SubscriptionChannel::PricesAMM price data
SubscriptionChannel::SubscribeMarketPricesMarket price subscriptions

Authenticated channels

VariantDescription
SubscriptionChannel::OrdersYour order updates
SubscriptionChannel::FillsYour fill updates
SubscriptionChannel::SubscribePositionsYour position updates
SubscriptionChannel::SubscribeTransactionsYour transaction updates

SubscriptionOptions

FieldTypeDescription
market_slugOption<String>Single market slug
market_slugsVec<String>Multiple market slugs
market_addressOption<String>Single market address
market_addressesVec<String>Multiple market addresses
filtersBTreeMap<String, Value>Additional filters

Unsubscribing

ws.unsubscribe(
    SubscriptionChannel::Orderbook,
    SubscriptionOptions {
        market_slugs: vec!["btc-above-100k-march-2025".to_string()],
        ..Default::default()
    },
)
.await?;

Updating credentials

You can rotate credentials at runtime:
ws.set_api_key("lmts_new_key_here");
or:
use limitless_exchange_rust_sdk::HmacCredentials;

ws.set_hmac_credentials(HmacCredentials {
    token_id: "new-token-id".to_string(),
    secret: "new-base64-secret".to_string(),
});
If the socket is already connected, the client reconnects automatically with the new credentials.

Auto-reconnect

When auto_reconnect is enabled, the client reconnects and replays saved subscriptions automatically.
The Rust client preserves normalized subscription options internally, so reconnects restore previous subscriptions without extra application code.