# History and catch-up

A connection can drop when the network fails, the server restarts or the computer sleeps. After reconnection, the server sends missed events if they are still in channel history. History is limited by both time and event count, so even a short disconnection may leave a gap. In that case, the server returns `recovered: false` and your program must catch up over REST.

## Channel history

The library reconnects automatically and sends the position of the last received event. If channel history covers the gap, the server delivers the missed events and returns `recovered: true`.

The history keeps events for 24 hours, but no more than the last 1000 in the donations, Twitch and YouTube channels and 100 in the goals channel. These are the maximum history limits. After the Centrifugo server restarts, history may be unavailable even for a shorter gap. When the history does not cover the gap, the server answers `recovered: false`.

The library passes the server's answer in the subscription event:

| Library | Event | Fields |
|---|---|---|
| centrifuge-js | `subscribed` | `wasRecovering`, `recovered` |
| centrifuge-python | `on_subscribed` | `was_recovering`, `recovered` |
| centrifuge-go | `OnSubscribed` | `WasRecovering`, `Recovered` |

If `wasRecovering` is `true` and `recovered` is `false`, the server could not fully recover events from the supplied position: catch up over REST. If both fields are `false`, the client did not request recovery. This happens on an initial connection and when a new client instance starts without a saved position; it does not prove that no events arrived while your program was away.

After a restart, supply the saved `epoch` and `offset` for each channel if your client supports restoring them. If the position was not saved or supplied, catch up over REST from your saved processing checkpoint regardless of these flags. If that checkpoint is also missing, choose a period for the initial load: the gap can no longer be determined automatically. Persist processed records and processing progress so that a failure cannot leave the position ahead of unprocessed events.

## How to read the gap

Wait a random time from 1 to 10 seconds before sending requests. This spreads out requests after an outage. Catch up on each channel your program uses, starting from the timestamps below:

| Channel | Request | Starting timestamp |
|---|---|---|
| `donations` | [Donations](https://developers.donatty.com/en/reference/donations.md) `GET /v1/donations` without the `moderation` filter | `since` is the earlier of two: the `createdAt` of the oldest donation that is still `PENDING`, and the `createdAt` of the newest donation you received minus a minute |
| `goals` | [Fundraising goals](https://developers.donatty.com/en/reference/goals.md) `GET /v1/goals` | not needed: the response is the current state of the goals |
| `twitch`, `youtube` | [Platform events](https://developers.donatty.com/en/reference/platform-events.md) `GET /v1/platform-events` with `order=asc` | `since` is the `recordedAt` of the last processed event minus a few minutes |

Starting earlier also returns records you have already processed. Skip them as described in [Repeats and order](#repeats-and-order). The response cannot include events Donatty did not receive from the platform. For gaps at the source, check the [source status](https://developers.donatty.com/en/sources.md#gaps-gapsince-and-unavailabletypes). Once the source is `READY` again, catch up on its events in the same way, with `since` a few minutes before `gapSince`. Show donations from the catch-up by the [Moderation and display](https://developers.donatty.com/en/structures/donation.md#moderation-and-display) rule.

The step-by-step algorithm with requests is in the [Stream and catch-up](https://developers.donatty.com/en/examples/stream-recovery.md) example.

## Repeats and order

- One event can arrive twice, for example when it is resent from the history. The event `id` does not change when it is resent: remember it and skip repeats.
- Events of one donation and of one goal arrive in the order of the changes.
- Match catch-up records with the ones from the stream by the record `id`, not the event `id`: donations by the donation `id` (`don_…`, in the event it is `data.id`), Twitch and YouTube events by the platform event `id` (`pevt_…`, in the stream it is `data.id`). The CloudEvents event itself has its own `id`.

## HTTP stream

To resume the HTTP stream from its last position, see [Resuming the HTTP stream](https://developers.donatty.com/en/realtime.md#resuming-the-http-stream). If the response says `recovered: false`, catch up over REST as described above; the example is in [Stream and catch-up](https://developers.donatty.com/en/examples/stream-recovery.md).
