@protobase/cli 0.121.10 → 0.121.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/package.js +1 -1
- package/dist/relay/app-entry.js +16 -3
- package/dist/relay/register.js +1 -1
- package/package.json +8 -8
- package/skills/protobase/references/features/history-comments-and-live-updates.md +3 -2
- package/skills/protobase/references/reference/cli.md +1 -1
- package/skills/protobase/references/reference/config/api.md +1 -1
- package/skills/protobase/references/reference/custom-components.md +35 -1
- package/skills/protobase/references/reference/realtime-relay.md +88 -3
- package/skills/protobase/references/reference/sidebar.md +17 -0
- package/skills/protobase/references/reference/ui-config.md +1 -0
package/dist/cli/package.js
CHANGED
package/dist/relay/app-entry.js
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
+
import { relayPresencePath } from "@protobase/server";
|
|
1
2
|
//#region src/relay/app-entry.ts
|
|
2
|
-
/**
|
|
3
|
-
const
|
|
3
|
+
/** How long a person who closed their last socket still counts as present, so a phone that reconnects never goes offline. */
|
|
4
|
+
const presenceGraceSeconds = 30;
|
|
5
|
+
const systemPath = (apiPath = "/api/v1") => apiPath.slice(0, apiPath.lastIndexOf("/"));
|
|
6
|
+
const presenceEntry = (presence, appUrl, apiPath) => {
|
|
7
|
+
if (!appUrl) throw new Error("realtime.relay.presence needs the app's url (url or PROTOBASE_URL), where the relay posts its presence reports");
|
|
8
|
+
return {
|
|
9
|
+
url: new URL(relayPresencePath(systemPath(apiPath)), appUrl).href,
|
|
10
|
+
key: presence.key,
|
|
11
|
+
grace_seconds: presenceGraceSeconds
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
/** The app's entry in the relay's apps file: its id, its keys, the browser origin of its URL and, with presence, where its reports go. */
|
|
15
|
+
const relayAppEntry = (relay, appUrl, apiPath) => ({
|
|
4
16
|
id: relay.app,
|
|
5
17
|
api_keys: [relay.apiKey],
|
|
6
18
|
ticket_keys: [relay.ticketKey],
|
|
7
|
-
origins: appUrl ? [new URL(appUrl).origin] : []
|
|
19
|
+
origins: appUrl ? [new URL(appUrl).origin] : [],
|
|
20
|
+
...relay.presence && { presence: presenceEntry(relay.presence, appUrl, apiPath) }
|
|
8
21
|
});
|
|
9
22
|
//#endregion
|
|
10
23
|
export { relayAppEntry };
|
package/dist/relay/register.js
CHANGED
|
@@ -11,7 +11,7 @@ const registerRelay = (program) => {
|
|
|
11
11
|
const config = await loadSourceConfig(process.cwd(), { command: "tool" });
|
|
12
12
|
if (!config.realtime?.relay) throw new Error("The config has no realtime.relay");
|
|
13
13
|
const url = config.url ?? process.env.PROTOBASE_URL;
|
|
14
|
-
process.stdout.write(`${JSON.stringify(relayAppEntry(checkRelayOptions(config.realtime.relay), url), null, 2)}\n`);
|
|
14
|
+
process.stdout.write(`${JSON.stringify(relayAppEntry(checkRelayOptions(config.realtime.relay), url, config.apiPath), null, 2)}\n`);
|
|
15
15
|
});
|
|
16
16
|
};
|
|
17
17
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.121.
|
|
3
|
+
"version": "0.121.12",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@better-auth/core": "^1.7.12",
|
|
45
45
|
"@hono/node-server": "^2.1.3",
|
|
46
|
-
"@protobase/config": "0.121.
|
|
47
|
-
"@protobase/layout": "0.121.
|
|
48
|
-
"@protobase/query": "0.121.
|
|
49
|
-
"@protobase/schema": "0.121.
|
|
50
|
-
"@protobase/server": "0.121.
|
|
51
|
-
"@protobase/ui": "0.121.
|
|
52
|
-
"@protobase/workflows": "0.121.
|
|
46
|
+
"@protobase/config": "0.121.12",
|
|
47
|
+
"@protobase/layout": "0.121.12",
|
|
48
|
+
"@protobase/query": "0.121.12",
|
|
49
|
+
"@protobase/schema": "0.121.12",
|
|
50
|
+
"@protobase/server": "0.121.12",
|
|
51
|
+
"@protobase/ui": "0.121.12",
|
|
52
|
+
"@protobase/workflows": "0.121.12",
|
|
53
53
|
"@react-email/render": "^2.1.0",
|
|
54
54
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
55
55
|
"@sentry/vite-plugin": "^11.10.0",
|
|
@@ -18,6 +18,7 @@ The changes of each record, comments on it, and pages that refresh as records ch
|
|
|
18
18
|
- [Polling, then a socket](../reference/live.md#polling-then-a-socket): Apps and Node scripts poll for changes first and switch to a WebSocket after 30 seconds, or at once, falling back to polling while the socket is down.
|
|
19
19
|
- [Presence](../reference/channels.md#presence) (beta): Apps see who else has a record open and what each of them tracks, such as online or away, for whoever may read the record.
|
|
20
20
|
- [Realtime relay](../reference/realtime-relay.md) (in development): Apps hand their realtime connections to a relay, so they and their database can sleep while people stay connected: the app issues connect tickets for signed-in people and visitors, keeps the relay's channel members in step with its members records, tells each channel's members of every change, and closes a session's sockets when it signs out or expires. `protobase dev` runs a relay for the app by itself.
|
|
21
|
-
- Relay presence (
|
|
22
|
-
- Relay
|
|
21
|
+
- [Relay presence](../reference/realtime-relay.md#presence) (in development): The relay reports who comes online, goes away and goes offline; the app keeps it in a resource of its own, raises that resource's events, so workflows can act when someone goes offline, and tells the people who share a conversation with them.
|
|
22
|
+
- [Relay in pages](../reference/realtime-relay.md#pages) (in development): Pages follow the relay on one socket: open views stay current from its notices and read again once after a reconnect, conversations show who types, sidebar groups say "typing…" and count unread messages without reading per message, all by configuration; `client.relay` gives the same from code.
|
|
23
|
+
- Relay client events (coming soon): Each channel resource declares in the config the events its clients may send each other, such as typing, with a schema, a size and a rate.
|
|
23
24
|
- [Broadcasts](../reference/channels.md#broadcasts) (beta): Apps on a record send each other short-lived events, such as typing, which are never stored, as JSON or as bytes, such as a chunk of audio or a drawing stroke; the app's own code sends them too, from an API function, over HTTP or [from SQL](../reference/channels.md#from-sql) with `channels.send()`.
|
|
@@ -658,7 +658,7 @@ Recounts a [time rollup](data-config.md#keeping-it-up-to-date)'s buckets from th
|
|
|
658
658
|
protobase relay app
|
|
659
659
|
```
|
|
660
660
|
|
|
661
|
-
Prints the app's entry for the apps file of the [realtime relay](realtime-relay.md), as JSON: its `id`, `api_keys` and `ticket_keys` from `realtime.relay`, and in `origins` the origin of the app's `url` (or `PROTOBASE_URL`). Add the relay's limits and options to it there. It reads `protobase.config.ts` with `command: 'tool'` and the `.env` nearest the current directory.
|
|
661
|
+
Prints the app's entry for the apps file of the [realtime relay](realtime-relay.md), as JSON: its `id`, `api_keys` and `ticket_keys` from `realtime.relay`, and in `origins` the origin of the app's `url` (or `PROTOBASE_URL`); with [presence](realtime-relay.md#presence), `presence` with the app's presence endpoint at that `url`, the presence key and a `grace_seconds` of 30. Add the relay's limits and options to it there. It reads `protobase.config.ts` with `command: 'tool'` and the `.env` nearest the current directory.
|
|
662
662
|
|
|
663
663
|
## `auth migration`
|
|
664
664
|
|
|
@@ -151,7 +151,7 @@ export default defineConfig({ runtimeUpdates: false })
|
|
|
151
151
|
|
|
152
152
|
## realtime
|
|
153
153
|
|
|
154
|
-
- **Type:** `{ processes?: 'one' | 'many', sqlSends?: boolean, relay?: { url, app, apiKey, ticketKey, channels } }`
|
|
154
|
+
- **Type:** `{ processes?: 'one' | 'many', sqlSends?: boolean, relay?: { url, app, apiKey, ticketKey, channels, presence? } }`
|
|
155
155
|
- **Default:** `{ processes: 'one', sqlSends: false }`
|
|
156
156
|
|
|
157
157
|
How [channels](../channels.md) and [Supabase Realtime](../supabase-realtime.md) are served. With `'one'`, one process serves the app, and broadcasts and presence go between its channels in memory. Set `'many'` when several processes serve the app behind one address: broadcasts and presence then go between the processes through the `realtime_relay` schema of the app's database, which [`protobase migration`](../cli.md#migration) writes and the server checks at start. See [Several processes](../channels.md#several-processes). Set `sqlSends: true` to let SQL in the app's database broadcast to channels with `channels.send()`, from the `channels` schema that `protobase migration` writes and the server checks at start. See [From SQL](../channels.md#from-sql). With `relay`, the app also hands its realtime connections to a relay, so it can sleep while people stay connected; see [Realtime relay](../realtime-relay.md).
|
|
@@ -425,7 +425,7 @@ A chat between people, such as a team's channels, is built from the parts the [a
|
|
|
425
425
|
| `ChatThread` | The scrolling list of messages: `length`, the number of messages, and `empty`, shown while there are none. A reader at the end follows new messages; one who scrolled back stays where they are, until `followKey` changes: change it when the reader sends, such as to a count of their sends, and the thread goes to the end and follows their message in. `onLoadEarlier` adds **Load earlier messages** at the top, with `loadingEarlier` while they come; the earlier messages keep the reader on the message they were reading, never on a `ChatDivider`, which moves when earlier messages of its day load above it. It opens on its `ChatDivider tone="new"` line, near the top, when the messages it first gets hold one, and at the end otherwise. `onReadToEnd` is called while the page shows and the reader is at the end: when it opens there, when a message arrives as they follow, when they scroll to the end and when the page shows again with them there, such as to move their read position. It scrolls inside itself, so the composer under it stays in view. |
|
|
426
426
|
| `ChatMessage` | One message. `author` (`{ id, name }`) draws it on the left beside the author's avatar, under their name and `time`, an ISO timestamp shown on the viewer's clock; `avatar` replaces the initials, such as with a photo, and `continued` leaves out avatar, name and time for a message that follows the same author's. `from: 'user' \| 'assistant'` draws an assistant chat's sides instead. `footer` adds a small line under either, such as whether it was read. |
|
|
427
427
|
| `ChatDivider` | A line across the chat with a label: `label` for a day, such as `chatDayLabel(time)` ("Today", "Yesterday" or the date, on the viewer's calendar) between two messages for which `sameChatDay(a, b)` is false; `tone="new"` for the red **New messages** line. |
|
|
428
|
-
| `Composer` | The box under the chat, which grows with its text: Enter or the button sends, Shift+Enter starts a new line. `onSend` gets the trimmed text and the ids of the people it mentions. With `searchPeople`, a function that returns the `{ id, name }` people whose name matches the text typed after `@`, such as a channel's members, typing `@` lists them above the box: the arrows move, Enter or Tab picks, Escape closes. `busy` keeps the text from being sent. For a message that carries files, `actions` draws an attach button left of the box and `attachments` a tray of the picked files above it, `canSendEmpty` lets Enter and the button send with no text, `onSend('', [])`, and `onFiles` gets the files pasted into the box or dropped on the composer, which uploads nothing itself; a paste that holds text stays text. |
|
|
428
|
+
| `Composer` | The box under the chat, which grows with its text: Enter or the button sends, Shift+Enter starts a new line. `onSend` gets the trimmed text and the ids of the people it mentions. With `searchPeople`, a function that returns the `{ id, name }` people whose name matches the text typed after `@`, such as a channel's members, typing `@` lists them above the box: the arrows move, Enter or Tab picks, Escape closes. `busy` keeps the text from being sent. For a message that carries files, `actions` draws an attach button left of the box and `attachments` a tray of the picked files above it, `canSendEmpty` lets Enter and the button send with no text, `onSend('', [])`, and `onFiles` gets the files pasted into the box or dropped on the composer, which uploads nothing itself; a paste that holds text stays text. `onDraft` gets the box's text each time it changes, such as for [typing](#typing). |
|
|
429
429
|
| `MentionBox`, `MentionedText` | The text box with `@` mentions that the composer and [comments](comments.md) are built from, for a box of your own: `value` and `onChange` hold the text and the people picked (`emptyDraft` to start), and `mentionedIds(text, picked)` the ids still mentioned. `MentionedText` shows a message's `text` with comments' [formatting](comments.md#formatting): bold, italic, strikethrough, code, code blocks, lists, quotes and web addresses as links, each `@name` of its `mentions` picked out. |
|
|
430
430
|
|
|
431
431
|
The messages' author comes from the server: give the resource [`.author()`](data-config.md#resource), and each create stores the signed-in user's id, never a value the client sends.
|
|
@@ -486,6 +486,40 @@ export const ChannelComposer = ({ onSend }: { onSend: (text: string, mentions: s
|
|
|
486
486
|
|
|
487
487
|
`searchPeople` filters the list in the browser and shows at most 8, as comments' mentions do. <gap>`useMembers()` cannot page or search on the server because a server-side people resource is not implemented, so the whole list loads in the browser, which suits a team, not thousands of members.</gap> <gap>Old messages of someone who has left the organization cannot show their name because looking up former members is not implemented.</gap> A person who works in an organization without being a member of it, through a global role, gets no list.
|
|
488
488
|
|
|
489
|
+
### Typing
|
|
490
|
+
|
|
491
|
+
In an app with the [realtime relay](realtime-relay.md#pages), a chat of your own shows who types and says when its reader types, over the same socket the admin app's pages use, on the record's channel, `<resource>/<key>`:
|
|
492
|
+
|
|
493
|
+
| | |
|
|
494
|
+
| --- | --- |
|
|
495
|
+
| `TypingLine` | The line above the box saying who else types on `channel`, by the names `useMembers()` gives; it keeps its height while nobody does, so the box does not move. |
|
|
496
|
+
| `useTypers(channel)` | The user ids of the others who type on the channel now, each until six seconds after the last they sent; empty without the relay or while it is down. |
|
|
497
|
+
| `useTypingSender(channel)` | `onText`, to call with the box's text as it changes, and `onSent`, once a message went: it sends `typing` events at most every three seconds, and that the reader stopped when they empty the box, send or leave. Nothing goes before the channel is joined. |
|
|
498
|
+
| `useViewChannel(channel)` | Says the page shows the channel while it is drawn, so only its notices read the page again, and joins it when the connect ticket did not list it, such as a conversation the reader joined since. |
|
|
499
|
+
|
|
500
|
+
```tsx
|
|
501
|
+
import { Composer, TypingLine, useTypingSender, useViewChannel } from '@protobase/ui'
|
|
502
|
+
|
|
503
|
+
export const RoomComposer = ({ room, onSend }: { room: number; onSend: (text: string, mentions: string[]) => Promise<boolean> }) => {
|
|
504
|
+
const channel = `rooms/${room}`
|
|
505
|
+
useViewChannel(channel)
|
|
506
|
+
const typing = useTypingSender(channel)
|
|
507
|
+
const send = async (text: string, mentions: string[]) => {
|
|
508
|
+
const sent = await onSend(text, mentions)
|
|
509
|
+
if (sent) typing.onSent()
|
|
510
|
+
return sent
|
|
511
|
+
}
|
|
512
|
+
return (
|
|
513
|
+
<>
|
|
514
|
+
<TypingLine channel={channel} />
|
|
515
|
+
<Composer onSend={send} onDraft={typing.onText} />
|
|
516
|
+
</>
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
The relay must allow the `typing` event on the channel resource, as for a [conversation page](realtime-relay.md#pages). For anything else, such as an event of your own, `useClient().relay` is the same socket (see [From code](realtime-relay.md#from-code)).
|
|
522
|
+
|
|
489
523
|
### Signed-in user
|
|
490
524
|
|
|
491
525
|
`useSignedInUser()` from `@protobase/ui` gives the signed-in person as `{ id, name? }`, `id` being the user id `.author()` stores and mentions hold, such as for a filter on their own messages (`author_id != "<id>"`) or mentions of them; it is `undefined` while the session loads, for a visitor and when nobody is signed in.
|
|
@@ -7,7 +7,7 @@ Hand the app's realtime connections to a relay, so the app and its database can
|
|
|
7
7
|
The app can hand its realtime connections to a relay, the open source [protobase-realtime](https://github.com/PrototeamHQ/protobase-realtime) server: the relay holds everyone's WebSocket, and the app talks to it only when something changes, so a quiet app and its database sleep while people stay connected. The relay keeps no data and never reads the app's database: the app tells it who is in which channel, what changed and for whom, and which sign-in sessions ended. It works beside the app's own [channels](channels.md) and [live updates](live.md), which stay as they are.
|
|
8
8
|
|
|
9
9
|
:::caution[In development]
|
|
10
|
-
The app's side is built: tickets, membership, change notices
|
|
10
|
+
The app's side is built: tickets, membership, change notices, session ends and presence, and so are pages' side in `@protobase/client` and the admin app: [live updates, typing and unread marks](#pages). <gap>The config cannot declare the events clients send each other, such as typing, because declaring client events per channel resource is not implemented, so the relay's apps file declares them by hand.</gap>
|
|
11
11
|
:::
|
|
12
12
|
|
|
13
13
|
## Config
|
|
@@ -27,6 +27,7 @@ export default defineConfig(({ env }) => ({
|
|
|
27
27
|
},
|
|
28
28
|
rooms: {},
|
|
29
29
|
},
|
|
30
|
+
presence: { resource: 'presences', user: 'user_id', status: 'status', at: 'since', key: env.RELAY_PRESENCE_KEY! },
|
|
30
31
|
},
|
|
31
32
|
},
|
|
32
33
|
}))
|
|
@@ -40,6 +41,7 @@ export default defineConfig(({ env }) => ({
|
|
|
40
41
|
| `channels` | The channel resources, by name. Each record of one is a channel, `<resource>/<key>`, named as its [record channel](channels.md) is; the resource needs a key of one field. |
|
|
41
42
|
| `channels.<resource>.members` | Where the channel's members come from: a resource whose records each put one person in one channel, with its relation field to the channel resource (`channel`) and its field of the person's user id (`user`). Index both columns: tickets read by the user, notices by the channel. Without `members`, the channel is open: a channel of a public record, which tickets list when asked (see [Tickets](#tickets)). |
|
|
42
43
|
| `channels.<resource>.mentions` | A field of the records noticed on the channel that lists the user ids they mention, an array or one id, which [notices](#notices) carry. |
|
|
44
|
+
| `presence` | Turns on [presence](#presence): the app's own resource it is kept in, its fields of the person's user id (`user`), the status (`status`) and when it began (`at`), and the bearer token of the relay's reports (`key`), at least 32 characters. |
|
|
43
45
|
|
|
44
46
|
The relay needs the app's own [sign-in](auth.md), whose sessions its tickets name, with its store in the app's own database, as the `auth` config puts it: an auth store in another database is refused. A members resource's changes must be captured, so declare [`.live()`](live.md) on it; the relay hears of the changes of every resource whose changes are captured, such as a live one, that is a channel resource or has a relation field to one. [`protobase migration`](cli.md#migration) writes the trigger that ends a deleted session's sockets, in the `relay` schema, and the server checks it at start.
|
|
45
47
|
|
|
@@ -59,9 +61,9 @@ It starts the relay on the first load of a config with `realtime.relay`, an edit
|
|
|
59
61
|
|
|
60
62
|
## Tickets
|
|
61
63
|
|
|
62
|
-
A client connects to the relay with a ticket from `POST /api/relay/ticket`, with `{ "channels": [...] }`, optional, as JSON. The answer is `{ ticket, url, expiresAt }`: the relay's socket address
|
|
64
|
+
A client connects to the relay with a ticket from `POST /api/relay/ticket`, with `{ "channels": [...] }`, optional, as JSON. The answer is `{ ticket, url, channels, expiresAt }`: the relay's socket address, a ticket that connects once, within a minute, and the channels it lists, which the client joins. Every connection, a reconnect too, takes a new ticket.
|
|
63
65
|
|
|
64
|
-
- **Signed in:** the ticket names the person and the sign-in session they called with, so a device that connects makes no new session, and lists every channel they are a member of, as the members resources hold it. A caller without a session of the app, such as an API key or an outside app acting for the user, is refused with `403`; a session that ended with `401`.
|
|
66
|
+
- **Signed in:** the ticket names the person and the sign-in session they called with, so a device that connects makes no new session, and lists every channel they are a member of, as the members resources hold it, and `people`, which every signed-in person has and [presence](#presence) is published on. A caller without a session of the app, such as an API key or an outside app acting for the user, is refused with `403`; a session that ended with `401`.
|
|
65
67
|
- **Visitors:** with an `anonymous` role, a caller without a token gets a ticket of an anonymous session of its own, which receives and never sends.
|
|
66
68
|
- **Open channels:** both list the channels they ask for in `channels` of resources without `members` whose records are public: the `anonymous` role may read them, as `GET /api/v1/<resource>/<key>` decides it for a visitor. Someone's own access to a record never makes its channel open, so no one's access to one can end while their socket stays open. A channel there is none of, one whose record is not public and one of a resource with members are refused alike with `404`; a record only some people may read takes `members`. A ticket lists at most 200 channels (`400`, or `422` when the memberships are more).
|
|
67
69
|
|
|
@@ -82,3 +84,86 @@ Once an open channel's record stops being public, its notices stop, and a new ti
|
|
|
82
84
|
## Sessions
|
|
83
85
|
|
|
84
86
|
When a sign-in session ends, its sockets close: a trigger on the sessions table queues a `relay.close-sessions` job in the deleting transaction, for a sign-out, a revoke, a ban, a newer sign-in in the same browser or a delete in SQL alike, and the job tells the relay to close them. A ticket also queues a `relay.session-expiry` job for its session's expiry, once for each session: it closes the session's sockets once the session expired, and waits for the new expiry of one that was extended meanwhile. The person's other sessions stay connected. The relay itself keeps no timers.
|
|
87
|
+
|
|
88
|
+
## Presence
|
|
89
|
+
|
|
90
|
+
With `presence`, the relay reports who comes online, goes away and goes offline, combined over their devices, and the app keeps it in a resource of its own, one record per person:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
export const presences = resource('presences')
|
|
94
|
+
.fields({ user_id: f.text(), status: f.enum(['online', 'away', 'offline']), since: f.timestamp() })
|
|
95
|
+
.primaryKey((r) => r.user_id)
|
|
96
|
+
.live()
|
|
97
|
+
.events((e) => [e.updated('wentOffline', { when: 'status = "offline"' })])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The resource is keyed by its user field, its status field is text or an enum of `online`, `away` and `offline`, and its `at` field a timestamp; its changes must be captured, such as with [`.live()`](live.md). The server refuses to start otherwise. Protobase adds no table of its own.
|
|
101
|
+
|
|
102
|
+
Only the relay writes the records, so give no role `presences.create`, `presences.update` or `presences.delete`: whoever may write one can set anyone's presence and raise its events. A role that reads the resource reads the presence of everyone its rows allow, which can reach further than `people` does, so grant `presences.read` no wider than that.
|
|
103
|
+
|
|
104
|
+
The relay posts its reports to `POST /api/relay/presence` with the presence key as its bearer token, never a sign-in; the [apps file entry](cli.md#relay-app) names the address. A report holds at most 500 changes, each a person's status and when it began. The app writes them in one statement, which replaces a record's status only with one that began later, and answers `204`: a report the relay sends again, or one that arrives late, changes nothing. A report with a missing or wrong key is refused with `401` before its body is read. To change the key, change it in the config and in the apps file; the relay sends a refused report again, waiting up to a minute between tries, until the app takes it, so no change is lost. When the relay restarted, which a report says with its start time, everyone it has not reported since and who is not offline yet is set offline as of that start, once in each server process for each start; the relay sends no report until someone's status changes, so this waits for the first one.
|
|
105
|
+
|
|
106
|
+
Each write goes through the resource's table, so its changes raise the resource's [events](events.md), [webhooks](webhooks.md) and [workflows](workflows.md) as any other record's do, such as a workflow on `presences.wentOffline`. An offline report raises its event once.
|
|
107
|
+
|
|
108
|
+
Each change of a person's presence is also published on `people` as a `presence_changed` event, the same bytes to everyone who shares a channel with members with them, themselves included, as the members tables hold it at dispatch, read for the whole batch in one query:
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
{ "user": "u1", "status": "offline", "at": "2026-11-21T10:00:00.000Z" }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
A person who shares no channel is published to no one. <gap>A page cannot show who of the others is online because reading presence in the pages is not implemented.</gap>
|
|
115
|
+
|
|
116
|
+
## Pages
|
|
117
|
+
|
|
118
|
+
With `realtime.relay`, the admin app follows the relay for someone signed in, on one socket per page, and `/meta` names the resources whose changes the relay notices:
|
|
119
|
+
|
|
120
|
+
- **Live updates:** the lists, records and counts of those resources are read again as the relay notices their changes, new, edited and deleted records alike, instead of through [`/api/live`](live.md), which stays for the other `.live()` resources. While a [conversation page](ui-config.md#conversation-pages) is open, only its own channel's notices read it again; the notices of other channels only move the [unread marks](sidebar.md#typing-and-unread-from-the-relay). After the socket was down, everything shown is read again once.
|
|
121
|
+
- **Typing:** a conversation page with `typing` says who else types, above its box, and tells the others while the reader types, as `typing` events on the record's channel. A sidebar group with [`typing`](sidebar.md#typing-and-unread-from-the-relay) says "typing…" beside a record someone types in, wherever the reader is.
|
|
122
|
+
- **Unread:** a sidebar group with [`unread`](sidebar.md#typing-and-unread-from-the-relay) counts each conversation's unread messages once, then adds the relay's notices without reading.
|
|
123
|
+
- **Presence:** the page tells the relay the person is away while it is hidden and online once it shows; a page that shows again, or a device back online, connects again at once.
|
|
124
|
+
|
|
125
|
+
The relay refuses events the app did not declare, so declare typing in the relay's apps file, on each channel resource with typing:
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
"client_events": {
|
|
129
|
+
"channels": {
|
|
130
|
+
"conversations": {
|
|
131
|
+
"events": {
|
|
132
|
+
"typing": { "schema": { "type": "object", "properties": { "typing": { "type": "boolean" } }, "required": ["typing"] }, "max_bytes": 64, "per_second": 2, "burst": 4 }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
A conversation page's `typing` needs its resource among `channels`: the server refuses to start otherwise.
|
|
140
|
+
|
|
141
|
+
<gap>A page does not hear of a channel the person joined after it connected, such as a new direct message, until it opens that conversation or connects again, because telling a person's sockets of a new channel is not implemented.</gap> <gap>An unread count also counts the reader's own messages sent from another device or tab, until they open the conversation, because notices do not name who made the change.</gap>
|
|
142
|
+
|
|
143
|
+
### From code
|
|
144
|
+
|
|
145
|
+
`client.relay` follows the relay from any page or app with `@protobase/client`:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const stop = client.relay.follow({
|
|
149
|
+
onNotice: (notice) => {}, // { kind, resource, key, channel, mentions? }
|
|
150
|
+
onEvent: (event) => {}, // { event, payload, from: { user, connection }, at, channel } or, direct, { ..., to }
|
|
151
|
+
onResync: () => {}, // connected again after a drop: read again what is shown
|
|
152
|
+
onStatus: (status) => {}, // 'connecting', 'open' or 'ended'
|
|
153
|
+
})
|
|
154
|
+
await client.relay.send('conversations/7', 'typing', { typing: true })
|
|
155
|
+
await client.relay.direct('u2', 'ring', { call: 12 })
|
|
156
|
+
client.relay.view('conversations/7', ['thread:3'])
|
|
157
|
+
client.relay.presence('away')
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
| | |
|
|
161
|
+
| --- | --- |
|
|
162
|
+
| `follow(handlers)` | Hears every notice and event until the returned function is called. The first follower opens the socket and the last closes it. The socket connects with a fresh ticket each time, joins the relay's own topic and every channel the ticket lists, and connects again after a pause that grows with each failure, up to 30 seconds; an app that refuses a ticket with a client error, such as for a session that ended, ends it (`ended`). |
|
|
163
|
+
| `send(channel, event, payload)` | Sends a declared event to the channel's other sockets. Rejects with a `RelayRefusal` and the relay's reason, such as `rate_limited` or `event_not_allowed`, or `not_joined`. |
|
|
164
|
+
| `direct(to, event, payload)` | Sends a declared direct event to every device of a person who shares a channel with the sender. |
|
|
165
|
+
| `join(channel)` | Joins a channel the ticket did not list, such as one the person joined since, and again after each reconnect until the returned function is called. The socket stays in the channel until it next connects. |
|
|
166
|
+
| `view(channel, keys)` | Says which keys the page views in the channel, so the app's publishes about other keys pass it by; `undefined` views everything. |
|
|
167
|
+
| `presence(status)` | `away` or `online`, as the relay combines it over the person's devices. |
|
|
168
|
+
| `reconnect()` | Connects at once when the socket is down and waiting to try again. |
|
|
169
|
+
| `joined(channel)`, `status()` | Whether the socket joined a channel, and the connection's status. |
|
|
@@ -133,6 +133,8 @@ export const channelsView = view<typeof channels>('channels').nav({ recent: { or
|
|
|
133
133
|
| `orderBy` | AIP-132 order, as `?order_by=`; the list's `sort` when omitted. |
|
|
134
134
|
| `limit` | How many records, 1 to 20; 3 when omitted. |
|
|
135
135
|
| `marks` | The name of a hook in the UI config's `marks` that marks each record for the person reading, such as an unread conversation in bold with a count. See [Marks per reader](#marks-per-reader). Optional. |
|
|
136
|
+
| `typing` | `true`: "typing…" beside a record while someone else types in it, from the [realtime relay](realtime-relay.md#pages). Needs the view's conversation page with `typing`. See [Typing and unread from the relay](#typing-and-unread-from-the-relay). Optional. |
|
|
137
|
+
| `unread` | `true`: a record bold with the count of its unread messages, from the view's conversation page and its `readPosition`, kept by the realtime relay. See [Typing and unread from the relay](#typing-and-unread-from-the-relay). Optional. |
|
|
136
138
|
|
|
137
139
|
The entry then works like this:
|
|
138
140
|
|
|
@@ -143,6 +145,21 @@ The entry then works like this:
|
|
|
143
145
|
|
|
144
146
|
The records come from the list API with the caller's token, so they follow the same access rules as the list page: row filters such as `.own` apply, and when the list is refused the group says it could not load. Any write to the resource made in the admin refreshes them, and so does returning to the window, unless a record has unsaved changes, so statuses changed elsewhere show up. A record read anew, by its record page or by project code refetching `useRecord`, refreshes its group too when it shows another status or title than the group's row.
|
|
145
147
|
|
|
148
|
+
### Typing and unread from the relay
|
|
149
|
+
|
|
150
|
+
With the app's [realtime relay](realtime-relay.md#pages), a group of conversations needs no hook of its own for typing and unread messages:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
view<typeof conversations>('conversations')
|
|
154
|
+
.nav({ recent: { orderBy: 'name asc', limit: 20, typing: true, unread: true } })
|
|
155
|
+
.record({ conversation: { resource: 'messages', field: 'conversation_id', sender: 'author_id', time: 'sent_at', text: 'body', readPosition: { resource: 'read_positions', field: 'conversation_id', message: 'last_read_id' }, typing: true } })
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
- `typing` says "typing…" after a record's title while someone else types in it, wherever the reader is in the app, for six seconds after the last they heard, and not while the relay is down. The open conversation's row leaves it out: the line above its box says who types.
|
|
159
|
+
- `unread` draws a record bold, with the count of the messages others wrote after the reader's read position. It is counted as the group shows, in two requests, the reader's read positions and one aggregate of the messages grouped by conversation in the database, and again after the relay reconnects. In between, each new message the relay notices on a conversation adds one without reading anything; the open conversation counts none, and opening one clears its count.
|
|
160
|
+
|
|
161
|
+
Both mark the group's own records over a `marks` hook's, so a direct message's row can still name its person.
|
|
162
|
+
|
|
146
163
|
### Marks per reader
|
|
147
164
|
|
|
148
165
|
A record can stand out for the person reading, such as a channel with messages they have not read: in bold, with a count at the end of its row. The group names a hook under `marks` in `protobase.ui.tsx`, and the sidebar calls it with the resource's name while it shows the records. It returns each record's mark by the record's key as its address carries it, or `undefined` while it loads, so it can read the API as the reader with `useList`, `useClient` or live updates, and its marks follow them:
|
|
@@ -846,6 +846,7 @@ view<typeof channels>('channels').record({
|
|
|
846
846
|
| `thread` | Replies in a thread under a message, in a panel beside the conversation: `{ field, count, last }`. |
|
|
847
847
|
| `attachments` | [Files](#files-in-messages) a message carries, kept in a resource of their own: `{ resource, field, file, width, height }`. |
|
|
848
848
|
| `call` | The record's [call](calls.md), by the name of the [API function](../guides/api-functions.md) that admits people to it. |
|
|
849
|
+
| `typing` | `true`: a line above the box says who else types, by their names among the organization's members, and the others hear while the reader types, through the app's [realtime relay](realtime-relay.md#pages); the record's resource is one of its channel resources. |
|
|
849
850
|
|
|
850
851
|
The header shows the record's title, which the top bar repeats at mobile width and so shows there only with `members`, the subtitle, the call with `call`, and *Details*, a link to the record's standard page at `?standard=true`, with its sections, Edit and Delete. The latest 50 messages show oldest first, with *Load earlier messages* at the top for more. A day divider opens each day, and one person's messages sent within five minutes of each other show under their name once. A message resource with [`.live()`](live.md) shows new, changed and deleted messages as they happen. Every read goes through the reader's own access: a message they may not read never shows, and a reader who may not read the messages, a field each one needs or the fields of `thread` gets the standard page. The box shows for a reader who may create messages.
|
|
851
852
|
|