@protobase/cli 0.121.10 → 0.121.11
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 +1 -1
- package/skills/protobase/references/reference/cli.md +1 -1
- package/skills/protobase/references/reference/config/api.md +1 -1
- package/skills/protobase/references/reference/realtime-relay.md +32 -2
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.11",
|
|
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.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",
|
|
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,6 @@ 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 (
|
|
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
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.
|
|
23
23
|
- [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).
|
|
@@ -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. <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>
|
|
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
|
|
|
@@ -61,7 +63,7 @@ It starts the relay on the first load of a config with `realtime.relay`, an edit
|
|
|
61
63
|
|
|
62
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.
|
|
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,31 @@ 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>
|