WebSocket APIConnection
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.
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*stringdata*integerThe server time, to echo back.
int64{ "type": "M", "topic": "heartbeat", "data": 1736696400000}Reply
Sent back on the same channel.
- payload
method*stringdata*integerThe data of the latest heartbeat, unchanged.
int64{ "method": "heartbeat", "data": 1736696400000}Wallet events
Subscribe with predictWalletEvents/{jwt}, where jwt is a wallet JWT from the REST API's POST /auth. A revoked, invalid or expired JWT is refused with invalid_credentials. Nothing is sent on subscribe.
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: 1. Open a new connection and subscribe to every topic again. 2. Keep reading the old connection until it closes. Both deliver during the overlap, so handle duplicates. 3. 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.