|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Loading prices...
WebSocket

Subscriptions

Learn how to subscribe and unsubscribe to real-time data channels for prices, orderbooks, and trades.

Subscribe to Channels

Subscribe to receive real-time updates for specific markets:

ws.send(JSON.stringify({
  type: 'subscribe',
  channel: 'prices',        // Channel type
  marketId: 'MARKET_ID'     // Market to subscribe to
}))

You'll receive a confirmation message:

{"type": "subscribed", "channel": "prices", "marketId": "..."}

Unsubscribe from Channels

Stop receiving updates for a specific channel:

ws.send(JSON.stringify({
  type: 'unsubscribe',
  channel: 'prices',
  marketId: 'MARKET_ID'
}))
All subscriptions are automatically cleaned up when the WebSocket connection closes.

Available Channels

prices

Real-time price updates for YES and NO tokens. Updates on every trade or significant price change.

orderbook

Orderbook changes including new orders, fills, and cancellations. Provides bid/ask depth up to 20 levels.

trades

Individual trade notifications with price, size, and side. Useful for trade tape displays.

market

Market-level updates including status changes and resolutions. Subscribe to receive resolution notifications.

Subscription Examples

Subscribe to multiple channels for comprehensive data:

const marketId = 'YOUR_MARKET_ID'
const channels = ['prices', 'orderbook', 'trades']

channels.forEach(channel => {
  ws.send(JSON.stringify({
    type: 'subscribe',
    channel,
    marketId
  }))
})

// Track subscriptions
const subscriptions = new Map()

ws.onmessage = (event) => {
  const data = JSON.parse(event.data)
  
  if (data.type === 'subscribed') {
    subscriptions.set(data.channel, data.marketId)
    console.log(`Subscribed to ${data.channel}`)
  }
}

Subscription hygiene

Subscribe only to markets you render. Fanning out dozens of idle channels increases CPU on the server and your client. Unsubscribe when routes unmount and debounce rapid subscribe/unsubscribe loops during search UIs.

If you hydrate SSR pages, wait for onopen before sending subscribe frames, and queue messages until the socket is ready so you do not drop initial subscription intents.