|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Loading prices...
WebSocket
Message Types
Reference for all WebSocket message types sent and received by the Polyblock server.
Server to Client Messages
price_update
Sent when market prices change:
{
"type": "price_update",
"marketId": "...",
"yesPrice": 0.65,
"noPrice": 0.35,
"timestamp": "2024-01-15T10:30:00Z"
}orderbook_update
Sent when orderbook changes:
{
"type": "orderbook_update",
"marketId": "...",
"bids": [{"price": 0.64, "size": 1000}],
"asks": [{"price": 0.66, "size": 800}],
"timestamp": "2024-01-15T10:30:00Z"
}trade
Sent when a trade executes:
{
"type": "trade",
"marketId": "...",
"price": 0.65,
"size": 100,
"side": "BUY",
"outcome": "YES",
"timestamp": "2024-01-15T10:30:00Z"
}market_resolved
Sent when a market resolves:
{
"type": "market_resolved",
"marketId": "...",
"outcome": "YES",
"timestamp": "2024-01-15T10:30:00Z"
}Client to Server Messages
subscribe
Subscribe to a channel:
{
"type": "subscribe",
"channel": "prices",
"marketId": "..."
}unsubscribe
Unsubscribe from a channel:
{
"type": "unsubscribe",
"channel": "prices",
"marketId": "..."
}ping
Keep connection alive:
{
"type": "ping"
}Message Format
All messages are JSON objects with a required type field:
interface WebSocketMessage {
type: string
marketId?: string
channel?: string
timestamp?: string
[key: string]: unknown
}Message Handling Example
ws.onmessage = (event) => {
const message = JSON.parse(event.data)
switch (message.type) {
case 'price_update':
updatePriceDisplay(message.yesPrice, message.noPrice)
break
case 'orderbook_update':
updateOrderbook(message.bids, message.asks)
break
case 'trade':
addToTradeTape(message)
break
case 'market_resolved':
handleResolution(message.outcome)
break
case 'subscribed':
console.log(`Subscribed to ${message.channel}`)
break
case 'error':
console.error('WebSocket error:', message.error)
break
default:
console.log('Unknown message type:', message.type)
}
}