@protobase/cli 0.121.11 → 0.121.13

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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.121.11";
2
+ var version = "0.121.13";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.121.11",
3
+ "version": "0.121.13",
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.11",
47
- "@protobase/layout": "0.121.11",
48
- "@protobase/query": "0.121.11",
49
- "@protobase/schema": "0.121.11",
50
- "@protobase/server": "0.121.11",
51
- "@protobase/ui": "0.121.11",
52
- "@protobase/workflows": "0.121.11",
46
+ "@protobase/config": "0.121.13",
47
+ "@protobase/layout": "0.121.13",
48
+ "@protobase/query": "0.121.13",
49
+ "@protobase/schema": "0.121.13",
50
+ "@protobase/server": "0.121.13",
51
+ "@protobase/ui": "0.121.13",
52
+ "@protobase/workflows": "0.121.13",
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",
@@ -19,5 +19,6 @@ The changes of each record, comments on it, and pages that refresh as records ch
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
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 client events (coming soon): Clients send each other events such as typing through the relay, as each channel resource declares them, with a schema, a size and a rate.
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()`.
@@ -8,7 +8,7 @@ Sign-in methods, users, roles, organizations and tokens.
8
8
  - [Sign-in page](../reference/client.md#the-admin-app): The app signs people in at its own address, where the browser's Back returns to the step before, and signing in comes back to the page they were on.
9
9
  - [First admin](../reference/auth.md#first-run): No account can be made over the web while the app has no users. You create the first admin from the terminal, and the last active admin cannot be deleted, demoted or blocked.
10
10
  - [Users from a script](../reference/auth.md#first-run): A seed or another script of the project creates and lists users, and mints their tokens, on the app's own sign-in.
11
- - [Versioned sign-in tables](../reference/auth.md#migrations): The sign-in tables come from numbered migrations that ship with Protobase, written as the project's own and recorded in the database, so a deploy checks them without comparing tables. Every feature has its tables from the start, so turning one on or off needs no migration.
11
+ - [Versioned sign-in tables](../reference/auth.md#migrations): The sign-in tables come from numbered steps that ship with Protobase, a chain for each module of sign-in, written as the project's own migrations and recorded in the database, so a deploy checks them without comparing tables. Every step only adds, and one that assumes something of the rows already there checks it first and names the rows to fix. Every feature has its tables from the start, so turning one on or off needs no migration.
12
12
  - [Table and column names](../reference/auth.md#table-and-column-names): Every sign-in table can take another name or a prefix, and every column but `id` another name, from the config, and the migrations, sign-in and Protobase's own queries all use it.
13
13
  - [Existing users table](../reference/auth.md#an-existing-users-table): Sign-in can use the users table another system keeps in the same database.
14
14
  - [Users page](../reference/auth.md#users-page): Admins find people, set roles, block them, add accounts and invite people by email.
@@ -26,17 +26,17 @@ Users, sessions and signing keys stay apart from the application's tables, in a
26
26
 
27
27
  ### Migrations
28
28
 
29
- The store's tables come from migrations that ship with Protobase, one version after another, and are part of the project's migrations, committed in the repository like the application's own: the examples carry them as `db/migrations/*_auth.sql`, and their `db:migrate` applies both. The store records each version it applied in its `auth_migrations` table. When an upgrade of Protobase brings new versions, write them as the project's next migration:
29
+ The store's tables come from steps that ship with Protobase, and are part of the project's migrations, committed in the repository like the application's own: the examples carry them as `db/migrations/*_auth.sql`, and their `db:migrate` applies both. Each module of sign-in has its own numbered chain of steps, the only source of its tables: `auth` (users, sessions, accounts, codes and signing keys), `two_factor`, `passkeys`, `sign_in_policy`, `staff_sign_in`, `organizations`, `mcp`, `personal_access_tokens`, `mail_senders` and `platform_devices`. A step is written against the store's [names](#table-and-column-names), so it makes the tables and columns under the names the app configures, and a step that adds columns to another module's table, such as `organizations` adding the organization of each user, session and personal access token, adds them only while that module is on. The store records the versions of its steps it applied in its `auth_migrations` table, one row per version, the numbers of the single chain the store had before its modules had chains of their own. When an upgrade of Protobase brings new steps, write them as the project's next migration:
30
30
 
31
31
  ```sh
32
32
  bun run db:migrate # the database at the project's current schema
33
- bun run protobase auth migration # reads auth_migrations; writes the versions not applied as db/migrations/NNN_auth.sql
33
+ bun run protobase auth migration # reads auth_migrations; writes the steps not applied as db/migrations/NNN_auth.sql
34
34
  bun run db:migrate # applies them
35
35
  ```
36
36
 
37
- Each version's statements are followed by its row in `auth_migrations`. The file only adds tables, columns and indexes, and the `pg_trgm` extension that the index of the users' names needs, never drops one, and is plain SQL to review, edit or leave out where the project handles the auth schema itself. `--dir` and `--name` choose the folder and the name after the number. [`protobase migration`](cli.md#migration) writes the same file, along with what the config's other features need, such as [jobs](jobs.md).
37
+ Each step's statements come under a comment naming its module, number and name, such as `-- auth 2 account_keys: ...`, and the last step of a version is followed by its row in `auth_migrations`. The file only adds tables, nullable columns, columns with a default, and indexes, and the `pg_trgm` extension that the index of the users' names needs, never drops, retypes or tightens anything, so it is safe to apply unattended; Protobase's tests lint every step it ships for this. A step that assumes something of the rows already there starts with a check that refuses before it changes anything: `auth` step 2, which makes one account per provider and account id, stops when two accounts share both, naming them, so you keep one of each in a migration of your own written before it, then apply again. The file is plain SQL to review, edit or leave out where the project handles the auth schema itself. `--dir` and `--name` choose the folder and the name after the number. [`protobase migration`](cli.md#migration) writes the same file, along with what the config's other features need, such as [jobs](jobs.md).
38
38
 
39
- Every store has the tables of every feature, whether the app turns the feature on or not: [passkeys](#passkeys), [two-factor authentication](#two-factor-authentication), [organizations](#organizations), the [OAuth provider for MCP](#oauth-for-mcp-clients) and [personal access tokens](personal-access-tokens.md). Turning a feature on or off needs no migration and drops nothing; the columns a feature adds to the user and session tables are empty until it is on. SAML providers and open sign-up keep nothing in the store.
39
+ Every store has the tables of every module, whether the app turns the feature on or not: [passkeys](#passkeys), [two-factor authentication](#two-factor-authentication), [organizations](#organizations), the [OAuth provider for MCP](#oauth-for-mcp-clients) and [personal access tokens](personal-access-tokens.md). Turning a feature on or off needs no migration and drops nothing; the columns a feature adds to the user and session tables are empty until it is on. SAML providers and open sign-up keep nothing in the store. <gap>A store cannot leave out the tables of a feature the app does not use because recording the steps a store has, rather than versions, is not implemented.</gap>
40
40
 
41
41
  The names the app configures, the store's schema ([`auth.schema`](config/sign-in.md#authschema)), its [tables and columns](#table-and-column-names), and uuid ids (`generateUserId: 'uuid'`), are the ones in effect when the file is written. Changing one later is a migration of the project's own, such as a rename. <gap>`protobase auth migration` cannot rename a table or column the store has already because writing a rename from a changed name is not implemented.</gap>
42
42
 
@@ -44,7 +44,7 @@ The server never changes the schema, and never compares it with what Better Auth
44
44
 
45
45
  No resource may be over a table of the store, which holds password hashes, sessions, tokens and signing keys: `protobase serve` and `protobase dev` refuse to start with one, naming it, whatever the table is called, and [`protobase scaffold`](cli.md#scaffold) never writes one. The API, the Supabase-compatible endpoints, live updates and MCP read the app's resources only, so none of them reads the store.
46
46
 
47
- A store made before 0.48, when `protobase auth migration` compared the database with Better Auth's tables, has no `auth_migrations`. For it, `protobase auth migration` writes the step that adopts it, once: it runs each version in a scratch schema, in a transaction it rolls back, to find the tables and columns that version makes, records each version whose tables and columns the store has, with their types, as applied, and writes the versions whose tables it has none of, such as MCP's for an app that never turned MCP on. The file checks again, when it is applied, that the store has every table and column it adopts, and stops otherwise, naming them. A store that has only some of a version's tables and columns, or has them with other types, is refused, naming what differs: bring it to the schema of Protobase 0.47 first, with that version's `protobase auth migration`. A store set up by Protobase 0.6, with Better Auth's camelCase names, or by `auth:migrate` before it, goes through 0.47 the same way.
47
+ A store made before 0.48, when `protobase auth migration` compared the database with Better Auth's tables, has no `auth_migrations`. For it, `protobase auth migration` writes the step that adopts it, once: it runs every step in a scratch schema, in a transaction it rolls back, to find the tables and columns each version makes, records each version whose tables and columns the store has, with their types, as applied, and writes the steps of the versions whose tables it has none of, such as MCP's for an app that never turned MCP on. The file checks again, when it is applied, that the store has every table and column it adopts, and stops otherwise, naming them. A store that has only some of a version's tables and columns, or has them with other types, is refused, naming what differs: bring it to the schema of Protobase 0.47 first, with that version's `protobase auth migration`. A store set up by Protobase 0.6, with Better Auth's camelCase names, or by `auth:migrate` before it, goes through 0.47 the same way.
48
48
 
49
49
  ### Table and column names
50
50
 
@@ -633,7 +633,7 @@ Register the app with the provider as a confidential client with the redirect UR
633
633
  - **The project's own provider wins:** with `socialProviders.github` in code and `PROTOBASE_SIGN_IN_PROVIDER=github`, the project's GitHub app handles the sign-in and the platform's is left out.
634
634
  - **The provider id:** set `PROTOBASE_SIGN_IN_PROVIDER=github` only when the provider's `sub` is the GitHub user id, as Protobase Cloud's is. Its links are then those of Better Auth's own GitHub provider, so an app that later brings its own GitHub app keeps every person's link. Dex's and Keycloak's `sub` is their own id, so keep the default `oidc` for them.
635
635
  - **Startup:** the provider's settings are read when the app starts; when they cannot be, the provider is left out until the next start (the status and sign-in page do not offer it) and every other way to sign in works.
636
- - **Linking by address, safely:** a provider's first sign-in for someone links their account only when the token says the provider verified the address (`email_verified: true`) and the account's own address is verified. Once linked, the link alone finds them: the provider's id and `sub`, whatever address the token carries later. A person linked to one subject of a provider is not linked to a second one by address (`403 PROVIDER_ACCOUNT_LINKED`), unless the provider signs in [devices](#device-accounts). The store holds each provider's account once, and for a provider that links one subject per person, one account per person (unique indexes on the provider and account id, and on the provider and person, auth store migration 11), so two first sign-ins at the same moment link one, whether they bring the same subject or two.
636
+ - **Linking by address, safely:** a provider's first sign-in for someone links their account only when the token says the provider verified the address (`email_verified: true`) and the account's own address is verified. Once linked, the link alone finds them: the provider's id and `sub`, whatever address the token carries later. A person linked to one subject of a provider is not linked to a second one by address (`403 PROVIDER_ACCOUNT_LINKED`), unless the provider signs in [devices](#device-accounts). The store holds each provider's account once, and for a provider that links one subject per person, one account per person (unique indexes on the provider and account id, and on the provider and person, the `auth` module's step 2), so two first sign-ins at the same moment link one, whether they bring the same subject or two.
637
637
  - The [sign-in policy](#sign-in-policy) has one row for every one of them, and `GET /api/auth/status` names those with a button as `platformProviders: [{ provider, name }]` besides listing them in `socialProviders`, and those that take [token sign-in](#token-sign-in), by token alone too, as `tokenProviders: [provider]`, so a client such as a phone app knows before it posts an ID token whether the app takes it, and through which provider.
638
638
 
639
639
  Set the first three together or none: some but not all of them stops the app at start with an error. In the config, `auth.signInProviders` takes a list of the same settings with `scopes`, `tokenSignIn`, `authorizationParams`, `hidden`, `button` and `devices`, and `[]` turns them off whatever the environment says:
@@ -668,9 +668,9 @@ PROTOBASE_SIGN_IN_PHONE_TOKENS=true
668
668
  A provider that gives each device a subject of its own, such as one that signs in a phone app, can have every device be an account of its own: `PROTOBASE_SIGN_IN_DEVICES=true` (`devices: true`), with [token sign-in](#token-sign-in) alone, without a client secret.
669
669
 
670
670
  - **Linked by address:** a device's first sign-in links it to the account with the address the provider vouches for, as for any provider, both addresses verified. A person holds as many of the provider's accounts as they have devices.
671
- - **Its sessions:** each session a device's token sign-in starts is kept as the device's in the store's `device_session` table (auth store migration 11), which goes with the session, so it lasts as long as the session however often it is renewed. A sign-in that waits for the [second step](#two-factor-authentication) is the device's once the step is taken.
671
+ - **Its sessions:** each session a device's token sign-in starts is kept as the device's in the store's `device_session` table (the `platform_devices` module's step 1), which goes with the session, so it lasts as long as the session however often it is renewed. A sign-in that waits for the [second step](#two-factor-authentication) is the device's once the step is taken.
672
672
  - **On the account page:** the **Devices** tab lists each device as the provider's name with the device's own name, which the device's ID token carries in the `device_name` claim (a string), with when it was linked, and the sessions it began under it rather than among the others. The connected accounts do not list them. `GET /api/auth/account/device-links` answers `{ links: [{ id, provider, name, device, linkedAt, sessions }] }`, `sessions` being the tokens of the sessions it began; `GET /api/auth/status` says `deviceLinks: true`.
673
- - **Sign out:** **Sign out** on a device, `POST /api/auth/account/device-links/sign-out` with its `id`, refuses its subject from then on, ends every session it began, however many, in one statement in the store, and removes its link, in that order: a token for it answers `403 DEVICE_SIGNED_OUT` and never links again, by address or otherwise, and a sign-in with it that was under way ends its own session. A sign-out that stopped half way is taken again with the same `id`. The person's other devices stay signed in, and a new device of the same address links as the first ones did. The refused subjects are kept in the store's `refused_account` table (auth store migration 11), and go when the person is deleted. A [staff session](#staff-sign-in) can do neither.
673
+ - **Sign out:** **Sign out** on a device, `POST /api/auth/account/device-links/sign-out` with its `id`, refuses its subject from then on, ends every session it began, however many, in one statement in the store, and removes its link, in that order: a token for it answers `403 DEVICE_SIGNED_OUT` and never links again, by address or otherwise, and a sign-in with it that was under way ends its own session. A sign-out that stopped half way is taken again with the same `id`. The person's other devices stay signed in, and a new device of the same address links as the first ones did. The refused subjects are kept in the store's `refused_account` table (the `platform_devices` module's step 1), and go when the person is deleted. A [staff session](#staff-sign-in) can do neither.
674
674
 
675
675
  ### Token sign-in
676
676
 
@@ -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, session ends and presence. <gap>Pages cannot connect to the relay from `@protobase/client` because its relay client is not implemented.</gap> <gap>Clients cannot send each other events such as typing through the relay because declaring client events per channel resource is not implemented.</gap>
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
@@ -61,7 +61,7 @@ It starts the relay on the first load of a config with `realtime.relay`, an edit
61
61
 
62
62
  ## Tickets
63
63
 
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, expiresAt }`: the relay's socket address and a ticket that connects once, within a minute. Every connection, a reconnect too, takes a new ticket.
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.
65
65
 
66
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`.
67
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.
@@ -112,3 +112,58 @@ Each change of a person's presence is also published on `people` as a `presence_
112
112
  ```
113
113
 
114
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