Overview
The CLOB WebSocket API provides real-time streaming data for market updates and user-specific events. This is essential for building responsive trading applications that react to market changes instantly.WebSocket Endpoints
The CLOB offers two WebSocket endpoints:| Endpoint | Purpose | Authentication |
|---|---|---|
/ws/market | Market data (orderbook, trades, prices) | Not required |
/ws/user | User-specific events (orders, fills, balances) | Required |
Prerequisites
- WebSocket client library (the examples use
wspackage) - CLOB API credentials (for user endpoint only)
- Token IDs or condition IDs to subscribe to
Market Data Streaming
Subscribe to public market data without authentication:Basic Market Connection
Market Data Events
The market WebSocket streams several event types:Orderbook Updates
Orderbook Updates
Sent when the orderbook changes:
Trade Events
Trade Events
Sent when trades are executed:
Price Updates
Price Updates
Sent when last trade price changes:
User Event Streaming
Subscribe to your personal trading events with authentication:Authenticated User Connection
User Event Types
Order Status Updates
Order Status Updates
Sent when your order status changes:Possible statuses:
LIVE, MATCHED, CANCELLED, PARTIALLY_FILLEDFill Events
Fill Events
Sent when your order is (partially) filled:
Balance Updates
Balance Updates
Sent when your balances change:
Complete Example
Here’s a production-ready WebSocket client with reconnection logic:Subscription Options
Subscribe by Token IDs
Receive updates for specific outcome tokens:Subscribe by Market/Condition
Receive updates for all tokens in a market:Initial Dump
Setinitial_dump: true to receive the current state immediately upon connection:
Best Practices
Keep-Alive Pings
Send PING every 50 seconds to prevent connection timeout.
Reconnection Logic
Implement exponential backoff for reconnection attempts.
Error Handling
Handle malformed messages and connection errors gracefully.
Subscription Limits
Limit subscriptions to necessary assets to reduce bandwidth.
Common Issues
Connection Timeout
Connection Timeout
Problem: WebSocket disconnects after 60 seconds.Solution: Send
PING messages every 50 seconds:Authentication Failed
Authentication Failed
Problem: User WebSocket rejects connection.Solution: Verify your API credentials are correct and active:
No Events Received
No Events Received
Problem: Connected but not receiving updates.Solution: Ensure you’re subscribed to active markets and your subscription message is correct. Set
initial_dump: true to test.Next Steps
Market Data Guide
Learn to interpret and use market data
Order Management
React to order events and manage positions
Basic Orders
Create orders programmatically
Trading Concepts
Understand CLOB trading mechanics