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

# Websocket

Mainnet websocket URL: [wss://ws.pacifica.fi/ws](wss://ws.pacifica.fi/ws)\
Testnet websocket URL: [wss://test-ws.pacifica.fi/ws](wss://test-ws.pacifica.fi/ws)

The API service provides a universal endpoint for websocket streams. The subscribed data will be streamed in the corresponding channel after the connection is established.

### Subscription Message

```
{
    "method": "subscribe",
    "params": { ... }
}
```

### Unsubscription Message

```
{
    "method": "unsubscribe",
    "params": { ... }
}
```

### Heartbeat and Timeout

A websocket connection will be closed if no message is sent for the past 60 seconds, or the connection has been alive for 24 hours.

To keep the connection alive without messages in 60 seconds, we can send a heartbeat message

```
{
    "method": "ping"
}
```

and an alive connection will respond with

```
{
    "channel": "pong"
}
```

### Compression

To reduce bandwidth, open the connection with `?compress=deflate`:

```
wss://ws.pacifica.fi/ws?compress=deflate
```

The server then sends subscription data and snapshots as binary frames of raw DEFLATE data (no zlib header). Each frame holds one complete message. Other messages, such as `pong`, errors, and trading responses, stay as text frames.

The compression state carries across messages, so use one decompressor for the whole connection. This is not the standard `permessage-deflate` extension.

```python theme={null}
import zlib

inflater = zlib.decompressobj(-zlib.MAX_WBITS)  # one per connection

def decode(message):
    if isinstance(message, bytes):
        return inflater.decompress(message).decode()
    return message
```

### Slow Clients

For `book` and `bbo`, a client that reads slowly receives only the latest snapshot, not every update in between. If the server cannot write to a connection for 60 seconds, it closes the connection with close code `1011`.


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