Realtime
The WebSocket that pushes Record changes, progress and site events to the desk.
The desk keeps one Phoenix WebSocket open per tab. The server pushes messages. Clients send nothing.
Connect
With a session cookie, call GET /api/realtime/token. It answers {token}. API keys cannot open a socket, since they have no session.
Open wss://<site>/api/realtime/websocket with the token as a WebSocket subprotocol. The Origin must be the site's own host.
Join topics. The server checks each join against your rights, and checks again on every rejoin.
@cardinal/sdk does this for you:
import { subscribe } from '@cardinal/sdk'
subscribe('rec:Sales Invoice/SINV-0001', {
message: (event, payload) => console.log(event, payload),
refetch: () => reloadForm(),
})Topics
| Topic | Needs | Carries |
|---|---|---|
rec:<rectype>/<name> | Read on the Record | Changes and renames of that Record, comment changes |
rectype:<rectype> | Select on the RecType | List changes |
user:<name> | Your own user only | Messages apps send to you through Cardinal.Realtime.publish |
task:<id> | The id, which only the code that started the task hands out | Progress of a long task, such as a bulk action |
tenant | Any session of the site | meta_changed, version_update and apps' <app>.<name> events |
A refused join answers {reason: "forbidden"}, or "unauthorized" once the session has ended.
Events
| Event | Sent when |
|---|---|
rec_update | A Record was saved |
rec_rename | A Record was renamed |
list_update | A Record of the RecType changed |
recinfo_update | A Comment on the Record changed |
progress | A task moved on |
meta_changed | A RecType or customization changed. Each desk refetches its boot after a random wait of up to 2 seconds |
version_update | A migrate ran on the site, failed ones included |
resync | The node may have missed messages. Refetch what you show |
Messages carry identifiers only, never field values. The client reads the Record again through the API, so permissions apply as usual.
How it reaches every node
The writing request calls pg_notify on the channel cardinal_realtime inside its transaction, so a rollback sends nothing.
Every web node holds one LISTEN cardinal_realtime connection and rebroadcasts each message to its own sockets on that topic.
If a node's listener loses its connection, or falls 10,000 messages behind, it listens again and pushes resync to every joined channel.
Logging a user out, disabling them or ending a session closes that session's sockets. When x-cardinal-perm changes on any API response, the sdk joins every topic again so the server checks the new rights.