@protobase/cli 0.125.0 → 0.125.1

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.
@@ -0,0 +1,20 @@
1
+ import { projectResources } from "../config/project-parts.js";
2
+ import { relayAppPart } from "../relay/app-entry.js";
3
+ import { configExports } from "@protobase/server";
4
+ //#region src/build/bundle-relay.ts
5
+ /**
6
+ * What the manifest says of the config's `realtime.relay`: the app's part of its entry in the relay's apps file, from
7
+ * the config and its views, as `protobase relay app` prints it, without the address, id or keys a build does not hold.
8
+ * `undefined` without `realtime.relay`, so its presence means the app hands its connections to the relay.
9
+ */
10
+ const bundleRelay = async (config) => {
11
+ const relay = config.realtime?.relay;
12
+ if (!relay) return;
13
+ const views = configExports(await projectResources(config)).views.map((view) => view.toModel());
14
+ return relayAppPart(relay, {
15
+ apiPath: config.apiPath,
16
+ views
17
+ });
18
+ };
19
+ //#endregion
20
+ export { bundleRelay };
@@ -7,6 +7,7 @@ import { layoutCheck } from "./layout-check.js";
7
7
  import { extensionImports } from "../project/extensions.js";
8
8
  import { viteBundle } from "./vite-bundle.js";
9
9
  import { cliProtobaseIds, provideCliModules } from "../project/cli-modules.js";
10
+ import { bundleRelay } from "./bundle-relay.js";
10
11
  import { collectComponents } from "./component-names.js";
11
12
  import { bundleEntryCode } from "./entry-code.js";
12
13
  import { hostPackages } from "./packages/host-packages.js";
@@ -100,7 +101,8 @@ const buildConfigBundle = async ({ projectDir, outFile, bun, extensions = [], co
100
101
  return {
101
102
  native: [...native.values()],
102
103
  mobile: config.mobile,
103
- notifications: Boolean(config.notifications)
104
+ notifications: Boolean(config.notifications),
105
+ relay: await bundleRelay(config)
104
106
  };
105
107
  };
106
108
  //#endregion
@@ -53,7 +53,7 @@ const buildDeployBundle = async ({ projectDir, outDir, bun, extensions = [], sou
53
53
  await removeOwnFolder(outDir, bundleNodeModules, "nodeModules");
54
54
  await removeOwnFolder(outDir, bundleMigrations, "migrations");
55
55
  const components = /* @__PURE__ */ new Set();
56
- const { native, mobile, notifications } = await buildConfigBundle({
56
+ const { native, mobile, notifications, relay } = await buildConfigBundle({
57
57
  projectDir,
58
58
  outFile: path.join(outDir, bundleLayout.server),
59
59
  bun,
@@ -103,7 +103,8 @@ const buildDeployBundle = async ({ projectDir, outDir, bun, extensions = [], sou
103
103
  native: pinnedNative,
104
104
  ...mobileSection && { mobile: mobileSection },
105
105
  ...packages.length > 0 ? { nodeModules: bundleNodeModules } : {},
106
- ...migrations ? { migrations } : {}
106
+ ...migrations ? { migrations } : {},
107
+ ...relay && { relay }
107
108
  };
108
109
  await writeFile(path.join(outDir, manifestFile), `${JSON.stringify(manifest, null, 2)}\n`);
109
110
  return manifest;
@@ -21,11 +21,12 @@ const isPlugin = (value) => isObject(value) && typeof value.name === "string" &&
21
21
  const isShell = (value) => isObject(value) && typeof value.protobase === "string" && Number.isInteger(value.pluginApi) && isStringList(value.paths) && (value.builtAt === void 0 || isTime(value.builtAt));
22
22
  const isTime = (value) => Number.isSafeInteger(value) && value >= 0;
23
23
  const isVersions = (value) => isObject(value) && Object.values(value).every((item) => typeof item === "string");
24
+ const isRelay = (value) => isObject(value) && (value.client_events === void 0 || isObject(value.client_events) && isObject(value.client_events.channels)) && (value.presence === void 0 || isObject(value.presence) && typeof value.presence.path === "string" && Number.isInteger(value.presence.grace_seconds));
24
25
  const isIcons = (value) => isObject(value) && typeof value.favicon === "boolean" && isStringList(value.png) && value.png.every((icon) => icon in pngIcons);
25
26
  const parseManifest = (value, file) => {
26
27
  const manifest = value;
27
28
  if (manifest?.version !== 3) throw new Error(`${file} is not a version 3 bundle manifest; rebuild it with \`protobase build\``);
28
- const { builtAt, server, public: publicDir, api, spa, plugins, theme, shell, icons, native, mobile, nodeModules, migrations } = manifest;
29
+ const { builtAt, server, public: publicDir, api, spa, plugins, theme, shell, icons, native, mobile, nodeModules, migrations, relay } = manifest;
29
30
  if (typeof server !== "string" || typeof publicDir !== "string" || typeof spa !== "string" || typeof theme !== "string" || !isStringList(api)) throw new Error(`${file} needs server, public, spa and theme paths and an api list`);
30
31
  if (!Array.isArray(plugins) || !plugins.every(isPlugin)) throw new Error(`${file} needs a plugins list, each with a name, path and api`);
31
32
  if (!isShell(shell)) throw new Error(`${file} needs a shell with its protobase version, pluginApi and paths`);
@@ -35,6 +36,7 @@ const parseManifest = (value, file) => {
35
36
  if (mobile !== void 0 && !(isObject(mobile) && isVersions(mobile.plugins) && (mobile.appId === void 0 || typeof mobile.appId === "string") && (mobile.push === void 0 || typeof mobile.push === "boolean"))) throw new Error(`${file} has a mobile that is not { appId, plugins, push } with an app id, a map of package names to versions and true or false`);
36
37
  if (nodeModules !== void 0 && typeof nodeModules !== "string") throw new Error(`${file} has a nodeModules that is no path`);
37
38
  if (migrations !== void 0 && typeof migrations !== "string") throw new Error(`${file} has a migrations that is no path`);
39
+ if (relay !== void 0 && !isRelay(relay)) throw new Error(`${file} has a relay that is not { client_events, presence } with its channels and a presence path and grace`);
38
40
  return {
39
41
  version: 3,
40
42
  ...builtAt !== void 0 && { builtAt },
@@ -49,7 +51,8 @@ const parseManifest = (value, file) => {
49
51
  ...native && { native },
50
52
  ...mobile && { mobile },
51
53
  ...nodeModules === void 0 ? {} : { nodeModules },
52
- ...migrations === void 0 ? {} : { migrations }
54
+ ...migrations === void 0 ? {} : { migrations },
55
+ ...relay && { relay }
53
56
  };
54
57
  };
55
58
  const readManifest = async (bundleDir) => {
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.125.0";
2
+ var version = "0.125.1";
3
3
  //#endregion
4
4
  export { version };
@@ -10,9 +10,11 @@ const devLimits = {
10
10
  channels_per_ticket: 200,
11
11
  request_bytes: 1048576
12
12
  };
13
+ const devKey = () => randomBytes(32).toString("base64url");
13
14
  const devKeys = () => store.__protobaseDevRelayKeys ??= {
14
- apiKey: randomBytes(32).toString("base64url"),
15
- ticketKey: randomBytes(32).toString("base64url")
15
+ apiKey: devKey(),
16
+ ticketKey: devKey(),
17
+ presenceKey: devKey()
16
18
  };
17
19
  const relayEnded = (code) => {
18
20
  delete store.__protobaseDevRelay;
@@ -20,7 +22,7 @@ const relayEnded = (code) => {
20
22
  };
21
23
  /**
22
24
  * The config with a relay of its own when `realtime.relay` names none: without a `url`, protobase dev runs the relay
23
- * from its npm package for the app at `devUrl`, with keys it makes unless the config sets them, and gives the app its
25
+ * from its npm package for the app at `devUrl`, with keys it makes unless the config sets them, the presence key too, and gives the app its
24
26
  * address and keys; its apps file declares the events of `views`' typing conversation pages too. It starts on the first load whose config has `realtime.relay`, and again when an edit changes the
25
27
  * app's entry in its apps file; a config without `realtime.relay`, or one with a `url`, stops it.
26
28
  */
@@ -35,7 +37,11 @@ const withDevRelay = async (config, projectDir, devUrl, views) => {
35
37
  ...relay,
36
38
  url: devUrl,
37
39
  apiKey: relay.apiKey || keys.apiKey,
38
- ticketKey: relay.ticketKey || keys.ticketKey
40
+ ticketKey: relay.ticketKey || keys.ticketKey,
41
+ ...relay.presence && { presence: {
42
+ ...relay.presence,
43
+ key: relay.presence.key || keys.presenceKey
44
+ } }
39
45
  });
40
46
  const entry = JSON.stringify({
41
47
  ...relayAppEntry(options, {
@@ -3,29 +3,44 @@ import { relayClientEvents, relayPresencePath, typingResources } from "@protobas
3
3
  /** How long a person who closed their last socket still counts as present, so a phone that reconnects never goes offline. */
4
4
  const presenceGraceSeconds = 30;
5
5
  const systemPath = (apiPath = "/api/v1") => apiPath.slice(0, apiPath.lastIndexOf("/"));
6
- const presenceEntry = (presence, appUrl, apiPath) => {
6
+ /**
7
+ * The part of the app's entry in the relay's apps file that its build decides, without its address, id or any key: the
8
+ * events its clients may send on each channel resource, typing included where a conversation page types, and, with
9
+ * presence, the path of the app's endpoint its reports go to and their grace. `protobase build` writes it into the
10
+ * bundle's manifest, and `relayAppEntry` builds on it.
11
+ */
12
+ const relayAppPart = (relay, { apiPath, views }) => {
13
+ const clientEvents = relayClientEvents(relay.channels, typingResources(views));
14
+ return {
15
+ ...clientEvents && { client_events: clientEvents },
16
+ ...relay.presence && { presence: {
17
+ path: relayPresencePath(systemPath(apiPath)),
18
+ grace_seconds: presenceGraceSeconds
19
+ } }
20
+ };
21
+ };
22
+ const presenceEntry = ({ path, grace_seconds }, key, appUrl) => {
7
23
  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
24
  return {
9
- url: new URL(relayPresencePath(systemPath(apiPath)), appUrl).href,
10
- key: presence.key,
11
- grace_seconds: presenceGraceSeconds
25
+ url: new URL(path, appUrl).href,
26
+ key,
27
+ grace_seconds
12
28
  };
13
29
  };
14
30
  /**
15
- * The app's entry in the relay's apps file: its id, its keys, the browser origin of its URL, the events its clients
16
- * may send on each channel resource, typing included where a conversation page types, and, with presence, where its
17
- * reports go.
31
+ * The app's entry in the relay's apps file: its id, its keys, the browser origin of its URL, and its part of the build
32
+ * (`relayAppPart`), with presence's endpoint at the app's URL and its key.
18
33
  */
19
- const relayAppEntry = (relay, { url, apiPath, views }) => {
20
- const clientEvents = relayClientEvents(relay.channels, typingResources(views));
34
+ const relayAppEntry = (relay, { url, ...build }) => {
35
+ const { client_events, presence } = relayAppPart(relay, build);
21
36
  return {
22
37
  id: relay.app,
23
38
  api_keys: [relay.apiKey],
24
39
  ticket_keys: [relay.ticketKey],
25
40
  origins: url ? [new URL(url).origin] : [],
26
- ...clientEvents && { client_events: clientEvents },
27
- ...relay.presence && { presence: presenceEntry(relay.presence, url, apiPath) }
41
+ ...client_events && { client_events },
42
+ ...presence && relay.presence && { presence: presenceEntry(presence, relay.presence.key, url) }
28
43
  };
29
44
  };
30
45
  //#endregion
31
- export { relayAppEntry };
46
+ export { relayAppEntry, relayAppPart };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.125.0",
3
+ "version": "0.125.1",
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.125.0",
47
- "@protobase/layout": "0.125.0",
48
- "@protobase/query": "0.125.0",
49
- "@protobase/schema": "0.125.0",
50
- "@protobase/server": "0.125.0",
51
- "@protobase/ui": "0.125.0",
52
- "@protobase/workflows": "0.125.0",
46
+ "@protobase/config": "0.125.1",
47
+ "@protobase/layout": "0.125.1",
48
+ "@protobase/query": "0.125.1",
49
+ "@protobase/schema": "0.125.1",
50
+ "@protobase/server": "0.125.1",
51
+ "@protobase/ui": "0.125.1",
52
+ "@protobase/workflows": "0.125.1",
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",
@@ -17,7 +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. `protobase dev` runs a relay for the app by itself. An app on a platform that runs the relay picks up its address and keys from PROTOBASE_RELAY_URL, PROTOBASE_RELAY_APP, PROTOBASE_RELAY_API_KEY and PROTOBASE_RELAY_TICKET_KEY; a value in the config wins.
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. An app on a platform that runs the relay picks up its address and keys from PROTOBASE_RELAY_URL, PROTOBASE_RELAY_APP, PROTOBASE_RELAY_API_KEY, PROTOBASE_RELAY_TICKET_KEY and PROTOBASE_RELAY_PRESENCE_KEY; a value in the config wins. `protobase build` writes the app's events and presence for the relay's apps file into the bundle's manifest, and the platform's PROTOBASE_RELAY_ENABLED switch, true or false, says when it moves the app.
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, whose sidebar and members page show them online, away or offline.
22
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, also of a conversation the person was just added to, all by configuration; `client.relay` gives the same from code.
23
23
  - [Relay client events](../reference/realtime-relay.md#client-events) (in development): 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.
@@ -289,7 +289,11 @@ By default the bundle runs on Node and on Bun. `--bun` makes it a bundle for Bun
289
289
  "native": { "@capacitor/core": "8.5.3", "@capacitor/ios": "8.5.3" },
290
290
  "mobile": { "appId": "com.example.shop", "plugins": { "@capacitor/camera": "^8.0.1" }, "push": true },
291
291
  "nodeModules": "node_modules",
292
- "migrations": "migrations"
292
+ "migrations": "migrations",
293
+ "relay": {
294
+ "client_events": { "channels": { "conversations": { "events": { "typing": { "schema": { "type": "object" }, "max_bytes": 256, "per_second": 2, "burst": 4 } } } } },
295
+ "presence": { "path": "/api/relay/presence", "grace_seconds": 30 }
296
+ }
293
297
  }
294
298
  ```
295
299
 
@@ -302,6 +306,7 @@ By default the bundle runs on Node and on Bun. `--bun` makes it a bundle for Bun
302
306
  - `theme` is the project's [theme](theme.md), which the app links at `/protobase.theme.css`. It is always there, empty when the project has none.
303
307
  - `nodeModules` is present only when the config module imports [native packages](#native-packages). It is always the `node_modules` folder beside `server`, where Bun and Node resolve the module's imports from, and holds plain packages: JavaScript, `package.json` files and native binaries for linux x64 and arm64 (glibc), which the runtime loads with `dlopen`. A host keeps it next to the server module and must not mount it `noexec` (an add-on there then fails with `failed to map segment from shared object`). Its binaries link nothing but glibc, `libstdc++.so.6`, `libgcc_s.so.1` and libraries in the bundle itself, so the run image must have those three. Without `nodeModules`, the server module is the only file the runtime reads.
304
308
  - `migrations` is present only when the project has a `db/migrations` folder: its `.sql` files, which the runtime's `migrate` applies (see [Deploy](../guides/deploy.md)). A host never serves it.
309
+ - `relay` is present only when the config has [`realtime.relay`](realtime-relay.md): the app's part of its entry in the relay's apps file, as [`relay app`](#relay-app) prints it, without the address, id or any key: `client_events`, when a channel resource has events, and with presence `presence`, the path of the app's presence endpoint and the reports' `grace_seconds`. A host that runs the relay completes it with the app's id, keys, origin and presence key, and the presence endpoint at the app's address.
305
310
  - `api` lists the path prefixes the server answers. A path is under a prefix when it equals it or continues it with `/` (`/api` covers `/api/meta` and `/api/v1:batchWrite`, not `/apiary`). It is always `/api` and the two OAuth discovery documents [MCP clients](auth.md#oauth-for-mcp-clients) read, which answer 404 without MCP: a bundle assumes the API lives under `/api`, with the resources at the default `apiPath` `/api/v1`, `/api/meta`, the docs, the API functions and the built-in assistant beside them and Better Auth at `/api/auth`. A bundle built before them sends them to the UI: rebuild it. The serve runtime refuses a config that sets another `apiPath`.
306
311
  - Every other path is the UI's. A `GET` or `HEAD` for a file in `public` gets that file; a folder gets its `index.html`. Otherwise a request whose `Accept` header contains `text/html` (a navigation) gets `spa` with status 200, so deep links such as `/orders/42` load the app; anything else gets 404. Other methods get 405.
307
312
  - Files under an `assets/` folder, `public/assets/` and each plugin's, are named by their content hash and never change: serve them with `Cache-Control: public, max-age=31536000, immutable`, and everything else, `index.html`, each `plugin.json` and the theme, with `no-cache`.
@@ -482,7 +487,8 @@ Serves the API of a bundle from `protobase build` and owns the process around it
482
487
  | `PROTOBASE_RUNS_ON_NOTE` | `true` ends the sign-in page on the [Runs on Protobase note](app-identity.md#runs-on-protobase-note), for a hosting platform; default `false`, and any value but `true` or `false` stops startup |
483
488
  | `PROTOBASE_RUNTIME_URL` | the endpoint behind the top bar's [runtime update button](versioning.md#runtime-updates), such as a hosting platform's; without it there is no button |
484
489
  | `PROTOBASE_SENTRY_DSN` | the DSN of a Sentry-compatible error tracker the app's server and admin app send their errors to, after the config's `errorReporting` ([Error reporting](api.md#error-reporting)); without it nothing is sent |
485
- | `PROTOBASE_RELAY_URL`, `PROTOBASE_RELAY_APP`, `PROTOBASE_RELAY_API_KEY`, `PROTOBASE_RELAY_TICKET_KEY` | the address, the app's id and its API and ticket keys of the [realtime relay](realtime-relay.md#from-the-platform), each where the config's `realtime.relay` leaves it out; without `realtime.relay` the relay stays off |
490
+ | `PROTOBASE_RELAY_URL`, `PROTOBASE_RELAY_APP`, `PROTOBASE_RELAY_API_KEY`, `PROTOBASE_RELAY_TICKET_KEY`, `PROTOBASE_RELAY_PRESENCE_KEY` | the address, the app's id, its API and ticket keys and its presence key of the [realtime relay](realtime-relay.md#from-the-platform), each where the config's `realtime.relay` leaves it out; without `realtime.relay` the relay stays off |
491
+ | `PROTOBASE_RELAY_ENABLED` | `true` when the platform moves the app to the [realtime relay](realtime-relay.md#from-the-platform); `true` without `realtime.relay` in the config stops the server. Unset or empty is `false`; any other value stops the server |
486
492
  | `PROTOBASE_MAP_STYLE_URL` | the URL of the MapLibre style the record page's [maps](ui-config.md#maps) are drawn with, after the config's `maps`; without it there are no maps |
487
493
  | `PROTOBASE_SOURCES` | other Protobase APIs whose resources the app draws as its own, a JSON list of `{ "url", "label", "resources", "roles"? }` after the config's `remoteResources` ([Remote resources](remote-resources.md#declaring-a-source)) |
488
494
  | `PROTOBASE_FILES_<NAME>`, `PROTOBASE_FILES_RETENTION`, `PROTOBASE_FILES_SECRET` | [file providers](files.md#providers) and their settings, over `files` in the config; a bucket's credentials in `PROTOBASE_FILES_<NAME>_ACCESS_KEY_ID` and `_SECRET_ACCESS_KEY` |
@@ -678,7 +684,7 @@ Recounts a [time rollup](data-config.md#keeping-it-up-to-date)'s buckets from th
678
684
  protobase relay app
679
685
  ```
680
686
 
681
- 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`, or else `PROTOBASE_RELAY_APP`, `PROTOBASE_RELAY_API_KEY` and `PROTOBASE_RELAY_TICKET_KEY`, and in `origins` the origin of the app's `url` (or `PROTOBASE_URL`); in `client_events` each channel resource's [client events](realtime-relay.md#client-events), with `typing` on those whose conversation page types; 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.
687
+ 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`, or else `PROTOBASE_RELAY_APP`, `PROTOBASE_RELAY_API_KEY` and `PROTOBASE_RELAY_TICKET_KEY`, and in `origins` the origin of the app's `url` (or `PROTOBASE_URL`); in `client_events` each channel resource's [client events](realtime-relay.md#client-events), with `typing` on those whose conversation page types; with [presence](realtime-relay.md#presence), `presence` with the app's presence endpoint at that `url`, the presence key (or `PROTOBASE_RELAY_PRESENCE_KEY`) and a `grace_seconds` of 30. Its `client_events` and presence path and grace are the bundle manifest's [`relay`](#the-manifest). 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.
682
688
 
683
689
  ## `auth migration`
684
690
 
@@ -43,7 +43,7 @@ export default defineConfig(({ env }) => ({
43
43
  | `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)). |
44
44
  | `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. |
45
45
  | `channels.<resource>.events` | The [events](#client-events) the channel's members may send each other, by name, each with its payload's `schema`, its `maxBytes` and the rate one person may send it at, `perSecond` in bursts of `burst`. |
46
- | `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. |
46
+ | `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, which defaults to [`PROTOBASE_RELAY_PRESENCE_KEY`](#from-the-platform). |
47
47
 
48
48
  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, after the auth store's steps when the same file writes them, so a new app's first migration sets up both; the server checks it at start.
49
49
 
@@ -59,9 +59,14 @@ An app on a platform that runs the relay picks up its address and keys from the
59
59
  | `PROTOBASE_RELAY_APP` | `app` |
60
60
  | `PROTOBASE_RELAY_API_KEY` | `apiKey` |
61
61
  | `PROTOBASE_RELAY_TICKET_KEY` | `ticketKey` |
62
+ | `PROTOBASE_RELAY_PRESENCE_KEY` | `presence.key` |
62
63
 
63
64
  Each is a default on its own: a value in the config wins, and a key that neither sets stops the server at start, naming the key and its variable. The relay stays off without `realtime.relay` in the config, however many of these are set: the app turns it on by declaring its channels.
64
65
 
66
+ `PROTOBASE_RELAY_ENABLED` is the platform's switch: `true` when its apps file carries the app's entry, `false`, or unset or empty, otherwise; any other value stops the server at start, naming it. A config with `realtime.relay` uses the relay whatever it says. `true` without `realtime.relay` stops the server at start, as the channels and their members come only from the config. <gap>An app without `realtime.relay` cannot move to the relay with `PROTOBASE_RELAY_ENABLED` because deriving its channels and their members from its conversation pages is not implemented.</gap>
67
+
68
+ [`protobase build`](cli.md#the-manifest) writes the app's part of its apps file entry into the bundle's manifest, as `relay`: its `client_events` and, with presence, the presence endpoint's path and `grace_seconds`, without the address, id or any key. A platform reads it from each build and completes the entry with the app's id, keys, origin and presence key, and the endpoint at the app's address.
69
+
65
70
  ```ts
66
71
  export default defineConfig({
67
72
  realtime: { relay: { channels: { conversations: { members: { resource: 'conversation_members', channel: 'conversation_id', user: 'user_id' } } } } },
@@ -78,7 +83,7 @@ npm install --save-dev @protobase/realtime
78
83
 
79
84
  <gap>`protobase dev` cannot run the relay without the project installing `@protobase/realtime` because the CLI does not depend on it until the package is published.</gap>
80
85
 
81
- It starts the relay on the first load of a config with `realtime.relay`, an edit that adds it included, on a free port of `127.0.0.1`, serving this one app with the dev server's origin, and gives the app its address. The keys are the config's when it sets them, and otherwise made anew for each `protobase dev`. An edit that changes the app's id or keys starts it again, one that removes `realtime.relay` or sets a `url` stops it, and it stops with `dev`. Its own messages, such as an apps file it refuses, show in the terminal. With a `url`, the app uses that relay and `dev` starts none. `dev` reads [the platform's variables](#from-the-platform) for neither: the app's id comes from the config.
86
+ It starts the relay on the first load of a config with `realtime.relay`, an edit that adds it included, on a free port of `127.0.0.1`, serving this one app with the dev server's origin, and gives the app its address. The keys, the presence key included, are the config's when it sets them, and otherwise made anew for each `protobase dev`. An edit that changes the app's id or keys starts it again, one that removes `realtime.relay` or sets a `url` stops it, and it stops with `dev`. Its own messages, such as an apps file it refuses, show in the terminal. With a `url`, the app uses that relay and `dev` starts none. `dev` reads [the platform's variables](#from-the-platform) for neither: the app's id comes from the config.
82
87
 
83
88
  ## Tickets
84
89
 
@@ -122,7 +127,7 @@ The resource is keyed by its user field, its status field is text or an enum of
122
127
 
123
128
  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.
124
129
 
125
- 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.
130
+ 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, or `PROTOBASE_RELAY_PRESENCE_KEY`, 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.
126
131
 
127
132
  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.
128
133