A reconnect that opens a fresh socket and waits for a full snapshot is correct, but it is slower than it needs to be. If the feed gives you a monotonically increasing sequence number, you can ask for the frames you missed and keep going. If the cursor is too old, the server tells you to resync and you take a new snapshot. That is the difference between a one-second blip and a board that looks empty for the length of a snapshot.
This walk-through uses the contract documented by PricedOdds: authenticate on wss://api.pricedodds.com/v1/stream, wait for ready, subscribe, apply snapshot parts, then patch price frames. After a drop, resume with since_seq. The pattern is vendor-specific in the field names, but the state machine is the one you want on any sequenced odds stream.
It is not betting advice. It does not invent latencies or book counts.
1. What you have to remember
Keep four pieces of state, all on the client:
-
The last sequence number you applied. Every
snapshotorpriceframe carries a sequence. Persist it as soon as you have applied the frame, not when you receive it. If you persist first and then crash mid-apply, a resume can skip data. -
The current subscription. Sport, league, book and market filters, plus any
includeflags (best,fair). You must send the same filter on resume that you used on subscribe. -
Whether the opening snapshot is complete. PricedOdds sends
snapshotframes withpart/parts. Do not treat the board as live until every part has arrived. -
A resume-or-resync flag. A
resume_okmeans you can keep patching. Aresyncmeans the cursor is too old: clear the in-memory board and wait for a fresh snapshot.
Store the sequence in the same place as the board (a variable is fine; SQLite is better if the process can die). Do not store it only in the WebSocket object — that dies with the connection.
2. The happy path
// documented PricedOdds stream: auth, ready, subscribe, snapshot, price
const SUB = {
type: "subscribe",
leagues: ["nba"],
books: ["draftkings", "fanduel"],
markets: ["moneyline", "spread"],
include: ["best", "fair"],
};
function sendSubscribe(ws, lastSeq) {
if (lastSeq != null) {
ws.send(JSON.stringify({ ...SUB, type: "resume", since_seq: lastSeq }));
return;
}
ws.send(JSON.stringify(SUB));
}
On a first connection lastSeq is null, so you send subscribe. The server replies with one or more snapshot frames, then price frames only when something changes. After a disconnect you send resume with the last applied sequence.
The ready frame in the PricedOdds docs includes heartbeat_seconds: 20. Treat that as the deadline for a heartbeat: if you see no frame in a little more than 20 seconds, close the socket yourself and reconnect. Do not wait for the TCP timeout.
3. Assembling a chunked snapshot
A snapshot can arrive in several parts. In a 24 November 2026 docs run (Node, 20 seconds, NBA moneyline on two books) the socket sent one ready and five snapshot parts. Your client should buffer parts and install them atomically:
function makeSnapshotBuffer() {
const parts = new Map();
let expected = null;
return function accept(frame) {
expected = frame.parts;
parts.set(frame.part, frame);
if (expected == null || parts.size < expected) return null;
const ordered = [];
for (let i = 1; i <= expected; i++) {
const p = parts.get(i);
if (!p) return null; // still missing a hole
ordered.push(p);
}
parts.clear();
expected = null;
return ordered;
};
}
Only after accept returns the full set should you replace the in-memory board. If a price frame arrives before the snapshot is complete, queue it and apply it afterwards, in sequence order. Applying it first can write a new price that the late snapshot then overwrites with a stale one.
4. Applying prices, then the sequence
function applyPrice(board, frame) {
for (const change of frame.changes || []) {
const key = change.sel || [change.event, change.market, change.period, change.side, change.line].join(":");
if (change.decimal === null) {
board.delete(key); // the book pulled the price
continue;
}
board.set(key, change);
}
}
PricedOdds uses opaque, stable ids (evt_, mkt_, sel_, plr_) for events, markets, selections and players. Book, league and market type ids are readable (draftkings, nba, moneyline). Join on the opaque ids, not on display names.
decimal: null means the book pulled that price. A frame whose american equals prev_american on an exchange or prediction market is a liquidity update, not a price move — keep that distinction if you also subscribe to those venues. DraftKings and FanDuel will not carry lay or liquidity fields.
Persist the sequence only after the board write succeeds:
function applyFrame(state, frame) {
if (frame.type === "snapshot") {
const complete = state.buffer(frame);
if (!complete) return;
const next = new Map();
for (const part of complete) applyPrice(next, part);
state.board = next;
state.lastSeq = complete[complete.length - 1].seq;
state.snapshotReady = true;
for (const queued of state.queue) applyFrame(state, queued);
state.queue = [];
return;
}
if (frame.type === "price") {
if (!state.snapshotReady) {
state.queue.push(frame);
return;
}
applyPrice(state.board, frame);
state.lastSeq = frame.seq;
}
}
5. Resume, resync, or start over
After you reconnect and the socket is ready:
ws.on("message", (raw) => {
const frame = JSON.parse(raw);
if (frame.type === "ready") {
sendSubscribe(ws, state.lastSeq);
return;
}
if (frame.type === "resume_ok") {
state.snapshotReady = true; // board is still valid
return;
}
if (frame.type === "resync") {
state.snapshotReady = false;
state.board = new Map();
state.queue = [];
// A fresh snapshot will follow. lastSeq will be replaced when it lands.
return;
}
if (frame.type === "error") {
console.error(frame.code, frame.message, frame.did_you_mean);
return;
}
applyFrame(state, frame);
});
A resume_ok is followed by the missed frames, in order. A resync is followed by a new snapshot; throw the old board away. If you keep both, you will mix two eras of prices.
Close codes worth handling, from the PricedOdds docs: 4001 auth, 4002 billing, 4003 plan, 4008 slow consumer, 4029 too many connections. 4008 means you are not reading fast enough — drain the socket, apply, and only then do slower work (database writes, alerts) on another queue. 4029 means you already have as many sockets as the plan allows; do not retry in a tight loop.
6. Do not resume markets that are not real-time
Not every market type is pushed as a delta. Read GET /v1/coverage (no key required) and keep the keys with realtime: true. Other markets still appear in the snapshot and can arrive as price frames when a REST refresh changes them, but a resume will not make them tick faster than the documented REST cadence (1 minute for live events, 15 minutes inside 6 hours, 60 minutes inside 36 hours, 120 minutes out to 7 days, as published by PricedOdds).
If you only bought two books on Solo, asking for a third returns book_not_in_plan. The resume filter has to stay inside the plan.
7. A small test you can run without a live slate
The resume logic can be tested with a fake sequence of frames. No network is required:
const test = require("node:test");
const assert = require("node:assert");
test("queued prices apply only after the snapshot is complete", () => {
const state = makeState();
applyFrame(state, { type: "price", seq: 2, changes: [{ sel: "a", decimal: 1.9 }] });
assert.equal(state.board.size, 0);
applyFrame(state, { type: "snapshot", part: 1, parts: 1, seq: 1, changes: [{ sel: "a", decimal: 2.0 }] });
assert.equal(state.board.get("a").decimal, 1.9); // queued price applied after
assert.equal(state.lastSeq, 2);
});
test("resync discards the old board", () => {
const state = makeState();
state.board.set("a", { decimal: 2.0 });
state.lastSeq = 10;
// emulate the handler branch
state.snapshotReady = false;
state.board = new Map();
applyFrame(state, { type: "snapshot", part: 1, parts: 1, seq: 50, changes: [{ sel: "b", decimal: 1.8 }] });
assert.equal(state.board.has("a"), false);
assert.equal(state.board.get("b").decimal, 1.8);
assert.equal(state.lastSeq, 50);
});
Checklist
- Persist
lastSeqonly after the frame has been applied - Send the same filter on
resumethat you used onsubscribe - Wait for every
snapshotpart before marking the board live - Queue
priceframes that arrive early; apply them in sequence afterwards - On
resync, drop the board and wait for a new snapshot - Honour
heartbeat_secondsfromready(20 in the current PricedOdds docs) - Back off on 4008 and 4029; do not reconnect in a tight loop
- Subscribe only to market types with
realtime: trueif you need push updates
If you want the stream contract and the catalog of books and market types in one place, start with the PricedOdds docs. Official catalog endpoints (/v1/sports, /v1/leagues, /v1/books, /v1/markets, /v1/coverage, /v1/status) can be read without a key.
18+ only. This tutorial is educational and is not betting advice.
Editorial note: Tools for Bettors publishes independent reviews and guides on betting tools and odds data.
Top comments (0)