Realtime (WebSocket)
Want updates the moment a score changes? Open a Socket.io connection, tell it which matches you care about, and listen. That’s the whole game:
sequenceDiagram
participant You as Your app
participant API as MyScore gateway
You->>API: connect (Socket.io)
You->>API: subscribe-match-public (matchId)
Note over API: a point is scored…
API-->>You: match:scoreUpdatePublic
API-->>You: match:scoreUpdatePublic
Note over API: match ends…
API-->>You: match:statusUpdatedPublic
1. Connect
Section titled “1. Connect”-
Install the client:
npm i socket.io-client -
Open a connection to the API origin:
import { io } from 'socket.io-client';const socket = io('https://api.myscore.live', {transports: ['websocket', 'polling'],reconnection: true, // auto-reconnect if the connection drops});
For public match scores you don’t even need a token. (Add auth: { token } only for private
rooms — see below.)
2. Subscribe to what you care about
Section titled “2. Subscribe to what you care about”socket.emit('subscribe-match-public', matchId); // one matchsocket.emit('subscribe-multiple-matches-public', [a, b]); // several at once3. Listen for updates
Section titled “3. Listen for updates”socket.on('match:scoreUpdatePublic', ({ matchId, data }) => { // data.sets = [{ home: 6, guest: 4 }, …] ← draw it on screen});
socket.on('match:statusUpdatedPublic', ({ matchId, data }) => { // data.status = 'in_progress' | 'completed' | …});That’s it — your UI now updates the instant a point is scored.
Always keep a backup
Section titled “Always keep a backup”WebSocket connections drop (tunnels, sleep, flaky wifi). The robust pattern is WebSocket for speed + a slow REST poll as a safety net:
socket.on('match:scoreUpdatePublic', applyUpdate);
setInterval(() => { if (!document.hidden) { fetch(`https://api.myscore.live/api/v1/matches/${matchId}`) .then((r) => r.json()) .then(applyUpdate); // catches anything the socket missed }}, socket.connected ? 10_000 : 15_000);