Quickstart
This is the smallest useful program: connect, listen for one event, read the snapshot, disconnect.
30 seconds to first odds
Section titled “30 seconds to first odds”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.
Reading the current state
Section titled “Reading the current state”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')Iterating over markets and selections
Section titled “Iterating over markets and selections”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}`) } }}Cleanup
Section titled “Cleanup”await client.disconnect()disconnect() closes the WebSocket and stops any reconnect loop. Idempotent.
Next steps
Section titled “Next steps”- Authentication — API keys and close codes.
- Data model —
SportEvent,Market,Selection,Quote,OrderBook. - Sport discrimination — narrow on
sportto access sport-specific fields. - Multi-bookmaker — same match, two sources.