# Streams

Live updates over server-sent events, and how to resume a stream after a disconnect.

Routes under `/v2/stream/` are server-sent event (SSE) streams.
Each event has an `event:` line and a `data:` line holding one JSON object.
Most `data` objects are the same envelope the REST routes answer with.
A `: keepalive` comment line arrives every 20 seconds.
Send the key in the `Authorization` header, as on every other route.

## The streams

| Stream | Events | Resume |
|---|---|---|
| `GET /v2/stream/token/{chain_id}/{address}/replay` | `attached`, then one `frame` per trade | `Last-Event-ID`, `from_block`, `from_seq` |
| `GET /v2/stream/token/{chain_id}/{address}/liquidity/replay` | `attached`, then one `frame` per liquidity change | `Last-Event-ID`, `from_block`, `from_seq` |
| `GET /v2/stream/pilot/trenches/{chain_id}` | `snapshot`, then `card`, `update`, `remove` | `Last-Event-ID` |
| `GET /v2/stream/token/{chain_id}/{address}/ohlcv?interval=` | `attached`, then one `frame` per changed candle | none |
| `GET /v2/stream/token/{chain_id}/{address}` | `attached`, then one `frame` per trade | none |

The plain token stream may be closed at the public edge.
Use `/replay` for trades: it carries the same trades and can resume.

## Resume

Resumable streams send an `id:` line with their events.
Keep the last `id` you received.
On reconnect, send it in the `Last-Event-ID` header.
The stream then sends what you missed and continues live.
Browser `EventSource` clients do this on their own.

The replay streams can also start from history:

- `from_block=<height>` starts at a block height (a slot on Solana).
- `from_seq=<n>` starts after a stream sequence.
- Send at most one of them. `Last-Event-ID` wins over both.
- Without any of them, the stream is live only.
- A `from_seq` that is no longer held answers `410`; resume with `from_block`.

The launches stream resumes with `Last-Event-ID` only.
An id it no longer holds gets a fresh `snapshot` instead of an error.

```sh
curl -N -H "Authorization: $CHAINMETRIC_KEY" -H "Last-Event-ID: 26527763.112.450.0" \
  https://api.chainmetric.io/v2/stream/token/1/c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2/replay
```

## Fast and safe data

A frame's `lane` is `fast` when it was published as soon as its block was seen.
The same data is published again with `lane` `safe` once the block is final, and replaces the fast copy.
`commitment` says how settled a frame is: `processed` (Solana fast), `confirmed` (EVM fast) or `finalized`.
A frame that a chain reorganisation withdrew arrives again with `revoked: true`.
If you want final data only, keep frames whose `commitment` is `finalized`.

## Launch events

The launches stream's `snapshot` is `{"views": {"new": [...], "curves": [...], "graduated": [...], "pairs": [...]}}`.
The stream calls the launches view `new`; the REST route calls it `launches`.
`card` and `update` carry `{views, card}`.
`remove` carries `{views, pool}`.

## When a stream is full

A stream that has no free slot answers `503` with `Retry-After: 5`.
Wait that long and reconnect.
