# Stream and catch-up

Use these steps to resume receiving events from the [Centrifugo stream](https://developers.donatty.com/en/realtime.md) after a disconnection. For recovery guarantees and limits, see [History and catch-up](https://developers.donatty.com/en/realtime/recovery.md). The key is in the `DONATTY_TOKEN` variable: [how to save it](https://developers.donatty.com/en/tokens.md#save-the-key-in-a-variable).

## 1. Remember what you processed

For each event you receive, store:

- the event `id`, to skip duplicates delivered from history;
- the `id` of the donation or the platform event (in the event it is `data.id`), to match it with the catch-up records;
- the data needed to choose where to start catch-up: the newest donation's `createdAt`, donations still marked `PENDING`, and the most recent Twitch and YouTube event's `recordedAt`.

To resume after a restart, persist this data outside process memory. To recover channel history, also save the channel `epoch` and the processed publication's `offset` if your client exposes them. Do not advance the saved position before processing succeeds.

## 2. After reconnecting, look at recovered

The library reconnects automatically and provides the server's response in the subscription event: [fields by library](https://developers.donatty.com/en/realtime/recovery.md#channel-history).

- `wasRecovering: true` and `recovered: true` — the server recovered the gap from the supplied position. No REST catch-up is needed for that gap; process the recovered events and discard duplicates.
- `wasRecovering: true` and `recovered: false`: history did not cover the gap. Catch up using the steps below.
- Both `false` — the client did not request recovery. After a restart without a position, catch up over REST from your saved processing checkpoint; if it is missing, choose an initial load period. These flags do not prove that there is no gap.

The HTTP stream does not reconnect by itself: open it again with `epoch` and `offset` ([how](https://developers.donatty.com/en/realtime.md#resuming-the-http-stream)) and look at `recovered` in the first line of the response.

## 3. Wait and read each channel

Wait a random time from 1 to 10 seconds, then catch up on each channel your program uses. Choose a starting timestamp from the [How to read the gap](https://developers.donatty.com/en/realtime/recovery.md#how-to-read-the-gap) table and replace the date in the commands with it.

Donations, without the `moderation` filter:


**macOS and Linux:**

```sh
curl -s "https://api.donatty.com/v1/donations?limit=100&since=2026-11-09T10:00:00Z" -H "Authorization: Bearer $DONATTY_TOKEN"
```
**Windows (PowerShell):**

```powershell
curl.exe -s "https://api.donatty.com/v1/donations?limit=100&since=2026-11-09T10:00:00Z" -H "Authorization: Bearer $env:DONATTY_TOKEN"
```


For goals, use [Fundraising goals](https://developers.donatty.com/en/reference/goals.md) to read their current state. No starting timestamp is needed.

Twitch and YouTube events, oldest first, `order=asc`:


**macOS and Linux:**

```sh
curl -s "https://api.donatty.com/v1/platform-events?order=asc&since=2026-11-09T10:00:00Z" -H "Authorization: Bearer $DONATTY_TOKEN"
```
**Windows (PowerShell):**

```powershell
curl.exe -s "https://api.donatty.com/v1/platform-events?order=asc&since=2026-11-09T10:00:00Z" -H "Authorization: Bearer $env:DONATTY_TOKEN"
```


## 4. Follow the pages and drop repeats

While `nextCursor` is not `null`, request the next page with `cursor=<nextCursor>` and the same parameters. For platform events, keep `order=asc`: a cursor from a different order will not work. An `order=asc` pass includes events recorded up to the first page request. Later events arrive through the stream or in the next pass.

Skip records whose `id` you have already processed: donations by the donation `id`, Twitch and YouTube events by the platform event `id` (`pevt_…`). Show donations by the [Moderation and display](https://developers.donatty.com/en/structures/donation.md#moderation-and-display) rule. Update the starting timestamps for the next catch-up only after reading all pages successfully.

## 5. Read the gaps of the sources

REST cannot return events that Donatty did not receive from the platform. These gaps appear in the [source status](https://developers.donatty.com/en/sources.md#gaps-gapsince-and-unavailabletypes). Once the source is `READY` again, read its events the same way, with `since` a few minutes before `gapSince`.

## Limitations

- State kept only in memory is lost on restart. Without saved processed `id`s, a catch-up checkpoint and the channel position, automatic reconnection cannot restore the program's progress: events may be shown again or missed.
- The catch-up returns only what Donatty received: [source conditions](https://developers.donatty.com/en/conditions/sources.md).

For programs that implement these steps for donations, see [Streaming donations: Python](https://developers.donatty.com/en/examples/realtime-python.md), [Streaming donations: Go](https://developers.donatty.com/en/examples/realtime-go.md) and the [OBS donation overlay](https://developers.donatty.com/en/examples/realtime-obs.md).
