M6-S2 — Real-time: keep a client live
Section titled “M6-S2 — Real-time: keep a client live”| Module | M6 — Integrations and live data |
| After this session you can | Keep an outside screen current without polling, and know exactly what the live channel does and does not promise. |
| Time | Facilitated: 39 min · On your own: 39 min |
| Before you start | M6-S1. Hala Mansour’s session in .tokens.json and your refresh.mjs (M5-S2). Ziad Karam waits at Job offer. Samir Khoury in a private window. |
| Go deeper | Real-time: live events and calls · Subscribing to events · Data model & events: socket messages |
◆ Orientation (1 min) — HR’s wall screen
Section titled “◆ Orientation (1 min) — HR’s wall screen”HR wants a screen on the office wall that shows applications moving as it happens (R-12). Polling the API every few seconds would be slow and wasteful. The platform pushes changes to connected clients instead: the same channel the portal uses to refresh an open record. In M5-S2 you decided the screen signs in as a person; now you connect it.
▤ Concept (5 min) — The live channel
Section titled “▤ Concept (5 min) — The live channel”Connecting
Section titled “Connecting”The live channel speaks Socket.IO version 4 (a plain WebSocket client cannot connect), on the tenant’s own host. The credential is an OAuth access token, the same one you use for HTTP, or an anonymous token on public pages. An API token cannot open the channel. The token is checked once, at the handshake: an open connection is not closed when the token expires, but every reconnect is a new handshake, so supply the token through a function that always returns the current one.
Subscribing
Section titled “Subscribing”A new connection receives nothing until it subscribes, and each subscription has an acknowledgement: wait for it, because some failed subscriptions report no error.
| You send | With | Acknowledgement | You then receive |
|---|---|---|---|
subscribeToNotifications | nothing: the token says who you are | subscribedToNotifications | The person’s notifications, and their jobs’ completions |
entries/subscribe-to-entry | { entityId, entryId } | subscribedToEntry | Changes to that record |
subscribeToJobExecution | { messageId } of a job you started | subscribedToJobExecution | The job’s completion |
What arrives
Section titled “What arrives”| Event | Means |
|---|---|
entries/value-updated | Field values changed; it may carry the record’s new updateIdentifier |
entries/workflow-updated | A step was reached or left, an action taken, an owner changed |
entries/comments-updated | A comment was added or changed |
entries/archived, entries/restored | The record was archived or restored |
notification | A notification for the signed-in person |
The events are the record-lifecycle and workflow events of M6-S1, delivered to whoever is subscribed.
What it promises, and what it does not
Section titled “What it promises, and what it does not”The channel is push-only and best-effort. Events are sent only while you are subscribed; nothing is kept for a disconnected client, and there is no replay. So after every connect or reconnect: subscribe again, then re-read over HTTP what you show. Writes and authoritative reads always go through the HTTP API. Notifications arrive from every space the person belongs to; a custom client filters them on the notification’s space.
sequenceDiagram participant S as Wall screen participant T as Tenant host S->>T: connect (access token) S->>T: entries/subscribe-to-entry T-->>S: subscribedToEntry (ack) T-->>S: entries/value-updated … Note over S,T: network drops, events are lost S->>T: reconnect (fresh token) S->>T: subscribe again S->>T: re-read the record over HTTP
What to notice: the re-read after reconnect is what makes a best-effort channel correct; the events only tell you when to look.
⛏ Lab (26 min) — The live board
Section titled “⛏ Lab (26 min) — The live board”Goal. A small program, signed in as Hala Mansour, that shows Ziad Karam’s application changing live and Hala’s notifications as they arrive, and that survives a reconnect.
Before you start. A terminal in your careers-client folder, with BASE set.
-
Install the client library:
npm install socket.io-client@4. -
Find the two ids: read Ziad Karam’s application as Hala through HR view:
Terminal window node refresh.mjscurl -s -G "$BASE/api/v1/v/$HRVIEW/v3/entities/job_application/entries" \-H "Authorization: Bearer $(jq -r .access_token .tokens.json)" \--data-urlencode 'search=Ziad' --data-urlencode 'filterCriteria={"limit":1}' \| jq -r '.entries[0] | "export ENTITY_ID=\(.dataEntity) ENTRY_ID=\(.id)"'Run the line it prints.
-
Create
live-board.mjs:import fs from 'node:fs';import { io } from 'socket.io-client';const { BASE, ENTITY_ID, ENTRY_ID } = process.env;const currentToken = () => JSON.parse(fs.readFileSync('.tokens.json', 'utf8')).access_token;const socket = io(BASE, {transports: ['websocket', 'polling'],auth: (cb) => cb({ token: currentToken() }), // a fresh token on every reconnect});socket.on('connect', () => {console.log('connected: subscribing (and a real board would re-read over HTTP now)');socket.emit('subscribeToNotifications');socket.emit('entries/subscribe-to-entry', { entityId: ENTITY_ID, entryId: ENTRY_ID });});socket.on('subscribedToNotifications', (a) => console.log('ack: notifications for', a.userId));socket.on('subscribedToEntry', (a) => console.log('ack: watching entry', a.entryId));socket.on('connect_error', (err) => console.error('refused:', err.message));socket.on('error', (e) => console.error('subscription error:', e.message));socket.on('disconnect', (reason) => console.log('disconnected:', reason));for (const event of ['entries/value-updated', 'entries/workflow-updated', 'entries/comments-updated', 'notification']) {socket.on(event, (payload) => {console.log(new Date().toISOString(), event, JSON.stringify(payload).slice(0, 160));});} -
Run
node live-board.mjs. You see connected and both acknowledgements. -
As Samir Khoury, open Ziad Karam at Job offer and change Base salary to 12500; save. The board prints
entries/value-updated, and a moment later another one when your Function’s reaction updates Offered gross. -
As Samir Khoury, attach an Offered contract and select Send job offer. The board prints
entries/workflow-updated. -
Turn your computer’s network off for ten seconds and on again. The board prints disconnected, then connected and both acknowledgements again.
-
Stop the board. Start it once more with your API token instead: replace
currentToken()withprocess.env.TOKEN. It prints refused: an API token cannot open the channel. Put the line back.
Check.
- Both subscriptions were acknowledged before you changed anything.
- A field change and a workflow step each arrived as their own event.
- The Function’s update arrived as a second value event, without you doing anything.
- After the network came back, the board resubscribed by itself.
- The API token was refused at the handshake.
Pass condition. The board shows Ziad Karam’s changes as they happen, recovers its subscriptions after a reconnect, and you can show that only an OAuth token opens it.
If something goes wrong
| What you see | What to do |
|---|---|
refused: Invalid token with Hala’s token | It expired: run node refresh.mjs and start again. |
| Connected, but no acknowledgement for the entry | An id is missing or wrong; the board would print subscription error. Repeat step 2. |
| Nothing arrives when Samir saves | You subscribed to a different record, or the save did not change a value. |
Tenant not resolved | BASE is not the tenant’s own host. |
Model answer
The board connects with Hala’s access token, supplied by a function so that every reconnect
handshake gets the current one, subscribes in its connect handler so that subscriptions are
restored after any reconnect, and waits for both acknowledgements. Samir’s save produced one
value event, the Function’s chained update a second, and the offer a workflow event. A real wall
screen would also re-read the record over HTTP in the connect handler, because anything that
happened while it was offline was not kept for it. The API token is refused at the handshake,
which is why M5-S2 gave the screen a person’s OAuth session.
◇ Worked scenario (1 min) — Monday morning’s frozen screen
Section titled “◇ Worked scenario (1 min) — Monday morning’s frozen screen”On Monday the wall screen still shows Friday’s figures, though it is connected and shows no error. Diagnose before reading on.
What happened. The office network restarted on Saturday. The program reconnected, as yours
does, but its subscriptions were made once, at start-up, not in the connect handler: the new
connection subscribed to nothing, so nothing arrived. Fix one: subscribe on every connect.
What it would still have missed. Even resubscribed, it would not know what changed while it was offline: the channel keeps nothing. Fix two: re-read the shown records over HTTP after every connect, then let events keep them current.
What would have kept it silent for a year. Its token was read once at start-up and cached; a reconnect after an hour would have been refused. Fix three: give the connection a function that returns the current token, and keep the session refreshed.
✓ Practice (5 min) — Check your understanding
Section titled “✓ Practice (5 min) — Check your understanding”A background service that calls the API with an API token also wants live updates. What does it need?
Check your answer
The correct answer is A. Open the explanation under each option to see why.
A client subscribes to a record, and the subscription silently does nothing. What should the client have done?
Check your answer
The correct answer is B. Open the explanation under each option to see why.
A board was offline for ten minutes. When it reconnects, what does it receive about those ten minutes?
Check your answer
The correct answer is C. Open the explanation under each option to see why.
An open live connection is closed when the access token used for its handshake expires.
Check your answer
The correct answer is B (False). Open the explanation under each option to see why.
↺ Recap (1 min) — After Module 6
Section titled “↺ Recap (1 min) — After Module 6”You connected a live client with the right credential, watched record and workflow events arrive, and designed it to recover from the channel’s limits. After Module 6 you can connect the suite to the outside world in both directions: outbound webhooks with a clear contract, and live clients that stay current.
Next. Module 7 secures everything you built: who sees what, what automations and integrations may reveal, and which credential each caller holds.