> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apexiumbet.com/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket encodings - JSON, MessagePack and zstd

> The three encodings of the Apexium API WebSocket, which one production accepts, and how to decode each in Python and Node.js.

You choose the encoding of channel messages with `receiveType` at login.

| `receiveType` | Frame | Content |
| - | - | - |
| `json` | text | The envelope as JSON |
| `binary` | binary | The envelope as [MessagePack](https://msgpack.org) |
| `zstd` | binary | The envelope as JSON, compressed with [zstd](https://facebook.github.io/zstd/) |

> [!WARNING]
>
> Production accepts `zstd` only. A login with another `receiveType` is refused with `4001`. The odds feed is several megabits per second for a client that follows many fixtures, and zstd makes it about four times smaller.

* **Control frames.** `login_ok`, `subscribed`, `snapshot_required`, `error` and the other control frames are always JSON text frames, whatever the `receiveType`. Tell them apart by the frame type: text is a control frame, binary is a channel message.
* **One frame, one message.** Each binary frame holds exactly one message, compressed on its own. There is no dictionary and no state between frames.

## Decode in Python

With [`websockets`](https://pypi.org/project/websockets/) and [`zstandard`](https://pypi.org/project/zstandard/):

```python theme={null}
import asyncio
import json

import websockets
import zstandard

URL = "wss://api.apexiumbet.com/v1/ws"
LOGIN = {
    "type": "login",
    "apiKey": "apx_live_…",
    "receiveType": "zstd",
    "channels": ["odds", "orders"],
    "fixtureIds": ["id1000001770366972"],
}

decompressor = zstandard.ZstdDecompressor()


async def main():
    async with websockets.connect(URL) as ws:
        await ws.send(json.dumps(LOGIN))
        async for frame in ws:
            if isinstance(frame, str):
                control = json.loads(frame)
                print("control", control["type"], control)
                continue
            message = json.loads(decompressor.decompress(frame))
            print(message["channel"], message["type"], message["ts"])


asyncio.run(main())
```

For `binary`, decode with [`msgpack`](https://pypi.org/project/msgpack/) instead: `message = msgpack.unpackb(frame)`.

## Decode in Node.js

Node.js 22.15 or later decompresses zstd natively. With [`ws`](https://www.npmjs.com/package/ws):

```js theme={null}
import { zstdDecompressSync } from 'node:zlib';
import WebSocket from 'ws';

const ws = new WebSocket('wss://api.apexiumbet.com/v1/ws');

ws.on('open', () =>
  ws.send(
    JSON.stringify({
      type: 'login',
      apiKey: 'apx_live_…',
      receiveType: 'zstd',
      channels: ['odds', 'orders'],
      fixtureIds: ['id1000001770366972'],
    })
  )
);

ws.on('message', (data, isBinary) => {
  if (!isBinary) {
    const control = JSON.parse(data.toString());
    console.log('control', control.type, control);
    return;
  }
  const message = JSON.parse(zstdDecompressSync(data).toString());
  console.log(message.channel, message.type, message.ts);
});
```

For `binary`, decode with [`msgpackr`](https://www.npmjs.com/package/msgpackr) instead: `const message = unpack(data)`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.