How realtime works
Centrifugo, connection and subscription tokens, and the channel registry. The map to read before you add a channel or an event.
Some screens shouldn't wait for a refresh: a department created in another tab, a student's exam answer, a guardian's enquiry reply. The API publishes a small event to Centrifugo (a websocket broker) and the Hub applies it. The server is the only writer; clients never publish. This is separate from Rover's chat stream, which is SSE.
| Piece | Does |
|---|---|
| Centrifugo v6 | The broker, backed by Redis so history survives a restart. Client publish is off everywhere |
RealtimeService | Mints tokens and runs publishTo. Product code never calls Centrifugo directly |
REALTIME_CHANNELS (libs/shared/src/realtime/channel-registry.ts) | Each channel kind's name pattern, modes and allowed events. publishTo throws on an event the kind doesn't allow |
Hub RealtimeProvider | One client. useRealtimeChannel holds a channel while a component is mounted |
Tokens
The Hub swaps its normal JWT for Centrifugo tokens. There's no connect proxy.
| Token | Lasts | Route | Proves |
|---|---|---|---|
| Connection | 1 hour | GET /realtime/token (getRealtimeToken) | Who is on the websocket |
| Subscription | 1 day | POST /realtime/subscription-token (createRealtimeSubscriptionToken) | They may join this channel, in this mode. Refresh must resend mode |
| Observer | 1 day | POST /realtime/observer-tokens (createRealtimeObserverTokens) | One request for every campus in the school. School-level staff only; observers don't appear in presence |
The lifetimes come from the expiresIn the API signs; the Centrifugo config sets no token lifetime. Staff presence isn't a token: screens ask POST /realtime/online (listOnlineStaff). See Using presence.
Channels
In each pattern, a name in angle brackets, such as <campusId>, stands for that id.
| Kind | Pattern | Modes | Events |
|---|---|---|---|
personal | personal:user#<userId> | Joined from the connection token | notification, library-scan |
school-staff | staff:school.<schoolId> | participant | invalidate |
campus-staff | staff:campus.<campusId> | participant, observer | None (presence only) |
admin-staff | staff:admin.<schoolId> | participant | None |
cbt-exam | cbt:exam.<examId> | participant (students), observer (proctors) | exam-status |
cbt-exam-proctor | cbt:exam.<examId>.proctor | participant (staff) | attempt-flag, attempt-progress |
enquiry | enquiry:<enquiryId> | participant | enquiry-reply, enquiry-typing |
paired-device | device:pair.<pairingId> | From the device session (see Device pairing) | device-command, device-command-cancelled, unpaired |
Never put cache invalidation on personal: someone on several channels would get it many times. Invalidation lives on school-staff only. A notification with all-null fields means the user's read state changed elsewhere.
Channel names are opaque to the Hub: grants return { channel, token }, plus campusId on observer grants so the client can key a map without parsing names.
Events
Every publication is { type, data }, where type is a key of RealtimeEventMap. Clients ignore unknown types, so adding an event doesn't break open tabs.
Payload types don't go through Swagger. They travel on the Chowbea type bus from realtime.chowbea.ts into the Hub's _generated/bus. An event not exported on the bus leaves the Hub typing a guess.