WebSocket APIConnection
Reconnect notice
Sent before a server shuts down, for example during a rollout. The connection keeps
delivering for about graceMs, then closes with code 1001 and reason server draining.
On receiving it:
- Open a new connection and subscribe to every topic again.
- Keep reading the old connection until it closes. Both deliver during the overlap, so handle duplicates.
- If the new connection is refused, retry right away. The next attempt reaches a server that is not shutting down.
Treat the 1001 close as a reason to reconnect too, in case the notice was missed.
graceMs is approximate, so do not schedule against it. event and reason may gain
values, and an unknown one also means reconnect soon.
x-api-key<api-key>The API key in the handshake's x-api-key header. Use this where you can.
const socket = new WebSocket("wss://ws.predict.fun/ws?apiKey=YOUR_API_KEY");socket.addEventListener("message", (event) => { console.log(event.data);});- payload
type*stringtopic*stringReserved. It cannot be subscribed to, and its messages arrive unasked.
data*{ "type": "M", "topic": "system", "data": { "event": "reconnect", "reason": "server_shutdown", "graceMs": 7431 }}Heartbeat
Every 15 seconds the server sends a heartbeat carrying the current time. The first one arrives about 15 seconds after the connection opens. Echo its data back before the next one. A missing, late or different value closes the connection with code 1000 at the next heartbeat. The server does not respond to the echo.
Client example
A complete TypeScript client for the WebSocket API, to copy into your app or read alongside the how-tos.