Naalya Handbook

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.

PieceDoes
Centrifugo v6The broker, backed by Redis so history survives a restart. Client publish is off everywhere
RealtimeServiceMints 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 RealtimeProviderOne 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.

TokenLastsRouteProves
Connection1 hourGET /realtime/token (getRealtimeToken)Who is on the websocket
Subscription1 dayPOST /realtime/subscription-token (createRealtimeSubscriptionToken)They may join this channel, in this mode. Refresh must resend mode
Observer1 dayPOST /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.

KindPatternModesEvents
personalpersonal:user#<userId>Joined from the connection tokennotification, library-scan
school-staffstaff:school.<schoolId>participantinvalidate
campus-staffstaff:campus.<campusId>participant, observerNone (presence only)
admin-staffstaff:admin.<schoolId>participantNone
cbt-examcbt:exam.<examId>participant (students), observer (proctors)exam-status
cbt-exam-proctorcbt:exam.<examId>.proctorparticipant (staff)attempt-flag, attempt-progress
enquiryenquiry:<enquiryId>participantenquiry-reply, enquiry-typing
paired-devicedevice: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.

Doing the work

On this page