Urgent.News

What's breaking now, across thousands of outlets.

Tech

Building a Live Scoreboard with a Sports Data API (Vanilla JS + WebSocket)

In this tutorial we'll build a working live scoreboard — the kind you'd embed on a sports site or a fan-engagement app — using Orbistats as the data source. We'll cover the initial fixture fetch, the WebSocket connection for live updates, and basic UI rendering. No framework required; this works as a drop-in you can adapt into React/Vue later if you want.</p> <p>What We're Building</p> <p>A…

Building a live scoreboard using a Sports Data API with Vanilla JavaScript and WebSocket technology is demonstrated in this tutorial. The scoreboard will load today's fixtures using REST API on page load, connect to a live feed and update scores in real-time using WebSocket, show a pulsing "LIVE" badge on in-progress matches, and gracefully fall back if the socket drops.

First, an API key is obtained, either by testing with Orbistats' public sandbox or by signing up for a free tier key. This API key is used to fetch today's fixtures. The fetch function is used to retrieve the fixtures from the Orbistats API endpoint, and the JSON response is returned.

Next, the initial scoreboard is rendered on the webpage by creating a container element, fetching fixtures, iterating over each match, and creating a card element for each match. The container is then appended with the cards.

A persistent WebSocket connection is established to receive live updates. The onopen event logs when the live feed is connected, and the onmessage event updates the scoreboard with the received update. The onerror and onclose events handle errors and disconnections, respectively.

The update handler function updates the score and status of a specific match card with the new data received from the live feed. The payload from the WebSocket follows the structure documented on the Live Scores API page, containing match_id, status, minute, and the scores for each team.

Finally, the scoreboard is initialized by fetching fixtures, rendering the initial board, and connecting to the live feed. Basic CSS is provided for styling the match cards with appropriate colors, fonts, and animations for the "LIVE" status.

Potential future improvements include implementing fallback polling, showing odds alongside scores, supporting multiple sports, and skipping UI creation altogether if only data is needed.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Sunday 20 September →