Skip to content

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
Connect → subscribe → receive. The server pushes; you just listen.
  1. Install the client: npm i socket.io-client

  2. 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.)

socket.emit('subscribe-match-public', matchId); // one match
socket.emit('subscribe-multiple-matches-public', [a, b]); // several at once
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.

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);