guides

Live odds over WebSocket in JavaScript: a step-by-step tutorial

By · Published · Updated · 4 min read

This tutorial builds a Node.js client that keeps live odds from 2 sportsbooks in memory, in about 65 lines. It connects to the PricedOdds WebSocket, applies each price change, and resumes from the last sequence number after a drop.

You need Node.js 22 or later, which has a built-in WebSocket client, and no packages. Put the five steps in one file, for example live-odds.mjs, and run it with node live-odds.mjs. Your plan needs WebSocket access: All-Access, or the WebSocket add-on. The 3-day trial includes 1 connection. Signup takes a $1 card verification that is refunded immediately, and the plan you pick is charged from day 4 unless you cancel before the trial ends.

Step 1: connect and authenticate

Open the socket and send your key as the first message. The server answers with a ready frame that lists your plan and books.

const URL = "wss://api.pricedodds.com/v1/stream";
const KEY = process.env.PRICEDODDS_API_KEY;

function connect() {
  const ws = new WebSocket(URL);
  ws.addEventListener("open", () => ws.send(JSON.stringify({ type: "auth", key: KEY })));
  return ws;
}

Run this on a server. Never ship the key to a browser.

Step 2: subscribe

When ready arrives, send one subscribe message. Every field is optional, and filters keep the stream small.

const FILTERS = {
  leagues: ["nba", "nfl"],
  books: ["draftkings", "fanduel"],
  markets: ["moneyline", "spread", "total"],
  include: ["best", "fair"],
};

Step 3: keep the prices in a Map

The server first sends the current state in snapshot frames (10 events per frame, numbered with part and parts), then price frames with what changed. Key each price by selection and book, and fill the Map from both.

const prices = new Map();   // "sel_...|draftkings" -> { american, decimal, line }
let lastSeq = 0;

function applySnapshot(msg) {
  for (const { markets } of msg.data)
    for (const market of markets)
      for (const sel of market.selections)
        for (const p of sel.prices)
          prices.set(`${sel.id}|${p.book}`, { american: p.american, decimal: p.decimal, line: sel.line });
}

function applyPrice(msg) {
  const { book, changes } = msg.data;
  for (const c of changes) {
    const key = `${c.selection_id}|${book}`;
    if (c.decimal === null) prices.delete(key);        // the book pulled the price
    else prices.set(key, { american: c.american, decimal: c.decimal, line: c.line });
  }
}

A change carries market, period, side, line, decimal, american and prev_american. With include: ["best", "fair"], the frame also has the best price across your books and the no-vig fair price for each changed selection.

Step 4: handle every frame type

Store the highest seq you have seen. You need it to resume.

function handle(ws, raw, resumed) {
  const msg = JSON.parse(raw);
  if (msg.seq) lastSeq = Math.max(lastSeq, msg.seq);

  switch (msg.type) {
    case "ready":
      ws.send(JSON.stringify(resumed && lastSeq
        ? { type: "resume", since_seq: lastSeq, ...FILTERS }
        : { type: "subscribe", ...FILTERS }));
      break;
    case "snapshot":
      applySnapshot(msg);
      break;
    case "price":
      applyPrice(msg);
      break;
    case "resync":
      prices.clear();        // too old to resume: a fresh snapshot follows
      break;
    case "entitlement":
      FILTERS.books = FILTERS.books.filter((b) => msg.data.books.includes(b));
      break;
    case "error":
      console.error(msg);
      break;
  }
}

The entitlement frame arrives when your plan or book selection changes while you are connected. Keep only the books that are still in data.books.

Step 5: reconnect and resume

Networks drop. Reconnect with a short delay and resume from lastSeq. The server replies resume_ok and sends the frames you missed.

function run(resumed = false) {
  const ws = connect();
  ws.addEventListener("message", (event) => handle(ws, event.data, resumed));
  ws.addEventListener("close", (event) => {
    if ([4001, 4002, 4003].includes(event.code)) return;   // auth, billing, plan: fix the account first
    setTimeout(() => run(true), 2000);
  });
}

run();

We ran the script, the five steps in one file, on November 25, 2026, at 07:45 UTC, with Node 24 and a key on the All-Access plan. We added a log of frame types, closed the connection on purpose after 45 seconds and stopped it after 90. The first connection received ready and 9 snapshot frames; after the forced close it reconnected, sent resume, received resume_ok and carried on. In those 90 seconds it received 3 price frames, and the Map held 708 prices for NBA and NFL games at DraftKings and FanDuel at the end. The counts depend on the games on the board at that time.

Which close codes matter?

Six of them. Three mean "fix your account" and should not trigger a retry loop.

CodeMeaningWhat to do
4001Auth failedCheck the key
4002BillingUpdate payment
4003Plan has no WebSocketAdd the WebSocket add-on or choose All-Access
4008slow_consumer: your client could not keep upReconnect and resume. Do less work per message
4029Too many connections for your planClose another connection
Any otherNetwork dropReconnect and resume

The server pings every 20 seconds. Connections per plan: 1 on Solo and Starter, 2 on Growth and 3 on All-Access.

What does WebSocket access cost?

WebSocket is included in All-Access at $349 a month. It is an add-on on the other plans: +$59 on Solo, +$129 on Starter and +$169 on Growth. Solo with the add-on is $98 a month for any 2 books. See pricing and the WebSocket docs. Measured latency: in a 20-minute test on 23 November 2026, live prices reached an external client in 486 ms at the median and 2,399 ms at p95 from the upstream source timestamp, upstream delay included. Method and tables by book are on the latency page.

Before you subscribe, check the live odds API coverage by book.

The WebSocket subscription builder writes the subscribe and resume messages and a Node client for the filters you pick.

To find the exact key of a market or prop before you subscribe, use the market key finder.

For the REST calls with types, paging and typed errors, see the typed TypeScript client.

One use of this client is a chat bot: a Telegram bot that sends sportsbook price moves from the same stream.