diff --git a/codex-plugin/plugins/spacetimedb/skills/cli/SKILL.md b/codex-plugin/plugins/spacetimedb/skills/cli/SKILL.md index 6e7c140b7d6..e1e09f5932b 100644 --- a/codex-plugin/plugins/spacetimedb/skills/cli/SKILL.md +++ b/codex-plugin/plugins/spacetimedb/skills/cli/SKILL.md @@ -52,11 +52,11 @@ spacetime generate --lang unrealcpp --uproject-dir ./MyGame --module-path ./serv ### Client and Module Bindings -`spacetime generate --bindings client` (default) generates bindings for clients connecting to a database. `--bindings module` generates receiver bindings used inside a **calling database module** to call reducers in another **receiving database** through asynchronous IDC. Rust and TypeScript module bindings are supported; C#/C++ module bindings are not implemented yet. `unrealcpp` is client-only; `cpp` is accepted only in module mode. +`spacetime generate --bindings client` (default) generates bindings for clients connecting to a database. `--bindings module` generates receiver bindings used inside a **sending database module** to call reducers in another **receiving database** through asynchronous IDC. Rust and TypeScript module bindings are supported; C#/C++ module bindings are not implemented yet. `unrealcpp` is client-only; `cpp` is accepted only in module mode. -In `spacetime.json`, each calling database's non-inherited `dependencies` array requires its own `module-path` and dependency `name` plus receiving-module `module-path` or `database`. A `database` reference resolves another target's local source/build settings in the same config; it does not fetch a running database's schema. A direct receiving module source takes precedence. Dependencies do not themselves publish receiving databases or inherit the calling module's build settings. `generate ` processes both its regular `generate` entries (client bindings by default) and `dependencies` (module bindings automatically); no `--bindings` flag or second `generate` entry is needed for a dependency. For dependencies, module binding language defaults to the calling module's language and Rust/TypeScript output defaults to `/src/remote_bindings`. Explicit config `out-dir` is config-relative. A regular `generate` entry can select `"bindings": "module"`, but does not use dependency language or output defaults. The CLI `--bindings` override is allowed only when a single generation entry is selected. +In `spacetime.json`, each sending database's non-inherited `dependencies` array requires its own `module-path` and dependency `name` plus receiving-module `module-path` or `database`. A `database` reference resolves another target's local source/build settings in the same config; it does not fetch a running database's schema. A direct receiving module source takes precedence. Dependencies do not themselves publish receiving databases or inherit the sending module's build settings. `generate ` processes both its regular `generate` entries (client bindings by default) and `dependencies` (module bindings automatically); no `--bindings` flag or second `generate` entry is needed for a dependency. For dependencies, module binding language defaults to the sending module's language and Rust/TypeScript output defaults to `/src/remote_bindings`. Explicit config `out-dir` is config-relative. A regular `generate` entry can select `"bindings": "module"`, but does not use dependency language or output defaults. The CLI `--bindings` override is allowed only when a single generation entry is selected. -`dev` generates only root dependencies before building the calling module, and watches their source paths. Child targets are available for source lookup, not recursive dependency generation. `--skip-generate` skips receiver/client generation and receiver-only watching; `--skip-publish` does not. See https://spacetimedb.com/docs/cli-reference/spacetime-json#receiver-dependencies for examples. +`dev` generates only root dependencies before building the sending module, and watches their source paths. Child targets are available for source lookup, not recursive dependency generation. `--skip-generate` skips receiver/client generation and receiver-only watching; `--skip-publish` does not. See https://spacetimedb.com/docs/cli-reference/spacetime-json#receiver-dependencies for examples. ### Publishing & Deployment diff --git a/codex-plugin/plugins/spacetimedb/skills/concepts/SKILL.md b/codex-plugin/plugins/spacetimedb/skills/concepts/SKILL.md index 8ea6e8f9cfb..41d030039d1 100644 --- a/codex-plugin/plugins/spacetimedb/skills/concepts/SKILL.md +++ b/codex-plugin/plugins/spacetimedb/skills/concepts/SKILL.md @@ -99,6 +99,12 @@ Server-side modules can be written in: Rust, C#, TypeScript, C++ Lifecycle: Write → Compile → Publish (`spacetime publish`) → Hot-swap (republish without disconnecting clients) +## Inter-Database Communication + +Reducers can't make network requests, but a reducer can call a reducer in another database on the same server through an **outbox table** (async IDC). The reducer inserts a row, and after its transaction commits, the row is delivered as a reducer call to the target database exactly once, in order with other calls through the same outbox to the same database. The sending reducer doesn't wait, and the two databases don't commit together: the receiving reducer runs later in its own transaction and can fail. An optional `on_result` reducer in the sending database receives the result, so it can undo or record the work. + +Use it to split an application into databases that scale and deploy independently, such as a lobby that sends players to separate game world databases. Only Rust and TypeScript modules can declare outbox tables. See the language-specific server skills for syntax. + ## Identity - **Identity**: A long-lived, globally unique identifier for a user. diff --git a/codex-plugin/plugins/spacetimedb/skills/rust-server/SKILL.md b/codex-plugin/plugins/spacetimedb/skills/rust-server/SKILL.md index 0e282242263..58854ab3539 100644 --- a/codex-plugin/plugins/spacetimedb/skills/rust-server/SKILL.md +++ b/codex-plugin/plugins/spacetimedb/skills/rust-server/SKILL.md @@ -257,6 +257,63 @@ ctx.db.tick_timer().insert(TickTimer { scheduled_id: 0, scheduled_at: at }); Construct a connection ID from a numeric representation with `ConnectionId::from_u128(value)`. +## Inter-Database Calls (Async IDC) + +A reducer calls a reducer in another database by inserting a row into an outbox table. The call is sent only if the transaction commits, runs exactly once, and runs in order with other calls through the same outbox to the same database. The sending reducer doesn't wait for it. Both databases must be on the same server. + +Generate receiver bindings by listing the receiving module under `dependencies` in the sending database's `spacetime.json`, then running `spacetime generate `. Rust bindings are written to `src/remote_bindings/`. + +```json +{ + "database": "lobby", + "module-path": "./lobby", + "dependencies": [{ "name": "game_world", "database": "game-world" }], + "children": [{ "database": "game-world", "module-path": "./game_world" }] +} +``` + +```rust +mod remote_bindings; +use remote_bindings::game_world; + +// Each row is one call to game_world's `admit_player(name: String)` reducer. +#[spacetimedb::table(accessor = admit_player_outbox, outbox(game_world::admit_player))] +pub struct AdmitPlayerOutbox { + #[primary_key] + #[auto_inc] + msg_id: u64, // exactly one u64 auto_inc primary key + #[target] + world: game_world::Identity, // exactly one target, typed by the bindings + name: String, // one column per reducer parameter, same name and type + // #[param(name = name)] player_name: String, // alias a column to a parameter + // Extra columns are allowed. They aren't sent, but on_result can read them. +} + +#[spacetimedb::reducer] +pub fn send_to_world(ctx: &ReducerContext, name: String, world: game_world::Identity) { + // Sent if and only if this transaction commits. + ctx.db.admit_player_outbox().insert(AdmitPlayerOutbox { msg_id: 0, world, name }); +} + +// Optional, at most one per outbox. Private. Runs once per row, then the row is deleted. +#[spacetimedb::reducer(on_result(admit_player_outbox))] +pub fn on_admit_player_result(ctx: &ReducerContext, row: AdmitPlayerOutbox, result: Result<(), String>) { + if let Err(error) = result { + log::warn!("{} was not admitted: {error}", row.name); + } +} +``` + +- The receiving reducer is an ordinary reducer and needs no changes. In it, `ctx.sender()` is the sending database's identity, so authorize calls by checking it against an allow-list table. +- The receiving database runs `client_connected` and `client_disconnected` around every delivered call, with no JWT. If `client_connected` rejects the sending database, the call is retried forever. +- Outbox rows are insert-only, so a pending call can't be cancelled. A call is never retried once it has a result. To try again, insert a new row, for example from `on_result`. +- Wrap a plain `Identity` to get a target: `game_world::Identity(identity)`. +- Index lookups (`find`, `filter`) on a `game_world::Identity` column don't compile. Iterate and compare instead. +- An outbox table can't also be a scheduled table or an event table. +- After the receiving reducer's parameters change, regenerate the bindings and republish the sending database. Calls built from old bindings fail with a signature mismatch error. + +See https://spacetimedb.com/docs/databases/inter-database-communication for details. + ## Procedures and HTTP Procedures use `&mut ProcedureContext` and may return typed values: diff --git a/codex-plugin/plugins/spacetimedb/skills/typescript-server/SKILL.md b/codex-plugin/plugins/spacetimedb/skills/typescript-server/SKILL.md index 8563a5e9808..42bc4d80ba1 100644 --- a/codex-plugin/plugins/spacetimedb/skills/typescript-server/SKILL.md +++ b/codex-plugin/plugins/spacetimedb/skills/typescript-server/SKILL.md @@ -259,6 +259,68 @@ export const tick = spacetimedb.reducer( // const micros = at.tag === 'time' ? at.value : at.value.microsSinceUnixEpoch; // bigint ``` +## Inter-Database Calls (Async IDC) + +A reducer calls a reducer in another database by inserting a row into an outbox table. The call is sent only if the transaction commits, runs exactly once, and runs in order with other calls through the same outbox to the same database. The sending reducer doesn't wait for it. Both databases must be on the same server. + +Generate receiver bindings by listing the receiving module under `dependencies` in the sending database's `spacetime.json`, then running `spacetime generate `. TypeScript bindings are written to `src/remote_bindings/`. + +```json +{ + "database": "lobby", + "module-path": "./lobby", + "dependencies": [{ "name": "game_world", "database": "game-world" }], + "children": [{ "database": "game-world", "module-path": "./game_world" }] +} +``` + +```typescript +import gw from './remote_bindings/game_world'; + +// Each row is one call to game_world's `admitPlayer({ name })` reducer. +const admitPlayerOutbox = table( + { outbox: { receiver: gw.reducers.admitPlayer } }, + { + msgId: t.u64().primaryKey().autoInc(), // exactly one u64 autoInc primary key + world: gw.identity().target(), // exactly one target, typed by the bindings + name: t.string(), // one column per reducer parameter, same name and type + // playerName: t.string().param({ name: 'name' }), // alias a column to a parameter + // Extra columns are allowed. They aren't sent, but onResult can read them. + } +); +// Shorthand: gw.outbox.admitPlayer() declares { msgId, target, ...params } for you. +// It can't be used for a reducer with a parameter named msgId or target. + +const spacetimedb = schema({ admitPlayerOutbox }); +export default spacetimedb; + +export const sendToWorld = spacetimedb.reducer( + { name: t.string(), world: gw.identity() }, + (ctx, { name, world }) => { + // Sent if and only if this transaction commits. + ctx.db.admitPlayerOutbox.insert({ msgId: 0n, world, name }); + } +); + +// Optional, at most one per outbox. Private. Runs once per row, then the row is deleted. +export const onAdmitPlayerResult = spacetimedb.reducer( + { onResult: admitPlayerOutbox }, + { row: admitPlayerOutbox.rowType, result: t.result(t.unit(), t.string()) }, + (ctx, { row, result }) => { + if ('err' in result) console.warn(`${row.name} was not admitted: ${result.err}`); + } +); +``` + +- The receiving reducer is an ordinary reducer and needs no changes. In it, `ctx.sender` is the sending database's identity, so authorize calls by checking it against an allow-list table. +- The receiving database runs `clientConnected` and `clientDisconnected` around every delivered call, with no JWT. If `clientConnected` rejects the sending database, the call is retried forever. +- Outbox rows are insert-only, so a pending call can't be cancelled. A call is never retried once it has a result. To try again, insert a new row, for example from `onResult`. +- A plain `Identity` can't be a target. Use values typed `gw.identity()`, such as reducer arguments or columns of that type. Index lookups on those columns accept a plain `Identity`. +- An outbox table can't also be a scheduled table or an event table. +- After the receiving reducer's parameters change, regenerate the bindings and republish the sending database. Calls built from old bindings fail with a signature mismatch error. + +See https://spacetimedb.com/docs/databases/inter-database-communication for details. + ## Custom Types ```typescript diff --git a/skills/cli/SKILL.md b/skills/cli/SKILL.md index 6e7c140b7d6..e1e09f5932b 100644 --- a/skills/cli/SKILL.md +++ b/skills/cli/SKILL.md @@ -52,11 +52,11 @@ spacetime generate --lang unrealcpp --uproject-dir ./MyGame --module-path ./serv ### Client and Module Bindings -`spacetime generate --bindings client` (default) generates bindings for clients connecting to a database. `--bindings module` generates receiver bindings used inside a **calling database module** to call reducers in another **receiving database** through asynchronous IDC. Rust and TypeScript module bindings are supported; C#/C++ module bindings are not implemented yet. `unrealcpp` is client-only; `cpp` is accepted only in module mode. +`spacetime generate --bindings client` (default) generates bindings for clients connecting to a database. `--bindings module` generates receiver bindings used inside a **sending database module** to call reducers in another **receiving database** through asynchronous IDC. Rust and TypeScript module bindings are supported; C#/C++ module bindings are not implemented yet. `unrealcpp` is client-only; `cpp` is accepted only in module mode. -In `spacetime.json`, each calling database's non-inherited `dependencies` array requires its own `module-path` and dependency `name` plus receiving-module `module-path` or `database`. A `database` reference resolves another target's local source/build settings in the same config; it does not fetch a running database's schema. A direct receiving module source takes precedence. Dependencies do not themselves publish receiving databases or inherit the calling module's build settings. `generate ` processes both its regular `generate` entries (client bindings by default) and `dependencies` (module bindings automatically); no `--bindings` flag or second `generate` entry is needed for a dependency. For dependencies, module binding language defaults to the calling module's language and Rust/TypeScript output defaults to `/src/remote_bindings`. Explicit config `out-dir` is config-relative. A regular `generate` entry can select `"bindings": "module"`, but does not use dependency language or output defaults. The CLI `--bindings` override is allowed only when a single generation entry is selected. +In `spacetime.json`, each sending database's non-inherited `dependencies` array requires its own `module-path` and dependency `name` plus receiving-module `module-path` or `database`. A `database` reference resolves another target's local source/build settings in the same config; it does not fetch a running database's schema. A direct receiving module source takes precedence. Dependencies do not themselves publish receiving databases or inherit the sending module's build settings. `generate ` processes both its regular `generate` entries (client bindings by default) and `dependencies` (module bindings automatically); no `--bindings` flag or second `generate` entry is needed for a dependency. For dependencies, module binding language defaults to the sending module's language and Rust/TypeScript output defaults to `/src/remote_bindings`. Explicit config `out-dir` is config-relative. A regular `generate` entry can select `"bindings": "module"`, but does not use dependency language or output defaults. The CLI `--bindings` override is allowed only when a single generation entry is selected. -`dev` generates only root dependencies before building the calling module, and watches their source paths. Child targets are available for source lookup, not recursive dependency generation. `--skip-generate` skips receiver/client generation and receiver-only watching; `--skip-publish` does not. See https://spacetimedb.com/docs/cli-reference/spacetime-json#receiver-dependencies for examples. +`dev` generates only root dependencies before building the sending module, and watches their source paths. Child targets are available for source lookup, not recursive dependency generation. `--skip-generate` skips receiver/client generation and receiver-only watching; `--skip-publish` does not. See https://spacetimedb.com/docs/cli-reference/spacetime-json#receiver-dependencies for examples. ### Publishing & Deployment diff --git a/skills/concepts/SKILL.md b/skills/concepts/SKILL.md index 8ea6e8f9cfb..41d030039d1 100644 --- a/skills/concepts/SKILL.md +++ b/skills/concepts/SKILL.md @@ -99,6 +99,12 @@ Server-side modules can be written in: Rust, C#, TypeScript, C++ Lifecycle: Write → Compile → Publish (`spacetime publish`) → Hot-swap (republish without disconnecting clients) +## Inter-Database Communication + +Reducers can't make network requests, but a reducer can call a reducer in another database on the same server through an **outbox table** (async IDC). The reducer inserts a row, and after its transaction commits, the row is delivered as a reducer call to the target database exactly once, in order with other calls through the same outbox to the same database. The sending reducer doesn't wait, and the two databases don't commit together: the receiving reducer runs later in its own transaction and can fail. An optional `on_result` reducer in the sending database receives the result, so it can undo or record the work. + +Use it to split an application into databases that scale and deploy independently, such as a lobby that sends players to separate game world databases. Only Rust and TypeScript modules can declare outbox tables. See the language-specific server skills for syntax. + ## Identity - **Identity**: A long-lived, globally unique identifier for a user. diff --git a/skills/rust-server/SKILL.md b/skills/rust-server/SKILL.md index 0e282242263..58854ab3539 100644 --- a/skills/rust-server/SKILL.md +++ b/skills/rust-server/SKILL.md @@ -257,6 +257,63 @@ ctx.db.tick_timer().insert(TickTimer { scheduled_id: 0, scheduled_at: at }); Construct a connection ID from a numeric representation with `ConnectionId::from_u128(value)`. +## Inter-Database Calls (Async IDC) + +A reducer calls a reducer in another database by inserting a row into an outbox table. The call is sent only if the transaction commits, runs exactly once, and runs in order with other calls through the same outbox to the same database. The sending reducer doesn't wait for it. Both databases must be on the same server. + +Generate receiver bindings by listing the receiving module under `dependencies` in the sending database's `spacetime.json`, then running `spacetime generate `. Rust bindings are written to `src/remote_bindings/`. + +```json +{ + "database": "lobby", + "module-path": "./lobby", + "dependencies": [{ "name": "game_world", "database": "game-world" }], + "children": [{ "database": "game-world", "module-path": "./game_world" }] +} +``` + +```rust +mod remote_bindings; +use remote_bindings::game_world; + +// Each row is one call to game_world's `admit_player(name: String)` reducer. +#[spacetimedb::table(accessor = admit_player_outbox, outbox(game_world::admit_player))] +pub struct AdmitPlayerOutbox { + #[primary_key] + #[auto_inc] + msg_id: u64, // exactly one u64 auto_inc primary key + #[target] + world: game_world::Identity, // exactly one target, typed by the bindings + name: String, // one column per reducer parameter, same name and type + // #[param(name = name)] player_name: String, // alias a column to a parameter + // Extra columns are allowed. They aren't sent, but on_result can read them. +} + +#[spacetimedb::reducer] +pub fn send_to_world(ctx: &ReducerContext, name: String, world: game_world::Identity) { + // Sent if and only if this transaction commits. + ctx.db.admit_player_outbox().insert(AdmitPlayerOutbox { msg_id: 0, world, name }); +} + +// Optional, at most one per outbox. Private. Runs once per row, then the row is deleted. +#[spacetimedb::reducer(on_result(admit_player_outbox))] +pub fn on_admit_player_result(ctx: &ReducerContext, row: AdmitPlayerOutbox, result: Result<(), String>) { + if let Err(error) = result { + log::warn!("{} was not admitted: {error}", row.name); + } +} +``` + +- The receiving reducer is an ordinary reducer and needs no changes. In it, `ctx.sender()` is the sending database's identity, so authorize calls by checking it against an allow-list table. +- The receiving database runs `client_connected` and `client_disconnected` around every delivered call, with no JWT. If `client_connected` rejects the sending database, the call is retried forever. +- Outbox rows are insert-only, so a pending call can't be cancelled. A call is never retried once it has a result. To try again, insert a new row, for example from `on_result`. +- Wrap a plain `Identity` to get a target: `game_world::Identity(identity)`. +- Index lookups (`find`, `filter`) on a `game_world::Identity` column don't compile. Iterate and compare instead. +- An outbox table can't also be a scheduled table or an event table. +- After the receiving reducer's parameters change, regenerate the bindings and republish the sending database. Calls built from old bindings fail with a signature mismatch error. + +See https://spacetimedb.com/docs/databases/inter-database-communication for details. + ## Procedures and HTTP Procedures use `&mut ProcedureContext` and may return typed values: diff --git a/skills/typescript-server/SKILL.md b/skills/typescript-server/SKILL.md index 8563a5e9808..42bc4d80ba1 100644 --- a/skills/typescript-server/SKILL.md +++ b/skills/typescript-server/SKILL.md @@ -259,6 +259,68 @@ export const tick = spacetimedb.reducer( // const micros = at.tag === 'time' ? at.value : at.value.microsSinceUnixEpoch; // bigint ``` +## Inter-Database Calls (Async IDC) + +A reducer calls a reducer in another database by inserting a row into an outbox table. The call is sent only if the transaction commits, runs exactly once, and runs in order with other calls through the same outbox to the same database. The sending reducer doesn't wait for it. Both databases must be on the same server. + +Generate receiver bindings by listing the receiving module under `dependencies` in the sending database's `spacetime.json`, then running `spacetime generate `. TypeScript bindings are written to `src/remote_bindings/`. + +```json +{ + "database": "lobby", + "module-path": "./lobby", + "dependencies": [{ "name": "game_world", "database": "game-world" }], + "children": [{ "database": "game-world", "module-path": "./game_world" }] +} +``` + +```typescript +import gw from './remote_bindings/game_world'; + +// Each row is one call to game_world's `admitPlayer({ name })` reducer. +const admitPlayerOutbox = table( + { outbox: { receiver: gw.reducers.admitPlayer } }, + { + msgId: t.u64().primaryKey().autoInc(), // exactly one u64 autoInc primary key + world: gw.identity().target(), // exactly one target, typed by the bindings + name: t.string(), // one column per reducer parameter, same name and type + // playerName: t.string().param({ name: 'name' }), // alias a column to a parameter + // Extra columns are allowed. They aren't sent, but onResult can read them. + } +); +// Shorthand: gw.outbox.admitPlayer() declares { msgId, target, ...params } for you. +// It can't be used for a reducer with a parameter named msgId or target. + +const spacetimedb = schema({ admitPlayerOutbox }); +export default spacetimedb; + +export const sendToWorld = spacetimedb.reducer( + { name: t.string(), world: gw.identity() }, + (ctx, { name, world }) => { + // Sent if and only if this transaction commits. + ctx.db.admitPlayerOutbox.insert({ msgId: 0n, world, name }); + } +); + +// Optional, at most one per outbox. Private. Runs once per row, then the row is deleted. +export const onAdmitPlayerResult = spacetimedb.reducer( + { onResult: admitPlayerOutbox }, + { row: admitPlayerOutbox.rowType, result: t.result(t.unit(), t.string()) }, + (ctx, { row, result }) => { + if ('err' in result) console.warn(`${row.name} was not admitted: ${result.err}`); + } +); +``` + +- The receiving reducer is an ordinary reducer and needs no changes. In it, `ctx.sender` is the sending database's identity, so authorize calls by checking it against an allow-list table. +- The receiving database runs `clientConnected` and `clientDisconnected` around every delivered call, with no JWT. If `clientConnected` rejects the sending database, the call is retried forever. +- Outbox rows are insert-only, so a pending call can't be cancelled. A call is never retried once it has a result. To try again, insert a new row, for example from `onResult`. +- A plain `Identity` can't be a target. Use values typed `gw.identity()`, such as reducer arguments or columns of that type. Index lookups on those columns accept a plain `Identity`. +- An outbox table can't also be a scheduled table or an event table. +- After the receiving reducer's parameters change, regenerate the bindings and republish the sending database. Calls built from old bindings fail with a signature mismatch error. + +See https://spacetimedb.com/docs/databases/inter-database-communication for details. + ## Custom Types ```typescript