# Centrifugo stream: WebSocket and HTTP

Donations, moderation decisions, replays, goal changes, Twitch and YouTube events arrive over an open connection: you don't need to poll the API. Events are delivered by a [Centrifugo](https://centrifugal.dev/) server. Connect with its official library or read the HTTP stream with any HTTP client.

See [Stream channels](https://developers.donatty.com/en/realtime/channels.md) for available events, [History and catch-up](https://developers.donatty.com/en/realtime/recovery.md) for recovery after a disconnection, and [Source conditions](https://developers.donatty.com/en/conditions/sources.md) for platform requirements and delays. The machine-readable description of the events is [AsyncAPI 3.0](https://api.donatty.com/v1/asyncapi.yaml): it needs no key.

## Where to connect

| Method | Address |
|---|---|
| WebSocket, for Centrifugo libraries | `wss://api.donatty.com/v1/realtime/websocket` |
| HTTP stream, a `POST` request | `https://api.donatty.com/v1/realtime/uni_http_stream` |

You need a secret key `dnt_pat_…`: [how to issue one](https://developers.donatty.com/en/tokens.md#issue-a-key). Pass it in the connection header `Authorization: Bearer …`. Keys in the URL or connection data are rejected. Public keys (`dnt_pk_…`) are not supported. A connection receives events only for the channel whose dashboard issued the key.

## Check the connection from a terminal

Save the key in the `DONATTY_TOKEN` variable: [how to do it](https://developers.donatty.com/en/tokens.md#save-the-key-in-a-variable). Then run:


**macOS and Linux:**

```sh
curl -N -X POST "https://api.donatty.com/v1/realtime/uni_http_stream" \
  -H "Authorization: Bearer $DONATTY_TOKEN" -H "Content-Type: application/json" -d '{}'
```
**Windows (PowerShell):**

```powershell
curl.exe -N -X POST "https://api.donatty.com/v1/realtime/uni_http_stream" `
  -H "Authorization: Bearer $env:DONATTY_TOKEN" -H "Content-Type: application/json" -d '{}'
```


The command reads the stream until you press Ctrl+C. Each line in the response is a JSON object:

```text
{"connect":{"client":"66179ade-…","version":"6.10.3 OSS","subs":{"donations:str_8c1f0e9d-…":{"recoverable":true,"epoch":"cmOZ","offset":41,"positioned":true}},"ping":25,"session":"3aaacfaa-…"}}
{}
{"channel":"donations:str_8c1f0e9d-…","pub":{"data":{"specversion":"1.0","id":"evt_1f0b7c52-…","type":"donation.created", …},"offset":42}}
```

- The first line confirms the connection and names the channels in `subs`.
- The server sends an empty object `{}` every 25 seconds: this is how it checks the connection.
- A line with `pub` contains an event in `pub.data`.
- A line with `disconnect` reports a rejected connection, for example `{"disconnect":{"code":4501,"reason":"invalid_token"}}`: [what the code means](https://developers.donatty.com/en/realtime/close-codes.md).

To send a test donation, follow the [first streamed donation](https://developers.donatty.com/en/quickstart/live.md) quick start. For the event fields, see [donation event formats](https://developers.donatty.com/en/structures/donation-events.md).

## Libraries

| Library | Where the key goes | Ready-made example |
|---|---|---|
| [centrifuge-js](https://github.com/centrifugal/centrifuge-js) — browser, OBS, Node.js | the `headers` option | [OBS overlay](https://developers.donatty.com/en/examples/realtime-obs.md) |
| [centrifuge-python](https://github.com/centrifugal/centrifuge-python) | the `headers=` parameter | [Python bot](https://developers.donatty.com/en/examples/realtime-python.md) |
| [centrifuge-go](https://github.com/centrifugal/centrifuge-go) | the `Config.Header` field | [Go bot](https://developers.donatty.com/en/examples/realtime-go.md) |
| no library — the HTTP stream | the request header | [the terminal check](#check-the-connection-from-a-terminal) |

The client libraries reconnect automatically and request missed events from channel history: [what the history covers](https://developers.donatty.com/en/realtime/recovery.md). The HTTP stream does not reconnect by itself: [how to resume it where it stopped](#resuming-the-http-stream).

> **Do not enable the sse transport in centrifuge-js.**
> The `sse` transport includes the key in the request URL. Use WebSocket with the endpoint in the table; it needs no extra setup.

## Resuming the HTTP stream

The HTTP stream does not reconnect by itself. Open it again and pass the position of the last event you received in the request body: the server sends what was missed from the channel history the same way it does for the libraries.

```json
{"subs": {"donations:str_<uuid>": {"recover": true, "epoch": "cmOZ", "offset": 42}}}
```

The first line of the response contains the channel's `epoch`. Each event contains its `offset`. Missed events come in the first line of the new response, in the channel's `publications` field, next to `recovered: true`. If it says `recovered: false`, the history did not cover the gap: [when that happens](https://developers.donatty.com/en/realtime/recovery.md#channel-history); for REST catch-up requests, see the [Stream and catch-up](https://developers.donatty.com/en/examples/stream-recovery.md) example.

## Connection limit and replacing a key

For connection limits, see [API limits](https://developers.donatty.com/en/rate-limits.md#stream-connections). For replacing a key and recovering events within the available history, see [Keys and scopes](https://developers.donatty.com/en/tokens.md#replace-a-key-in-an-open-stream).

## Close codes

For close codes and how to handle each one, see the [Stream close codes](https://developers.donatty.com/en/realtime/close-codes.md) page.
