The server enforces a 60-second idle timeout. If no data flows through your connection for 60 seconds, it will be automatically closed.
Send periodic pings to keep your connection alive:
Ping
Pong (server response)
Send a ping every 30 seconds to stay well within the timeout window:
JavaScript
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ method: "ping" }));
}
}, 30000);
ws.onclose = () => clearInterval(pingInterval);
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ method: "ping" }));
}
}, 30000);
ws.onclose = () => clearInterval(pingInterval);
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ method: "ping" }));
}
}, 30000);
ws.onclose = () => clearInterval(pingInterval);Python
import asyncio
async def keepalive(ws):
while True:
await ws.send('{"method": "ping"}')
await asyncio.sleep(30)import asyncio
async def keepalive(ws):
while True:
await ws.send('{"method": "ping"}')
await asyncio.sleep(30)import asyncio
async def keepalive(ws):
while True:
await ws.send('{"method": "ping"}')
await asyncio.sleep(30)If you are subscribed to high-frequency channels like raw_book_diffs, the constant data flow acts as an implicit keepalive. Explicit pings are only needed for quiet channels.