DEV Community

kenn-kentonm
kenn-kentonm

Posted on

Polling vs WebSocket for live football scores: what I'd pick and why

If you're showing live football scores, you have two options.
Polling. Request the live feed every 20 to 60 seconds. It's simple, it works everywhere, and a score strip doesn't need more. The feed is cached on the server for about 12 seconds, so polling faster than 20 seconds gains nothing.
setInterval(async () => {
const res = await fetch("https://api.predictiqpro.com/live/feed", { headers });
console.log(await res.json());
}, 30000);
WebSocket. You get a snapshot on connect, then only the matches that changed. Fewer requests, faster goals. The cost is reconnect logic and keeping a connection alive. I'd use it for a goal alert bot and polling for a page.
Whichever you pick, run it on a server. A key in browser JavaScript can be copied by anyone.
Full quickstart with both: live football scores in JavaScript. Per-league pages with the league ID and ready requests: Eredivisie, Brasileirão, La Liga.
The live feed needs an API key, which switches on with a 14-day trial (card required, nothing charged until it ends). The WebSocket needs the Developer plan or higher.

Top comments (0)