cardinal

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

TopicNeedsCarries
rec:<rectype>/<name>Read on the RecordChanges and renames of that Record, comment changes
rectype:<rectype>Select on the RecTypeList changes
user:<name>Your own user onlyMessages apps send to you through Cardinal.Realtime.publish
task:<id>The id, which only the code that started the task hands outProgress of a long task, such as a bulk action
tenantAny session of the sitemeta_changed, version_update and apps' <app>.<name> events

A refused join answers {reason: "forbidden"}, or "unauthorized" once the session has ended.

Events

EventSent when
rec_updateA Record was saved
rec_renameA Record was renamed
list_updateA Record of the RecType changed
recinfo_updateA Comment on the Record changed
progressA task moved on
meta_changedA RecType or customization changed. Each desk refetches its boot after a random wait of up to 2 seconds
version_updateA migrate ran on the site, failed ones included
resyncThe 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.

On this page