> ## 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.

# Subscribing

> Subscribe, unsubscribe and ping on the multi-events stream: params, pairs, the subscribed reply, the event envelope and sampling.

All client messages are JSON text frames with an `op` field. The multi-events stream accepts three ops: `subscribe`, `unsubscribe` and `ping`.

## `subscribe`

```json theme={null}
{
  "op": "subscribe",
  "id": "big-trades",
  "channel": "multi-events",
  "params": {
    "chains": ["evm:1", "evm:8453"],
    "events": ["swap"],
    "filters": { "gte": ["tokenAmountUsd", 100000] }
  }
}
```

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

The operation.

**id** `string` *required*

A subscription id you choose: 1 to 64 characters made of letters, digits, `_`, `.`, `:` and `-`. It must be unique among the active subscriptions on this connection. Every message about this subscription carries the same `id`.

**channel** `string` *required*

Always `"multi-events"`. Any other value returns `UNKNOWN_CHANNEL`.

**params** `object` *required*

What to stream. See [Params](#params).

**since** `string | object`

Optional resume position: a cursor string when the subscription has one pair, or an object mapping pairs to cursors when it has several. See [Cursors and resuming](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md).

> [!WARNING]
>
> Put `filters` inside `params`. Unknown keys at the top level of the message are silently ignored, so a `filters` key next to `params` has no effect and you receive every trade on the chain.

### Params

`params` is a strict object: any key other than the four below is rejected with `INVALID_PARAMETERS` (for example a misspelled `filter` or `chain`).

**chains** `string[]` *required*

One or more chain identifiers in the form `evm:<chainId>`, for example `"evm:1"` or `"evm:8453"`. Each chain may appear only once. Bare numbers and chain names are rejected with the message `Invalid blockchain ID. Expected format: "evm:<chainId>" (e.g. "evm:1", "evm:137")`. See [Chains](https://sonar-cf3aeac7.mintlify.site/streams/overview.md#chains).

**events** `string[]` *required*

One or more event kinds, each at most once:

* `swap`: the trade as indexed and priced.
* `swap-enriched`: the same trade plus `baseTokenData` and `quoteTokenData`.

See [Event payloads](https://sonar-cf3aeac7.mintlify.site/streams/event-payloads.md).

**filters** `object`

Optional. Either a single filter that applies to every requested event kind, or a map from event kind to filter. Without `filters`, every trade on the requested chains is delivered.

```jsonc theme={null}
// One filter for every kind
"filters": { "gte": ["tokenAmountUsd", 100000] }

// One filter per kind
"filters": {
  "swap": { "eq": ["sender", "0xd0b53d9277642d899df5c87a3966a349a798f224"] },
  "swap-enriched": { "gte": ["tokenAmountUsd", 100000] }
}
```

In the map form, every kind listed in `events` needs an entry (`filters.<kind>: every requested event needs a filter`), and a key that is not in `events` is rejected (`filters.<key>: not in events`). You can't leave one kind unfiltered in a map; use two subscriptions instead. See [Filters](https://sonar-cf3aeac7.mintlify.site/streams/filters.md).

**maxUpdatesPerMinute** `integer`

Optional, 1 to 600. Turns the subscription into a sampled one that drops events. See [Sampling](#sampling).

### Pairs

The server splits a subscription into **pairs**, one per chain and event kind. A pair is named `<event>|<chain>`:

```json theme={null}
"params": { "chains": ["evm:1", "evm:8453"], "events": ["swap", "swap-enriched"] }
```

produces four pairs: `swap|evm:1`, `swap-enriched|evm:1`, `swap|evm:8453` and `swap-enriched|evm:8453`.

Each pair has its own cursor and its own [cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md). Within a pair, events arrive in block order, then in the order of the trades in the block. There is no ordering between pairs.

### The `subscribed` reply

When the subscription is accepted, the server replies with `subscribed`. Its shape depends on the number of pairs.

```json One pair theme={null}
{
  "type": "subscribed",
  "id": "weth-whales",
  "channel": "multi-events",
  "cursor": "26745119-0"
}
```

```json Several pairs theme={null}
{
  "type": "subscribed",
  "id": "big-trades",
  "channel": "multi-events",
  "cursors": {
    "swap|evm:1": "26745119-0",
    "swap|evm:8453": "55410322-0"
  }
}
```

```json Sampled theme={null}
{
  "type": "subscribed",
  "id": "sample",
  "channel": "multi-events"
}
```

* For a pair that starts live, the cursor is the current head of that chain's stream. It can be `"0-0"` if the chain has no data yet.
* For a pair you resume with `since`, it echoes the cursor you sent.
* Sampled subscriptions get neither `cursor` nor `cursors`.

`subscribed` is always sent before any event of the subscription, including replayed events.

### The event envelope

Every matching trade arrives in an `event` message:

```json theme={null}
{
  "type": "event",
  "id": "big-trades",
  "channel": "multi-events",
  "pair": "swap|evm:8453",
  "cursor": "55410325-0:7",
  "data": { "event": "swap", "chainId": "evm:8453", "...": "..." }
}
```

**type** `"event"`

Message type.

**id** `string`

Your subscription id.

**channel** `"multi-events"`

Channel name.

**pair** `string`

The pair this event belongs to, for example `swap|evm:8453`. **Present only when the subscription has more than one pair.** With a single pair, it is left out.

**cursor** `string`

Position of this trade in the pair's stream, for example `55410325-0:7`. Store it to [resume](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md). Left out on sampled subscriptions.

**data** `object`

The trade. See [Event payloads](https://sonar-cf3aeac7.mintlify.site/streams/event-payloads.md).

Besides events, a subscription can receive `cursor` checkpoints and `gap` messages. Both carry `pair` under the same rule. See [Cursors and resuming](https://sonar-cf3aeac7.mintlify.site/streams/cursors-and-resume.md).

## Sampling

Set `maxUpdatesPerMinute` to cap how often events are sent, for example for a live ticker that doesn't need every trade.

* The server keeps a minimum interval of `ceil(60000 / maxUpdatesPerMinute)` milliseconds between events. A matching trade that arrives before the interval has passed is **dropped**, not queued. In practice you get the first matching trade after each interval.
* The cap applies **per pair**, not per subscription. A subscription with 2 chains and 2 event kinds at `maxUpdatesPerMinute: 60` can deliver up to 240 events a minute.
* Sampled events have no `cursor`, `subscribed` has no cursors, and no `cursor` checkpoints are sent.
* A sampled subscription can't be resumed. Sending `since` together with `maxUpdatesPerMinute` fails with `INVALID_PARAMETERS`: `A sampled subscription drops items and cannot resume, subscribe without since or maxUpdatesPerMinute`.
* `gap` messages are still sent to sampled subscriptions.

```text theme={null}
→ {"op":"subscribe","id":"sample","channel":"multi-events","params":{"chains":["evm:8453"],"events":["swap"],"maxUpdatesPerMinute":60}}
← {"type":"subscribed","id":"sample","channel":"multi-events"}
← {"type":"event","id":"sample","channel":"multi-events","data":{"event":"swap",...}}
```

Sampling doesn't change the [cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md) of a subscription.

## `unsubscribe`

```json theme={null}
{ "op": "unsubscribe", "id": "big-trades" }
```

The server replies:

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

The id can then be reused. Unsubscribing an id that isn't active returns:

```json theme={null}
{ "type": "error", "id": "big-trades", "code": "NOT_FOUND", "message": "Unknown subscription big-trades" }
```

Subscriptions don't survive a disconnect. When the socket closes, all its subscriptions are removed, and you need to subscribe again on the new connection.

## `ping`

```json theme={null}
{ "op": "ping" }
```

The server replies `{ "type": "pong" }`. Use it to measure round-trip time. It is separate from the WebSocket protocol heartbeat: an application `ping` doesn't keep the connection alive, and it counts toward the [message rate limit](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md#connection-limits).

## Errors

Errors use this shape:

```json theme={null}
{ "type": "error", "id": "big-trades", "code": "INVALID_PARAMETERS", "message": "chains.0: Invalid blockchain ID. Expected format: \"evm:<chainId>\" (e.g. \"evm:1\", \"evm:137\")", "details": [{ "field": "chains.0", "message": "Invalid blockchain ID. Expected format: \"evm:<chainId>\" (e.g. \"evm:1\", \"evm:137\")" }] }
```

* `id` is the subscription the error is about. It is missing when the message itself couldn't be read, for example invalid JSON, an unknown `op`, or a malformed `id` or `since`.
* For parameter errors, `details` lists each problem as `{ field, message }`, where `field` is the dotted path inside `params` (for example `chains.0` or `filters.and.0.gte.0`), and `message` joins them as `field: message` separated by `; `. An unknown key in `params` has an empty `field`, for example `{ "field": "", "message": "Unrecognized key: \"filter\"" }`.
* After any subscribe error except `DUPLICATE_SUBSCRIPTION`, the id is free to use again.

Common subscribe errors:

| Code | Message | Cause |
| - | - | - |
| `INVALID_PARAMETERS` | Field-level messages | Bad `chains`, `events`, `filters`, `maxUpdatesPerMinute`, or an unknown key in `params` |
| `INVALID_PARAMETERS` | `This subscription has several pairs, since must map each pair to its cursor` | `since` is a string but the subscription has several pairs |
| `INVALID_PARAMETERS` | `Unknown pairs in since` | `since` names a pair this subscription doesn't have |
| `UNKNOWN_CHANNEL` | `Unknown channel: <name>` | `channel` isn't `multi-events` |
| `DUPLICATE_SUBSCRIPTION` | `Subscription <id> already exists` | The id is already used on this connection |
| `SUBSCRIPTION_LIMIT` | `At most 100 subscriptions, each entry in a list counts as one` | The connection's cost budget would be exceeded. See [Limits and subscription cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md) |
| `RESUME_EXPIRED` | `Cursor is older than the stream history` | A `since` cursor is older than the stream keeps |

The full list, including connection-level errors, is in [Limits and subscription cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md#error-codes).


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