WebSocket
Subscribe to realtime presence updates over a WebSocket gateway.
WSS /socketAppend ?compression=zlib_json to receive zlib-compressed frames.
Handshake
On connect you receive Opcode 1: Hello with a heartbeat_interval. Send
Opcode 2: Initialize immediately, then Opcode 3: Heartbeat on the
interval.
| Opcode | Name | Direction |
|---|---|---|
| 0 | Event (INIT_STATE, PRESENCE_UPDATE) | Receive |
| 1 | Hello | Receive |
| 2 | Initialize | Send |
| 3 | Heartbeat | Send |
| 4 | Unsubscribe | Send |
Subscribing
// Opcode 2 — subscribe to one, many, or all
{ "op": 2, "d": { "subscribe_to_id": "94490510688792576" } }
{ "op": 2, "d": { "subscribe_to_ids": ["id1", "id2"] } }
{ "op": 2, "d": { "subscribe_to_all": true } }Minimal client
const ws = new WebSocket('wss://api.zruvix.com/socket');
ws.onmessage = ({ data }) => {
const { op, d } = JSON.parse(data);
if (op === 1) {
// Hello — subscribe, then start heartbeating.
ws.send(JSON.stringify({ op: 2, d: { subscribe_to_id: '94490510688792576' } }));
setInterval(() => ws.send(JSON.stringify({ op: 3 })), d.heartbeat_interval);
}
if (op === 0) {
// INIT_STATE or PRESENCE_UPDATE
console.log(d);
}
};