# Realtime

How the admin and the api talk over a socket: what a connection has to prove,
which rooms exist and who may join them, how presence is derived, and what
happens when a session is revoked. Specified by [ARCHITECTURE
§8](https://github.com/Docker-Hunterpedia/Helpdock/blob/main/docs/planning/ARCHITECTURE.md#8-channel-adapters) and
[§12](https://github.com/Docker-Hunterpedia/Helpdock/blob/main/docs/planning/ARCHITECTURE.md#12-widget), and by [DOMAIN-RULES
§1.4](https://github.com/Docker-Hunterpedia/Helpdock/blob/main/docs/planning/DOMAIN-RULES.md#14-workers-and-websockets),
[§7](https://github.com/Docker-Hunterpedia/Helpdock/blob/main/docs/planning/DOMAIN-RULES.md#7-realtime-delivery-contract) and
[§12](https://github.com/Docker-Hunterpedia/Helpdock/blob/main/docs/planning/DOMAIN-RULES.md#12-staff-lifecycle). Implemented by M0-13.

The widget's own namespace, `/widget`, is described with the rest of the
widget protocol in [widget-protocol.md](https://helpdock.oday-bakkour.com/docs/widget-protocol/); what the two share
is below.

## The one rule

> Socket.IO does not guarantee delivery. The REST API is the source of truth;
> sockets are notifications. — DOMAIN-RULES §7

Nothing in Helpdock may depend on a socket frame arriving. A screen reads its
state over REST and applies events to it; a send is a `POST` whose response
carries the `seq`. A socket that never connects makes the admin slower to
notice things, never wrong about them.

## The server

One Socket.IO server, attached to the same HTTP server Fastify listens on, with
four settings that are not the defaults:

| Setting | Value | Why |
|---|---|---|
| `path` | `/socket.io` | What Caddy proxies and what the client asks for. |
| `transports` | `['websocket']` | No long-polling (ARCHITECTURE §12). Polling would also make sticky sessions a deployment requirement. |
| origin check | per namespace (below) | A WebSocket handshake is not covered by the same-origin policy, so a page anywhere could otherwise open one. The engine cannot tell which namespace a connection is for — that arrives in the first packet, after the upgrade — so each namespace's handshake middleware checks `Origin` itself: `/staff` accepts `APP_URL`, or no `Origin` at all (a server-to-server client, not a page); `/widget` accepts the brand's allowed origins (M4-03). For `/staff` it is defence in depth rather than the control: the credential is the bearer token in `auth.token` and never a cookie, so a cross-origin page has nothing to present. |
| `adapter` | Redis, two connections | A room has to mean "everyone, on every replica". A subscribed ioredis client accepts no other command, so the publisher cannot be the subscriber. |

It lives in `apps/api/src/realtime/redis-io.adapter.ts` and is installed on the
application before `init()`, because that is when Nest binds gateways to
whatever adapter is in force.

## Namespaces

| Namespace | Principal | Milestone |
|---|---|---|
| `/staff` | `staff` | M0-13 |
| `/widget` | `visitor` | M4-04 |

`/staff` refuses anything but a staff principal, and a page on any origin but
`APP_URL`. `/widget` (M4-04) is its own gateway with its own credential (the
visitor secret of DOMAIN-RULES §4.1), its own origin allow-list and its own
Redis channel, `helpdock:realtime:widget`; the server and the adapter are
shared. Two things cross between them: an agent's `ticket:viewing` with
`activity: 'replying'` becomes the visitor's typing indicator, and a visitor's
typing and read receipts reach `ticket:<id>` as `ticket:visitor_typing` and
`ticket:visitor_read`.

## The handshake

A socket authenticates exactly as a request does (DOMAIN-RULES §1.4). The
credential is the same ten-minute access token, presented in `auth.token`
because a browser cannot set headers on a WebSocket handshake:

```ts
io('/staff', { path: '/socket.io', transports: ['websocket'], auth: { token } });
```

The server verifies it with `SessionPrincipalResolver` — the same signature
check, the same claim schema, the same Redis revocation lookup as HTTP. There is
no second way in.

A refusal is a `connect_error` carrying `{ code, message }`, never a silent
hang:

| `code` | Meaning |
|---|---|
| `unauthenticated` | No `auth.token`, or one this install did not sign, or a session that has been revoked. |
| `forbidden` | A valid token for a principal that does not belong on this namespace. |
| `internal` | The handshake could not be checked at all. |

The accepted socket carries the `Principal`, the session id (`sid`) and the
family id (`fam`).

## Rooms

A room is `<kind>:<uuid>`. Joining one "runs the same permission check as the
corresponding REST read" (DOMAIN-RULES §1.4), which here means two checks, in
this order:

1. **The permission**, by the global `PermissionGuard`. `@SubscribeMessage`
   handlers declare `@Requires(...)` or `@Authenticated()` exactly as routes do,
   and `pnpm check:routes` fails on one that declares neither. The brand comes
   from the message rather than from a path, so every brand-scoped event names
   a `brandId`.
2. **The scope**, by `authorizeRoom`. This is the part Postgres does for HTTP
   (DOMAIN-RULES §1.3 layer 3) and that no row-level security policy can do for
   a room name.

| Room | Rule |
|---|---|
| `brand:<id>` | The principal holds a role in that brand, and the room is the brand the message named. |
| `department:<id>` | An explicit department list containing the id needs no query: the list is itself per-brand, so membership proves both the brand and the scope. An *unrestricted* scope (`all`) does need one — a room name carries no brand, and `all` means "every department *of that brand*" — so `departments` is read inside a transaction scoped to the brand the join named. |
| `ticket:<id>` | The ticket exists inside the principal's own scope. The check is the *same* question `GET /api/brands/:brandId/tickets/:ticketId` asks, asked the same way: a transaction carrying the principal's brand and departments, and the policies answering. "No such ticket" and "not in your departments" are therefore one answer, and neither confirms the other. |
| `user:<id>` | M3-07. That person alone: `id` is the principal's own. Every socket is put in its own on connect, so a client never has to ask; an explicit join of somebody else's is refused. The bell hears `notification:created` and `notification:read` here, whichever brand or screen the socket is on. |

`department:` and `ticket:` also re-ask the permission the *route* they mirror
declares, `ticket:read`, rather than resting on the `brand:read` the join itself
is declared with. Every role that holds one holds the other today; asking anyway
is what stops the next role from being able to subscribe to traffic it cannot
fetch.

The two that need a read go through `RoomScopeReader`
(`apps/api/src/realtime/room-reader.ts`). It is the only place in the app that
opens a tenant transaction outside the request lifecycle for a staff principal,
which is why it is one file with two reads in it.

Every event — not only a join — re-asks two things first, because a socket
outlives the token it was opened with:

1. **Has that token expired?** A socket's authority *is* its access token, and
   DOMAIN-RULES §1.6 caps that at ten minutes. Past `exp` the socket is closed
   and the client reconnects with a fresh one, which nobody sees.
2. **Has the session been revoked?** The revocation marker itself lives only as
   long as an access token can, which is why the expiry check comes first.

## Events

Client to server, all acknowledged:

| Event | Payload | Declares | Acknowledges |
|---|---|---|---|
| `room:join` | `{ brandId, room }` | `@Requires('brand:read')` | `{ ok: true, data: { room } }` |
| `room:leave` | `{ room }` | `@Authenticated()` | `{ ok: true, data: { room } }` |
| `presence:set` | `{ brandId, status: 'online' \| 'away' }` | `@Requires('brand:read')` | `{ ok: true, data: { status } }` |
| `presence:active` | `{ brandId }` — M14-18; nothing about what was done | `@Requires('brand:read')` | `{ ok: true, data: { status, idleEnded } }` |
| `presence:heartbeat` | `{}` | `@Authenticated()` | `{ ok: true, data: { at } }` |
| `ticket:viewing` | `{ brandId, ticketId, activity? }` — `activity` is `viewing` (the default) or `replying` | `@Requires('ticket:read')` | `{ ok: true, data: { ticketId } }` |

A refusal is `{ ok: false, error: { code, message } }` on the same
acknowledgement, with the codes above plus `invalid_payload`,
`session_revoked` and `rate_limited`.

### Event budgets

`room:join`, `presence:set` and `presence:heartbeat` each cost Redis work, so
each is limited per person, across every socket and every replica, with the
same sliding-window counter in Redis that the sign-in form uses
(`apps/api/src/realtime/socket-rate-limit.ts`). An event over its budget is
answered `rate_limited` and the socket stays open; the budget is counted before
the message is parsed, so malformed messages spend it too.

| Event | Per person, per minute |
|---|---|
| `room:join` | 120 |
| `presence:set` | 30 |
| `presence:heartbeat` | 60 (twenty-five open tabs at one every 25 s) |

Server to client:

| Event | Payload | Envelope `seq` |
|---|---|---|
| `presence:changed` | `{ userId, brandId, status }` | `null` |
| `ticket:changed` | `{ brandId, ticketId, departmentId, event }` where `event` is `ticket.created` or `ticket.updated` | `null` |
| `ticket:message` | `{ brandId, ticketId, departmentId, messageId, seq, kind, event }` where `event` is `ticket.replied` or `ticket.note_added` | the message's `seq` |
| `ticket:viewing` | `{ brandId, ticketId, activity, userId }` — relayed to the *rest* of `ticket:<id>` | `null` |
| `notification:created` | `{ brandId, notificationId }`, to `user:<id>` only (M3-07). Ids, no content: the bell re-reads its list over REST, and a client looking at another brand ignores it | `null` |
| `notification:read` | `{ brandId, notificationId }`, to `user:<id>` only (M14-10). Read-state invalidation: refreshes the bell for this brand, never announces a new notification or triggers a new-notification sound | `null` |
| `revoked` | `{ code: 'session_revoked', message }`, immediately before the socket is closed | — |

The two ticket events carry **ids and no content**. That is what "the REST API
is the source of truth; sockets are notifications" means in practice: a screen
re-reads the ticket rather than patching it from a frame, and an internal note
has no body in the payload to leak. `departmentId` is the department the ticket
is in *now*, so a client holding a `department:` room can tell whether the
ticket has just arrived in it or just left it.

Each one reaches `ticket:<id>` — whoever has the ticket open — and
`department:<id>`, because a new ticket has to appear in a list nobody was
looking at. A ticket that moves department reaches the room it has *left* too,
and the server then turns every socket out of `ticket:<id>`: a room is
authorised when it is joined, and who may read that ticket has just changed.
The clients re-join and are authorised again, which nobody sees.

`ticket:viewing` is the one event that travels in both directions, and the only
one a client originates. It exists because **a room is not a membership list**:
Socket.IO can enumerate the sockets in `ticket:<id>` that *this replica* is
holding, and "is anybody else looking at this" has to be true across every
replica. So the clients say so instead — every
`TICKET_VIEWING_INTERVAL_MS` while a ticket is open — the gateway authorises
each announcement exactly as it authorises the join and relays it to the rest of
the room, and each client drops a name nobody has repeated inside
`TICKET_VIEWING_TTL_MS`. Nothing is stored anywhere, which is what makes
"closed the tab", "lost the network" and "went to lunch with it open" one
answer. It carries ids and no content, like the other two ticket events.

M1-09 added `activity`, so the pill can say "is replying" as well as "is
viewing": a client announces `replying` the moment its composer holds something
unsent and `viewing` the moment it empties, as well as on the interval. It is
one more word in the same announcement, authorised the same way, and still not
stored — "is replying" is a warning, not a lock.

Every event emitted through `RealtimePublisher` travels in an envelope.
`revoked` is the exception: the revocation subscriber sends it bare, because it
is the last thing a socket ever hears and there is nothing to catch up on.

```ts
{ seq: number | null, at: string, data: T }
```

`seq` is the per-conversation cursor of DOMAIN-RULES §7. A client that receives
`seq > last_seq + 1` has missed something and catches up over REST. Ephemeral
events — presence, typing, queue position — are never replayed and carry
`null`, which is how a client tells the two apart without a table of event
names. `at` is when the server emitted it.

The names, the payload schemas and the envelope are declared once in
`packages/schemas/src/realtime.ts` and used by both sides. Every payload is
parsed on the way out, so a field nobody meant to send cannot leak.

## Presence

`online | away | offline`, derived rather than stored (DOMAIN-RULES §12). The
sockets decide whether a person is reachable at all; the explicit toggle only
chooses between the two reachable states.

Redis holds it, because it is spread over every replica:

| Key | Holds | Expiry |
|---|---|---|
| `presence:brands` | Brands anyone is present in, so the reaper has a work list | with its last member |
| `presence:<brandId>` | User ids present in that brand | with its last member |
| `presence:sockets:<brandId>:<userId>` | That person's socket ids there | with its last member |
| `presence:sock:<socketId>` | Nothing; its existence *is* the liveness | 60 s |
| `presence:status:<brandId>` | The explicit `away` toggles | with its last member |
| `presence:active:<brandId>` | When each person last reported activity, in epoch ms (M14-18) | with its last member |
| `presence:idle:<brandId>` | People marked away for being idle (M14-18) | with its last member |

The liveness key is the only one with a TTL, and that is the point: a replica
that dies without running a single disconnect handler leaves its sockets'
keys behind, they expire within a minute, and the reaper turns the people they
belonged to offline. The client refreshes it every 25 s with
`presence:heartbeat`, so two heartbeats may be lost before anyone is called
offline.

A reaper runs in every api process every 30 s. Every replica sweeps, but taking
a person out of `presence:<brandId>` answers "removed" on exactly one of them,
so `presence:changed` is emitted once however many replicas are running.

`GET /api/brands/:brandId/presence` (`@Requires('staff:read')`) answers the map
a screen renders before any event arrives. Nobody is listed as `offline`:
absence is what offline means, so the map grows with who is working rather than
with the brand's roster.

`staff:read` is the one permission every role holds. DOMAIN-RULES §1.2
restricts *tickets* by department, never colleagues: an Agent has to see who is
online before taking a ticket. Changing who may work in a brand stays
`staff:manage`.

### Idle agents marked away

The server cannot tell the difference between reading a ticket and going to
lunch — the socket stays open either way — so the browser says, and the server
decides what silence means (M14-18).

The admin sends `presence:active` with the brand when a pointer, key, wheel or
touch event happens, the window gains focus or a tab becomes visible, and once on opening, at most once
a minute per tab; an event inside the minute schedules one report for its end.
The message carries the brand and nothing else: never which key or element.

The server keeps one clock per person per brand, not per socket, so a second
tab's silence does not undo the first's work. A department may set **Mark idle
agents away after** (10 to 240 minutes, Inbox › Assignment); a person's
limit is the shortest among the departments that set one and whose rotation
includes them. Departments that already existed when the setting arrived
(migration `0068`) were backfilled to a 10-minute limit — the closest to 1.0's
five-minute away timer the 10-minute minimum allows; departments created after
it default to off (NULL). Admins can change it or turn it off per department on
Inbox › Assignment. The reaper's 30 s sweep marks whoever is past their
limit, and
`presence:changed` says `away`. Checking the clock, the person's presence and
marking them is one Lua script on Redis's own clock (`TIME`), so a report
landing at the same moment, or a departure, cannot be overruled and replicas
whose clocks disagree still agree; exactly one replica announces it, because
the `sadd` in that script answers 1 on one replica only. The sweep reads the
limits through the `IDLE_AWAY_POLICY` token (`DepartmentIdleAwayPolicy`), and a
sweep that cannot read them changes nothing.

Activity ends the mark, announces `online`, and the acknowledgement of that one
report says `idleEnded: true` so the admin shows "You were set to away while
idle" once. An explicit Away from the user menu is a separate reason: activity
never undoes it, and choosing Online ends both. Going offline forgets the clock
and the mark.

Idle away is `away`, not `offline`: the rotation skips the person, the widget
does not list them as online, the notification presence hold treats them as
away, and the offline auto-unassign timer never starts for them.

### The hook M1 needs

`StaffOfflineHook.onStaffOffline(userId, brandId, since)` fires when the last of
someone's sockets in a brand goes. M1-07's `OutboxStaffOfflineHook`
(`apps/api/src/assignment/staff-offline.hook.ts`) is what `RealtimeModule`
provides under the `STAFF_OFFLINE_HOOK` token: it writes an
`assignment.staff_offline` outbox row when any department of the brand
auto-unassigns, and the worker starts the department's timer
([Assignment](https://helpdock.oday-bakkour.com/docs/ticketing-settings/#the-offline-timer)). The gateway did not
change.

## Revocation

> On role change, deactivation, or "log out everywhere", the server publishes
> `principal.revoked` over Redis and every replica disconnects that principal's
> sockets within 5 seconds. — DOMAIN-RULES §1.4

M0-05 publishes `{ principalType, principalId, reason, familyIds? }`.
`RevocationSubscriber` subscribes on a dedicated connection — a subscribed
ioredis client may run no other command, and the Socket.IO adapter has
subscriptions of its own — parses the message, and closes the sockets of that
person that this replica is holding. It emits `revoked` first, so the client
shows "signed out" instead of reconnecting in a loop.

Revocation is per browser. Each socket remembers the refresh family (`fam`) of
the token it was opened with, and a message that carries `familyIds` closes only
the sockets of those families:

| Revocation | `familyIds` | Sockets closed |
|---|---|---|
| Sign out, refresh-token reuse, an account left with no brand role | the one family | that browser's |
| Password change from the security page | every family but the current one | every other browser's |
| Sign out everywhere, password reset, role or department change, deactivation | absent | every socket of that person |

That is the push half. The pull half is the revocation check on every room join,
for the window before the message arrives and for a replica that missed it.

## The admin client

`apps/admin/src/realtime/` holds one client behind one interface
(`RealtimeClient`), with two implementations:

- `SocketRealtimeClient` — the real connection. Socket.IO's own reconnection is
  off, because each attempt has to carry a *fresh* access token: the one this
  tab held ten minutes ago has expired and re-presenting it would fail forever.
  It reconnects itself with exponential backoff and full jitter (500 ms
  doubling to 30 s), heartbeats while connected, and stops for good on
  `revoked`.
- `MockRealtimeClient` — the fixture `pnpm dev` and the browser tests run
  against, chosen by the same `VITE_AUTH_API` switch as `MockAuthApi`. The two
  cannot disagree: a real socket against a mocked session would have no token to
  present.

`RealtimeProvider` sits below `RequireSession`, because a connection needs a
principal, a brand and a token. It reads the REST map, applies
`presence:changed` to it, joins `brand:<currentBrandId>`, and re-targets the
same client when the brand switcher is used. `useRealtime()` gives the current
person's own status and the toggle; `usePresence(brandId)` gives the map.

The sidebar footer shows the status as a `PresenceDot` (DESIGN §6.2: 8 px,
success, warning, n400) *and* in words, because colour is never the only carrier
of meaning (DESIGN §10).

## What later milestones add

| Milestone | Adds |
|---|---|
| M1-09 | Shipped in branch: `activity` on `ticket:viewing` ("is replying"), and `ticket:changed` in both tickets' rooms on a merge, an unmerge and a split. |
| M3-07 | Shipped: the `user:<id>` room and `notification:created` (see [notifications](https://helpdock.oday-bakkour.com/docs/notifications/)). |
| M4-03 | Shipped in branch: the origin check moved from the engine into each namespace's handshake; the widget handshake's origin allow-list and its per-address throttle. |
| M4-04 | Shipped in branch: the `/widget` namespace and the full delivery contract for conversations — `clientId`, `seq`, cursor catch-up and the SSE fallback ([widget protocol](https://helpdock.oday-bakkour.com/docs/widget-protocol/)); `ticket:visitor_typing` and `ticket:visitor_read` on `/staff`. The widget's client is `apps/widget/src/transport/remote.ts`. |

Adding an event is three steps: a schema and a name in
`packages/schemas/src/realtime.ts`, an `emitToRoom` call through
`RealtimePublisher`, and — if clients send it — a `@SubscribeMessage` handler
with its `@Requires(...)`.

## Emitting from a worker

`APP_ROLE=worker` drains the queues and runs no Nest application, so it holds no
Socket.IO namespace and cannot emit. `APP_ROLE=api` holds the sockets and runs no
queue. A side effect that ends in a socket frame therefore crosses one process
boundary, over the same Redis pub/sub shape `principal.revoked` already uses
(`apps/api/src/realtime/broadcast.ts`):

```
worker   →  PUBLISH helpdock:realtime:emit   { rooms, event, data, seq, evict }
api      →  RealtimeEmitSubscriber → RealtimePublisher.emitToRoom(…, { local: true })
```

`evict` names the rooms whose members must be turned out once the frame is
delivered — the ticket room of a ticket that has just changed department, whose
members all joined under the old department's scope.

`local: true` is not an optimisation. Every replica subscribes and every replica
receives the message, and the Socket.IO Redis adapter would fan a normal emit out
again — so a room would hear the same frame once per replica. The fan-out has
already happened by the time the subscriber runs, so each replica does the last
hop to its own sockets alone.

Both halves of the message are parsed on arrival — the envelope and the payload,
through the event's own schema — because it crosses a process boundary and
anything with `PUBLISH` on that Redis can write to the channel. A frame that
does not match is dropped rather than thrown on: this runs inside a Redis
`message` handler, where a throw is an unhandled rejection. An event a replica
does not know, which is what a rolling deploy looks like, is dropped the same
way; the screen re-reads over REST anyway.

## Known gaps

- None open. Per-browser revocation and the event budgets closed M0's two
  gaps in M9.

## Operating it

- **Behind a proxy.** `/socket.io` is served by the api like any other path, so
  `docker/caddy/Caddyfile` needs nothing for it: Caddy's `reverse_proxy` passes
  a WebSocket upgrade through on its own. A proxy that does not must be
  configured to.
- **Replicas.** No sticky sessions are needed, because there is no polling and
  the Redis adapter makes rooms replica-independent. A socket does pin to the
  replica it connected to for its lifetime, which is what a WebSocket is.
- **Metrics.** The gateway sets `socket_connections`, labelled by `namespace`,
  on every connect and disconnect. The gauge belongs to M0-10's registry and is
  scraped from `/metrics` like everything else
  ([operations](https://helpdock.oday-bakkour.com/docs/operations/#metrics)); `realtime/` writes to it through a
  one-method `SocketConnectionsGauge` so it needs to know nothing else about
  Prometheus.
- **Configuration.** None. `APP_URL` decides the allowed origin and `REDIS_URL`
  the adapter's connections; both already exist.
