@protobase/cli 0.121.1 → 0.121.2
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/dev/entry.js +2 -1
- package/dist/main.js +2 -0
- package/dist/migration/parts.js +7 -2
- package/dist/relay/app-entry.js +10 -0
- package/dist/relay/register.js +18 -0
- package/dist/serve/startup-checks.js +2 -1
- package/package.json +8 -8
- package/skills/protobase/SKILL.md +1 -0
- package/skills/protobase/references/features/history-comments-and-live-updates.md +3 -0
- package/skills/protobase/references/reference/cli.md +9 -1
- package/skills/protobase/references/reference/config/api.md +2 -2
- package/skills/protobase/references/reference/realtime-relay.md +72 -0
- package/skills/protobase/references/reference/webhooks.md +1 -1
package/dist/cli/package.js
CHANGED
package/dist/dev/entry.js
CHANGED
|
@@ -7,7 +7,7 @@ import { devRealtimePrune } from "./dev-prune.js";
|
|
|
7
7
|
import { devDatabase, loadDevConfig } from "./project.js";
|
|
8
8
|
import { withDevServiceWorker } from "./service-worker-route.js";
|
|
9
9
|
import { setMissing, supabaseEnv } from "./supabase-env.js";
|
|
10
|
-
import { apiKeySettings, assistantSettings, authSchemaDrift, authStoreTables, checkAi, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkResourcesOutsideAuthStore, checkStatusSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, hasUsers, relayed, sqlSends } from "@protobase/server";
|
|
10
|
+
import { apiKeySettings, assistantSettings, authSchemaDrift, authStoreTables, checkAi, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkRelaySchema, checkResourcesOutsideAuthStore, checkStatusSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, hasUsers, relayed, sqlSends } from "@protobase/server";
|
|
11
11
|
import { upgradeWebSocket } from "@hono/node-server";
|
|
12
12
|
import { Hono } from "hono";
|
|
13
13
|
//#region src/dev/entry.ts
|
|
@@ -42,6 +42,7 @@ if (project.webhooks) await checkWebhooksSchema(db, project.webhooks);
|
|
|
42
42
|
if (project.workflows) await checkWorkflowsSchema(db, project.workflows);
|
|
43
43
|
if (relayed(project.realtime)) await checkRealtimeRelay(db);
|
|
44
44
|
if (sqlSends(project.realtime)) await checkChannelSends(db);
|
|
45
|
+
if (project.realtime?.relay) await checkRelaySchema(db, project.auth);
|
|
45
46
|
if (project.notifications) await checkNotificationsSchema(db, project.notifications, project.auth);
|
|
46
47
|
if (project.status) await checkStatusSchema(db, project.status, project.auth);
|
|
47
48
|
if (project.userTables) await checkUserTablesSchema(db);
|
package/dist/main.js
CHANGED
|
@@ -10,6 +10,7 @@ import { registerEncryption } from "./encryption/register.js";
|
|
|
10
10
|
import { registerMigration } from "./migration/register.js";
|
|
11
11
|
import { registerMobile } from "./mobile/register.js";
|
|
12
12
|
import { registerNotifications } from "./notifications/register.js";
|
|
13
|
+
import { registerRelay } from "./relay/register.js";
|
|
13
14
|
import { protobaseVersion } from "./version/version.js";
|
|
14
15
|
import { registerSkill } from "./skill/register.js";
|
|
15
16
|
import { registerImport } from "./supabase/register.js";
|
|
@@ -202,6 +203,7 @@ registerSkill(program);
|
|
|
202
203
|
registerImport(program);
|
|
203
204
|
registerMobile(program);
|
|
204
205
|
registerNotifications(program);
|
|
206
|
+
registerRelay(program);
|
|
205
207
|
const run = (argv) => program.parseAsync(argv).catch((error) => {
|
|
206
208
|
if (error instanceof ExitSignal) {
|
|
207
209
|
process.exitCode = error.code;
|
package/dist/migration/parts.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { authMigrationTitle } from "../auth/migration-file.js";
|
|
2
|
-
import { authSchemaMigration, callsMigration, capturedBy, changesMigration, channelSendsMigration, commentsMigration, computedMigration, derivedMigration, jobsSchemaMigration, notificationsMigration, ownedDefinitionsMigration, queryRunnerMigration, realtimeRelayMigration, relayed, sqlSends, statusMigration, unsubscribesMigration, userTablesMigration, webhooksMigration, workflowsMigration } from "@protobase/server";
|
|
2
|
+
import { authSchemaMigration, callsMigration, capturedBy, changesMigration, channelSendsMigration, commentsMigration, computedMigration, derivedMigration, jobsSchemaMigration, notificationsMigration, ownedDefinitionsMigration, queryRunnerMigration, realtimeRelayMigration, relayMigration, relayed, sqlSends, statusMigration, unsubscribesMigration, userTablesMigration, webhooksMigration, workflowsMigration } from "@protobase/server";
|
|
3
3
|
//#region src/migration/parts.ts
|
|
4
4
|
/** Every feature the project's config enables, in the order their files are written. */
|
|
5
5
|
const migrationParts = (project) => {
|
|
@@ -11,7 +11,7 @@ const migrationParts = (project) => {
|
|
|
11
11
|
title: authMigrationTitle,
|
|
12
12
|
sql: () => authSchemaMigration(project.auth)
|
|
13
13
|
}] : [],
|
|
14
|
-
...project.jobs.length > 0 || project.models.some(capturedBy(project.models)) || comments || aiFields || project.webhooks || project.workflows || project.notifications ? [{
|
|
14
|
+
...project.jobs.length > 0 || project.models.some(capturedBy(project.models)) || comments || aiFields || project.webhooks || project.workflows || project.notifications || project.realtime?.relay ? [{
|
|
15
15
|
name: "jobs",
|
|
16
16
|
title: "The jobs schema this version of Protobase needs",
|
|
17
17
|
sql: () => jobsSchemaMigration(project.db)
|
|
@@ -86,6 +86,11 @@ const migrationParts = (project) => {
|
|
|
86
86
|
title: "The delete of the definitions of user tables that resources in code own now",
|
|
87
87
|
sql: () => ownedDefinitionsMigration(project.db, project.models)
|
|
88
88
|
}] : [],
|
|
89
|
+
...project.realtime?.relay ? [{
|
|
90
|
+
name: "relay",
|
|
91
|
+
title: "The trigger that closes a deleted sign-in session's sockets on the realtime relay",
|
|
92
|
+
sql: () => relayMigration(project.db, project.auth)
|
|
93
|
+
}] : [],
|
|
89
94
|
...sqlSends(project.realtime) ? [{
|
|
90
95
|
name: "channel_sends",
|
|
91
96
|
title: "SQL's broadcasts to the record channels, channels.send()",
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/relay/app-entry.ts
|
|
2
|
+
/** The app's entry in the relay's apps file: its id, its keys and the browser origin of its URL. */
|
|
3
|
+
const relayAppEntry = (relay, appUrl) => ({
|
|
4
|
+
id: relay.app,
|
|
5
|
+
api_keys: [relay.apiKey],
|
|
6
|
+
ticket_keys: [relay.ticketKey],
|
|
7
|
+
origins: appUrl ? [new URL(appUrl).origin] : []
|
|
8
|
+
});
|
|
9
|
+
//#endregion
|
|
10
|
+
export { relayAppEntry };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/relay/register.ts
|
|
2
|
+
const registerRelay = (program) => {
|
|
3
|
+
program.command("relay").description("The realtime relay the app hands its connections to (realtime.relay)").command("app").description("Print the app's entry for the relay's apps file, from realtime.relay and url").action(async () => {
|
|
4
|
+
const [{ checkRelayOptions }, { loadSourceConfig }, { loadNearestEnvFile }, { relayAppEntry }] = await Promise.all([
|
|
5
|
+
import("@protobase/server"),
|
|
6
|
+
import("../config/source-config.js"),
|
|
7
|
+
import("../dev/env.js"),
|
|
8
|
+
import("./app-entry.js")
|
|
9
|
+
]);
|
|
10
|
+
loadNearestEnvFile(process.cwd());
|
|
11
|
+
const config = await loadSourceConfig(process.cwd(), { command: "tool" });
|
|
12
|
+
if (!config.realtime?.relay) throw new Error("The config has no realtime.relay");
|
|
13
|
+
const url = config.url ?? process.env.PROTOBASE_URL;
|
|
14
|
+
process.stdout.write(`${JSON.stringify(relayAppEntry(checkRelayOptions(config.realtime.relay), url), null, 2)}\n`);
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { registerRelay };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assistantSettings, authStoreTables, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkResourcesOutsideAuthStore, checkStatusSchema, checkUnsubscribesSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, relayed, sqlSends } from "@protobase/server";
|
|
1
|
+
import { assistantSettings, authStoreTables, checkAuthSchema, checkCalls, checkChanges, checkChannelSends, checkComments, checkComputed, checkDerived, checkNotificationsSchema, checkQueryRunner, checkRealtimeRelay, checkRelaySchema, checkResourcesOutsideAuthStore, checkStatusSchema, checkUnsubscribesSchema, checkUserTablesSchema, checkWebhooksSchema, checkWorkflowsSchema, configExports, relayed, sqlSends } from "@protobase/server";
|
|
2
2
|
//#region src/serve/startup-checks.ts
|
|
3
3
|
/**
|
|
4
4
|
* The server's startup checks of the database: each throws, with what to write as a migration, while a schema the
|
|
@@ -22,6 +22,7 @@ const checkStartup = async (project, db) => {
|
|
|
22
22
|
if (project.workflows) await checkWorkflowsSchema(db, project.workflows);
|
|
23
23
|
if (relayed(project.realtime)) await checkRealtimeRelay(db);
|
|
24
24
|
if (sqlSends(project.realtime)) await checkChannelSends(db);
|
|
25
|
+
if (project.realtime?.relay) await checkRelaySchema(db, project.auth);
|
|
25
26
|
if (project.notifications) await checkNotificationsSchema(db, project.notifications, project.auth);
|
|
26
27
|
if (project.status) await checkStatusSchema(db, project.status, project.auth);
|
|
27
28
|
if (project.unsubscribe) await checkUnsubscribesSchema(db);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.121.
|
|
3
|
+
"version": "0.121.2",
|
|
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.2",
|
|
47
|
+
"@protobase/layout": "0.121.2",
|
|
48
|
+
"@protobase/query": "0.121.2",
|
|
49
|
+
"@protobase/schema": "0.121.2",
|
|
50
|
+
"@protobase/server": "0.121.2",
|
|
51
|
+
"@protobase/ui": "0.121.2",
|
|
52
|
+
"@protobase/workflows": "0.121.2",
|
|
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",
|
|
@@ -84,6 +84,7 @@ Reference:
|
|
|
84
84
|
- When adding comments to records → read [Record comments](references/reference/comments.md)
|
|
85
85
|
- When open pages should refresh as records change → read [Live updates](references/reference/live.md)
|
|
86
86
|
- When pages on a record should see who else is there or send each other short-lived events (presence, broadcast) → read [Channels](references/reference/channels.md)
|
|
87
|
+
- When the app should hand its realtime connections to a relay, so it can sleep while people stay connected (tickets, channel members, change notices) → read [Realtime relay](references/reference/realtime-relay.md)
|
|
87
88
|
- When an app uses supabase-js Realtime (`postgres_changes` under RLS, broadcast, presence) → read [Supabase Realtime](references/reference/supabase-realtime.md)
|
|
88
89
|
- When filling fields with a model → read [AI fields and actions](references/reference/ai.md)
|
|
89
90
|
- When letting people without an account create records → read [Public forms](references/reference/forms.md)
|
|
@@ -17,4 +17,7 @@ The changes of each record, comments on it, and pages that refresh as records ch
|
|
|
17
17
|
- [Change stream](../reference/live.md#the-stream): Apps follow record changes over a stream that catches up after a reconnect.
|
|
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
|
+
- [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.
|
|
21
|
+
- Relay presence (coming soon): The relay reports who comes online, goes away and goes offline, and the app records it, raises its events and tells the right people.
|
|
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.
|
|
20
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()`.
|
|
@@ -634,7 +634,7 @@ Applies the project's `db/migrations/*.sql` that the database has not applied ye
|
|
|
634
634
|
protobase migration [--dir db/migrations] [--extend <dir>]
|
|
635
635
|
```
|
|
636
636
|
|
|
637
|
-
Writes what the database still needs for every feature the config enables, as the project's next migrations in `--dir`, one file per feature: the auth schema when sign-in is on (`NNN_auth.sql`, as [`auth migration`](#auth-migration) writes it), the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks`, `workflows` or `notifications`, or a resource has events, history, comments or an [AI field](ai.md) (`NNN_jobs.sql`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`NNN_changes.sql`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas), the triggers that keep the rollups, and the tables, indexes and refresh functions of [time rollups](data-config.md#time-rollups) (`NNN_computed.sql`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`NNN_query.sql`), the [comments table](comments.md#the-table) (`NNN_comments.sql`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`NNN_webhooks.sql`), the [workflows, versions, runs and steps](workflows.md#turning-them-on) of `workflows` (`NNN_workflows.sql`), the [devices and history tables](notifications.md#table-names) of `notifications` (`NNN_notifications.sql`), the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`NNN_unsubscribes.sql`), the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) (`NNN_realtime_relay.sql`), the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) (`NNN_channel_sends.sql`), and, with [`userTables`](user-tables.md#the-switch), the schemas of user tables (`NNN_user_tables.sql`) and the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`NNN_owned_definitions.sql`). It compares the database, with the project's migrations applied, with the config (for the auth schema, it reads the store's record of the migrations it applied), merged with those of `--extend` as `dev` merges them, and writes nothing when the database is current. Apply the files with the project's other migrations. It reads the `.env` nearest the current directory and needs `DATABASE_URL`.
|
|
637
|
+
Writes what the database still needs for every feature the config enables, as the project's next migrations in `--dir`, one file per feature: the auth schema when sign-in is on (`NNN_auth.sql`, as [`auth migration`](#auth-migration) writes it), the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks`, `workflows` or `notifications`, or a resource has events, history, comments or an [AI field](ai.md) (`NNN_jobs.sql`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`NNN_changes.sql`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas), the triggers that keep the rollups, and the tables, indexes and refresh functions of [time rollups](data-config.md#time-rollups) (`NNN_computed.sql`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`NNN_query.sql`), the [comments table](comments.md#the-table) (`NNN_comments.sql`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`NNN_webhooks.sql`), the [workflows, versions, runs and steps](workflows.md#turning-them-on) of `workflows` (`NNN_workflows.sql`), the [devices and history tables](notifications.md#table-names) of `notifications` (`NNN_notifications.sql`), the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`NNN_unsubscribes.sql`), the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) (`NNN_realtime_relay.sql`), the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) (`NNN_channel_sends.sql`), the trigger of [`realtime.relay`](realtime-relay.md#sessions) that ends a deleted session's sockets (`NNN_relay.sql`), and, with [`userTables`](user-tables.md#the-switch), the schemas of user tables (`NNN_user_tables.sql`) and the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`NNN_owned_definitions.sql`). It compares the database, with the project's migrations applied, with the config (for the auth schema, it reads the store's record of the migrations it applied), merged with those of `--extend` as `dev` merges them, and writes nothing when the database is current. Apply the files with the project's other migrations. It reads the `.env` nearest the current directory and needs `DATABASE_URL`.
|
|
638
638
|
|
|
639
639
|
```sh
|
|
640
640
|
bun run db:migrate # the database at the project's current schema
|
|
@@ -642,6 +642,14 @@ bun run protobase migration # writes db/migrations/NNN_auth.sql, NNN_jobs.
|
|
|
642
642
|
bun run db:migrate # applies them
|
|
643
643
|
```
|
|
644
644
|
|
|
645
|
+
## `relay app`
|
|
646
|
+
|
|
647
|
+
```sh
|
|
648
|
+
protobase relay app
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
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.
|
|
652
|
+
|
|
645
653
|
## `auth migration`
|
|
646
654
|
|
|
647
655
|
```sh
|
|
@@ -151,10 +151,10 @@ export default defineConfig({ runtimeUpdates: false })
|
|
|
151
151
|
|
|
152
152
|
## realtime
|
|
153
153
|
|
|
154
|
-
- **Type:** `{ processes?: 'one' | 'many', sqlSends?: boolean }`
|
|
154
|
+
- **Type:** `{ processes?: 'one' | 'many', sqlSends?: boolean, relay?: { url, app, apiKey, ticketKey, channels } }`
|
|
155
155
|
- **Default:** `{ processes: 'one', sqlSends: false }`
|
|
156
156
|
|
|
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).
|
|
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).
|
|
158
158
|
|
|
159
159
|
```ts
|
|
160
160
|
export default defineConfig({ realtime: { processes: 'many', sqlSends: true } })
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from docs/website/src/content/docs/reference/realtime-relay.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Realtime relay
|
|
4
|
+
|
|
5
|
+
Hand the app's realtime connections to a relay, so the app and its database can sleep while people stay connected.
|
|
6
|
+
|
|
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
|
+
|
|
9
|
+
:::caution[In development]
|
|
10
|
+
The app's side is built: tickets, membership, change notices and session ends. <gap>Pages cannot connect to the relay from `@protobase/client` because its relay client is not implemented.</gap> <gap>The relay cannot report presence to the app because the app's presence endpoint 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
|
+
:::
|
|
12
|
+
|
|
13
|
+
## Config
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
export default defineConfig(({ env }) => ({
|
|
17
|
+
realtime: {
|
|
18
|
+
relay: {
|
|
19
|
+
url: env.RELAY_URL!,
|
|
20
|
+
app: 'chat',
|
|
21
|
+
apiKey: env.RELAY_API_KEY!,
|
|
22
|
+
ticketKey: env.RELAY_TICKET_KEY!,
|
|
23
|
+
channels: {
|
|
24
|
+
conversations: {
|
|
25
|
+
members: { resource: 'conversation_members', channel: 'conversation_id', user: 'user_id' },
|
|
26
|
+
mentions: 'mentions',
|
|
27
|
+
},
|
|
28
|
+
rooms: {},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
}))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Key | |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `url` | The relay's address, `https://` or `http://`. The app sends its commands there, and clients connect to its `/socket`. |
|
|
38
|
+
| `app` | The app's id on the relay: 1 to 64 of `a-z`, `0-9`, `_` and `-`. |
|
|
39
|
+
| `apiKey`, `ticketKey` | One of the app's API keys and one of its ticket keys on the relay, each at least 32 characters. |
|
|
40
|
+
| `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
|
+
| `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
|
+
| `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. |
|
|
43
|
+
|
|
44
|
+
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
|
+
|
|
46
|
+
[`protobase relay app`](cli.md#relay-app) prints the app's entry for the relay's apps file, from this config and the app's `url`.
|
|
47
|
+
|
|
48
|
+
## Tickets
|
|
49
|
+
|
|
50
|
+
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.
|
|
51
|
+
|
|
52
|
+
- **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`.
|
|
53
|
+
- **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.
|
|
54
|
+
- **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).
|
|
55
|
+
|
|
56
|
+
## Notices
|
|
57
|
+
|
|
58
|
+
The [dispatch](events.md) that turns each transaction's changes into events also hands them to the relay: in its own transaction it reads each change's audience once, from the members tables, and queues one `relay.send` job with the commands for the batch. A record of a channel resource is noticed on its own channel, and a record of a resource with a relation to one on the channel it points at, both where it was and where it moved. Each notice is one `change` event to the channel, the same bytes to every socket it reaches:
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{ "kind": "create", "resource": "messages", "key": "42", "channel": "conversations/7", "mentions": ["u2"] }
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`kind` is `create`, `update`, `delete` or `undelete`; `mentions` is there with `mentions` set. A notice tells clients what changed, so they read only the records they show. On a channel with members it reaches its members as the database holds them at dispatch, and a person who joined or left with that change; on an open channel, everyone joined to it. The relay sends a notice once, however often the job sends it again.
|
|
65
|
+
|
|
66
|
+
A members record that is created or undeleted adds its person to the channel on the sockets they have open, before its notice, and one that is deleted or soft-deleted takes them out after it: they can join, or are closed out, without connecting again. Commands go in commit order, one job at a time; while the relay is down, they wait and try again, once a minute at most. A command the relay refuses, such as one over its size limit, goes to the app's error reporter and is passed over.
|
|
67
|
+
|
|
68
|
+
<gap>An open channel's notices still reach the sockets joined to it after its record stops being public, until they reconnect, because checking read access again on the relay is not implemented.</gap>
|
|
69
|
+
|
|
70
|
+
## Sessions
|
|
71
|
+
|
|
72
|
+
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.
|
|
@@ -90,7 +90,7 @@ A function target is called as the subscription's `as`, so `context.caller()` an
|
|
|
90
90
|
|
|
91
91
|
## Delivery and retries
|
|
92
92
|
|
|
93
|
-
- **Every delivery is a job of its own:** the dispatch job queues a `webhooks.deliver` job for each delivery, in the transaction that writes the change's history and live notices, and tries none itself, so a slow receiver never holds back a live update or another delivery. A
|
|
93
|
+
- **Every delivery is a job of its own:** the dispatch job queues a `webhooks.deliver` job for each delivery, in the transaction that writes the change's history and live notices, and tries none itself, so a slow receiver never holds back a live update or another delivery. A write through the API starts the dispatch at once, and a write from elsewhere at the dispatch's next poll. The first try then waits for a worker's next poll, since the delivery job is queued in a transaction: within about two seconds of an API write and about four of one from elsewhere, with the default [poll interval](jobs.md) of 2 seconds.
|
|
94
94
|
- **A failed try** queues the next `webhooks.deliver` job, retried on the Standard Webhooks schedule with up to 10% jitter: 5 s, 5 m, 30 m, 2 h, 5 h, 10 h, 14 h, 20 h and 24 h, 10 tries over about three days. A `Retry-After` header makes the wait longer, up to a day.
|
|
95
95
|
- **`410 Gone`** fails the delivery at once.
|
|
96
96
|
- **After the last try** the job is kept as failed, on the [failed jobs page](jobs.md#failed-jobs-page), where it can be retried.
|