Skip to content

Quickstart

This is the smallest useful program: connect, listen for one event, read the snapshot, disconnect.

import { createClient } from 'realtimeodds'
const client = createClient({
url: 'wss://api.realtimeodds.xyz',
apiKey: process.env.REALTIMEODDS_API_KEY!
})
client.on('sportEvent:added', ({ sportEvent }) => {
console.log(`[${sportEvent.bookmaker}] new ${sportEvent.sport}: ${sportEvent.name}`)
})
client.on('odds:changed', ({ bookmaker, selectionId, quote }) => {
console.log(`[${bookmaker}] ${selectionId}${quote.price}`)
})
await client.connect()

connect() resolves on the first successful connection. Once it resolves, the SDK has already populated its in-memory store; the sportEvent:added events that fire from then on are incremental — sport events appearing for the first time after the initial sync.

The snapshot is a synchronous read of every sport event the SDK currently knows about, keyed by SportEventId:

const { sportEvents, stale } = client.snapshot()
for (const event of sportEvents.values()) {
console.log(`${event.name}${event.markets.size} markets`)
}
if (stale) {
console.warn('Connection is down — data may be outdated')
}

Lookup a single event by id:

const event = client.getSportEvent('vmid:ps3838:1610547234')

sportEvent.markets is a ReadonlyMap<MarketId, Market>. Each market exposes selections, also a ReadonlyMap. Use .values() to iterate:

for (const market of event.markets.values()) {
for (const selection of market.selections.values()) {
if (selection.quote) {
console.log(`${selection.result}: ${selection.quote.price}`)
}
}
}
await client.disconnect()

disconnect() closes the WebSocket and stops any reconnect loop. Idempotent.