> ## Documentation Index
> Fetch the complete documentation index at: https://sonar-cf3aeac7.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Message reference

> Every message you can send to /v1/ws and every message the server sends back.

All messages are JSON objects sent as text frames. Messages you send have an `op` field. Messages the server sends have a `type` field. In the transcripts below, `->` is a message you send and `<-` is a message from the server.

Unknown extra fields in your messages are ignored.

## Client messages

### subscribe

Starts a subscription on a channel.

```json theme={null}
-> {"op":"subscribe","id":"weth-trades","channel":"fast-trade","params":{"chain":"evm:8453","token":"0x4200000000000000000000000000000000000006"}}
```

**op** `"subscribe"` *required*

Operation name.

**id** `string` *required*

Your name for this subscription, 1 to 64 characters from `A-Z a-z 0-9 _ . : -` (regex `^[\w.:-]+$`). It must not match another active subscription on this connection. The server echoes it on every message for this subscription.

**channel** `string` *required*

One of `fast-trade`, `token-details`, `market-details` or `ohlcv`. Any other value returns `UNKNOWN_CHANNEL`.

**params** `object` *required*

Channel parameters. The channel validates them. See each channel page for the allowed fields.

**since** `string | object`

Resume position. Either a cursor string such as `"38123457-0:4"`, or a map with one entry, `{"<channel>|<chain>": "<cursor>"}`. When set, the server replays what you missed after that cursor before going live. Not allowed together with `maxUpdatesPerMinute`. See [Cursors and resuming](https://sonar-cf3aeac7.mintlify.site/websocket/cursors-and-resume.md).

If the subscribe fails, you get an `error` with the same `id`, and the `id` stays free to reuse (except for `DUPLICATE_SUBSCRIPTION`, where the `id` belongs to the existing subscription).

### unsubscribe

Stops a subscription. The `id` can be reused right away.

```json theme={null}
-> {"op":"unsubscribe","id":"weth-trades"}
<- {"type":"unsubscribed","id":"weth-trades"}
```

**op** `"unsubscribe"` *required*

Operation name.

**id** `string` *required*

The `id` of an active subscription. An unknown id returns `NOT_FOUND` with message `Unknown subscription <id>`.

### ping

Application-level ping. The server answers with `pong`.

```json theme={null}
-> {"op":"ping"}
<- {"type":"pong"}
```

This is optional. It does not count as a heartbeat answer, since only WebSocket protocol pongs do, and it counts toward the message rate limit. See [Heartbeats](https://sonar-cf3aeac7.mintlify.site/websocket/overview.md#heartbeats).

## Server messages

| `type` | Sent when |
| - | - |
| [`welcome`](#welcome) | Once, right after the connection opens |
| [`subscribed`](#subscribed) | A subscribe succeeded |
| [`unsubscribed`](#unsubscribed) | An unsubscribe succeeded |
| [`event`](#event) | A trade matched a subscription |
| [`cursor`](#cursor) | Periodic checkpoint of a subscription's position |
| [`gap`](#gap) | Blocks were skipped upstream and will not be delivered |
| [`error`](#error) | A message or subscription was rejected |
| [`reconnect`](#reconnect) | The server is about to restart |
| [`pong`](#pong) | Reply to `{"op":"ping"}` |

> [!NOTE]
>
> The protocol also defines a `snapshot` message type. It is reserved and is never sent on `/v1/ws`. Ignore unknown message types so that your client keeps working if new ones are added.

### welcome

```json theme={null}
<- {"type":"welcome","connectionId":"3f0c5a2e-6d0b-4b8e-9f43-2a1d7c9e8b11","heartbeatMs":30000}
```

**connectionId** `string`

Random UUID for this connection.

**heartbeatMs** `number`

Interval between the server's WebSocket protocol pings, in milliseconds.

### subscribed

```json theme={null}
<- {"type":"subscribed","id":"weth-trades","channel":"fast-trade","cursor":"38123456-0"}
```

**id** `string`

Your subscription id.

**channel** `string`

Channel name.

**cursor** `string`

The subscription's starting position. For a live subscription it is the current stream position, a whole-block cursor such as `38123456-0`. When you resume with `since`, it is the cursor you sent. **Not present for sampled subscriptions** (those with `maxUpdatesPerMinute`).

When you resume with `since`, replayed events follow this message. Live events follow once the replay has caught up.

### unsubscribed

```json theme={null}
<- {"type":"unsubscribed","id":"weth-trades"}
```

**id** `string`

The subscription that was removed.

### event

One matched trade. The shape of `data` depends on the channel.

```json theme={null}
<- {"type":"event","id":"weth-trades","channel":"fast-trade","cursor":"38123457-0:4","data":{"token":"0x4200000000000000000000000000000000000006","...":"..."}}
```

**id** `string`

Your subscription id.

**channel** `string`

Channel name.

**cursor** `string`

Position of this trade, `<block>-0:<index>`. Store it to resume later. **Not present for sampled subscriptions.**

**data** `object`

The payload:

| Channel | `data` |
| - | - |
| `fast-trade` | [Trade event](https://sonar-cf3aeac7.mintlify.site/websocket/channels/fast-trade.md#event-data) |
| `token-details` | Trade event plus `tokenData` ([details](https://sonar-cf3aeac7.mintlify.site/websocket/channels/token-details.md#event-data)) |
| `market-details` | Trade event plus `pairData` ([details](https://sonar-cf3aeac7.mintlify.site/websocket/channels/market-details.md#event-data)) |
| `ohlcv` | Current candle ([details](https://sonar-cf3aeac7.mintlify.site/websocket/channels/ohlcv.md#event-data)) |

Events for one subscription arrive in chain order: by block, then by position in the block.

### cursor

A checkpoint. It means the subscription has processed everything up to and including this block, even if nothing in it matched your parameters.

```json theme={null}
<- {"type":"cursor","id":"weth-trades","cursor":"38123610-0"}
```

**id** `string`

Your subscription id.

**cursor** `string`

Whole-block cursor, `<block>-0`.

Checkpoints are sent about every 30 seconds to each live, non-sampled subscription whose position has moved since its last event or checkpoint. Quiet subscriptions get them too, so their stored cursor doesn't go stale. See [Cursors and resuming](https://sonar-cf3aeac7.mintlify.site/websocket/cursors-and-resume.md#checkpoints).

### gap

Blocks were skipped upstream. Trades in these blocks were not, and will not be, delivered on this stream.

```json theme={null}
<- {"type":"gap","id":"weth-trades","channel":"fast-trade","fromBlock":38123457,"toBlock":38123459}
```

**id** `string`

Your subscription id.

**channel** `string`

Channel name.

**fromBlock** `number`

First skipped block, inclusive.

**toBlock** `number`

Last skipped block, inclusive.

Gaps are sent to sampled subscriptions too. See [Gaps](https://sonar-cf3aeac7.mintlify.site/websocket/cursors-and-resume.md#gaps) for how to backfill them.

### error

```json theme={null}
<- {"type":"error","id":"weth-trades","code":"INVALID_PARAMETERS","message":"token or pool is required","details":[{"field":"","message":"token or pool is required"}]}
```

**id** `string`

The subscription id the error belongs to. **Absent** when the message itself could not be parsed (invalid JSON, a binary frame, or an envelope that fails validation, such as a bad `id` or malformed `since`). In that case you can't tell which request failed, so validate `id` and `since` on your side before sending.

**code** `string`

Machine-readable code. See the [error table](https://sonar-cf3aeac7.mintlify.site/websocket/limits-and-errors.md#error-codes).

**message** `string`

Human-readable description. For validation errors, it lists each problem as `field: message`, separated by `; `.

**details** `array | object`

Present on some errors. Validation errors have an array of `{ "field": string, "message": string }`. For a subscribe whose `params` fail, `field` is the path inside `params` (for example `tokens.0`), or `""` for a rule that covers several fields. For an envelope error without `id`, `field` is the path inside your message (for example `id` or `since`). `RESUME_EXPIRED` has `{ "retainedFrom": "<block>" }`, and `Unknown pairs in since` has `{ "pairs": [...] }`.

Errors never close the connection. Connection-level problems are reported with close codes instead. See [Limits, close codes and errors](https://sonar-cf3aeac7.mintlify.site/websocket/limits-and-errors.md).

### reconnect

The server is shutting down. Your connection will be closed with code `1012` within about 20 seconds.

```json theme={null}
<- {"type":"reconnect"}
```

No fields. Open a new connection and resubscribe with `since` set to your latest cursors. See the [reconnect loop](https://sonar-cf3aeac7.mintlify.site/websocket/cursors-and-resume.md#reconnect-loop).

### pong

```json theme={null}
<- {"type":"pong"}
```

No fields. Reply to `{"op":"ping"}`.

## Sampling with maxUpdatesPerMinute

Every channel accepts an optional `maxUpdatesPerMinute` integer from 1 to 600. It turns the subscription into a **sampled** subscription, which limits how often you receive events.

* The minimum interval between events is `ceil(60000 / maxUpdatesPerMinute)` milliseconds. For example, `60` gives one event per second at most.

* When the interval has passed, the server sends the **first** matching trade and drops every other trade until the next interval. It does not send the latest trade, and it does not merge trades. On `ohlcv`, a matching trade that produces no event (see [ohlcv](https://sonar-cf3aeac7.mintlify.site/websocket/channels/ohlcv.md#how-candles-are-updated)) still uses up the interval.

* The interval applies separately to each followed item:

  | Channel | Interval applies per |
  | - | - |
  | `fast-trade` | Followed token (the pool's base token for a pool-only subscription) |
  | `token-details` | Token in your list |
  | `market-details` | Pool in your list |
  | `ohlcv` | Subscription |

* Sampled subscriptions have no cursors. `subscribed` and `event` messages carry no `cursor`, and no `cursor` checkpoints are sent.

* A sampled subscription cannot resume. Sending `since` together with `maxUpdatesPerMinute` fails with:

```json theme={null}
<- {"type":"error","id":"watchlist","code":"INVALID_PARAMETERS","message":"A sampled subscription drops items and cannot resume, subscribe without since or maxUpdatesPerMinute"}
```

Use sampling for display widgets that only need a periodic refresh. Don't use it when you need every trade, such as for alerts, trade tapes or anything you want to resume.


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