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 server. Connect with its official library or read the HTTP stream with any HTTP client.
See Stream channels for available events, History and catch-up for recovery after a disconnection, and Source conditions for platform requirements and delays. The machine-readable description of the events is AsyncAPI 3.0: it needs no key.
Where to connect
Section titled “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. 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
Section titled “Check the connection from a terminal”Save the key in the DONATTY_TOKEN variable: how to do it. Then run:
curl -N -X POST "https://api.donatty.com/v1/realtime/uni_http_stream" \ -H "Authorization: Bearer $DONATTY_TOKEN" -H "Content-Type: application/json" -d '{}'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:
{"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
pubcontains an event inpub.data. - A line with
disconnectreports a rejected connection, for example{"disconnect":{"code":4501,"reason":"invalid_token"}}: what the code means.
To send a test donation, follow the first streamed donation quick start. For the event fields, see donation event formats.
Libraries
Section titled “Libraries”| Library | Where the key goes | Ready-made example |
|---|---|---|
| centrifuge-js — browser, OBS, Node.js | the headers option |
OBS overlay |
| centrifuge-python | the headers= parameter |
Python bot |
| centrifuge-go | the Config.Header field |
Go bot |
| no library — the HTTP stream | the request header | the terminal check |
The client libraries reconnect automatically and request missed events from channel history: what the history covers. The HTTP stream does not reconnect by itself: how to resume it where it stopped.
Resuming the HTTP stream
Section titled “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.
{"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; for REST catch-up requests, see the Stream and catch-up example.
Connection limit and replacing a key
Section titled “Connection limit and replacing a key”For connection limits, see API limits. For replacing a key and recovering events within the available history, see Keys and scopes.
Close codes
Section titled “Close codes”For close codes and how to handle each one, see the Stream close codes page.