Pinnacle odds WebSocket Node.js reconnect
Answer: on close, wait min(delay*2, 30000) + jitter ms, open a new socket, resend subscribe, and rebuild state from the fresh snapshot (clear maps when part === 1). Always reply to server ping with {"type":"pong"} or the server closes you after ~60 s. Builder plan required (€99/mo). Canonical file: stream.mjs in the Python/Node guide.
Reconnect pattern#
// Example — npm install ws; export PINNFEED_KEY=...
import WebSocket from 'ws';
const WS_URL = process.env.PINNFEED_WS || 'wss://pinnfeed.io/v1/stream';
const KEY = process.env.PINNFEED_KEY;
const events = new Map();
const markets = new Map();
function connect(delay = 1000) {
const ws = new WebSocket(WS_URL, { headers: { 'x-api-key': KEY } });
ws.on('open', () => {
delay = 1000;
ws.send(JSON.stringify({
type: 'subscribe',
sports: ['soccer', 'tennis'],
markets: ['moneyline'],
main: true,
specials: false,
}));
});
ws.on('message', (raw) => {
const msg = JSON.parse(raw);
if (msg.type === 'ping') return ws.send('{"type":"pong"}');
if (msg.type === 'snapshot') {
if (msg.part === 1) { events.clear(); markets.clear(); }
msg.events.forEach((e) => events.set(e.id, e));
msg.markets.forEach((m) => markets.set(m.id, m));
if (msg.final) console.log(`book ready: ${events.size} events, ${markets.size} markets`);
}
// apply deltas the same way as in the full stream.mjs
});
ws.on('close', (code) => {
const wait = Math.min(delay * 2, 30000);
console.log(`closed (${code}); reconnecting in ${wait / 1000}s`);
setTimeout(() => connect(wait), wait + Math.random() * 1000);
});
}
connect();
Close code 1001 (server_shutdown) means a deploy — reconnect with backoff (WebSocket docs). Do not stampede: jitter matters.
Trial CTA#
Start Builder — 3-day trial; card required, charged after day 3 unless cancelled. Related: Python example.
Questions people ask#
Should I reuse the same Map across reconnects?#
Clear on the first snapshot part (part === 1). Otherwise you keep markets the new snapshot never mentioned.
Can Hobby reconnect to the stream?#
Hobby has no WebSocket. You will get 403 plan_lacks_ws before any reconnect logic runs.
Is this educational content for adults?#
Yes (18+).