PerpsWsClient provides streaming data over WebSocket. Connections are made directly to the DEX for lowest latency — the SDK discovers the WebSocket URL from GET /dexes automatically.
All incoming data is normalized to the same types used by REST responses, so your application logic works identically whether data comes from a REST call or a WebSocket event.
Setup
PerpsWsClient lazily initializes connections — no WebSocket is opened until you call subscribe().
Subscribe
All channels use the same pattern: pass a subscription descriptor and a callback. The returned function unsubscribes when called.Available Channels
prices
All mid prices for every market on the DEX. Fires on every price change.orderbook
L2 orderbook for a specific market. Fires on every book update.trades
Public trade feed for a specific market.candle
OHLCV candle updates for a specific market and interval.orderUpdates
Live order status changes for a user’s orders. Requires the user’s wallet address.fills
Live fill notifications for a user’s trades.positions
Live position updates for a user.Multiple Subscriptions
You can subscribe to multiple channels simultaneously. Each returns an independent unsubscribe function.Connection Lifecycle
- Lazy connection — The WebSocket connects on the first
subscribe()call for each DEX - Automatic reconnection — Exponential backoff reconnection on disconnect (150ms, 300ms, 600ms, up to 10s)
- Resubscription — All active subscriptions are automatically re-sent after reconnection
- Keepalive — 30-second ping/pong heartbeat to detect stale connections
- Refcounted — Duplicate subscriptions to the same channel are deduplicated; the upstream subscription is removed only when the last listener unsubscribes
Cleanup
Always close the client when you’re done to release WebSocket connections:Type Safety
Thesubscribe method is fully typed — the callback receives the correct event type based on the subscription channel: