Loading...
Loading...
SpacetimeDB development best practices for TypeScript server modules and client SDK. This skill should be used when writing, reviewing, or refactoring SpacetimeDB code to ensure optimal patterns for real-time, multiplayer applications. Triggers on tasks involving SpacetimeDB modules, tables, reducers, subscriptions, or React integration.
npx skill4agent add isyn/stdb-skills spacetimedb-best-practicesspacetimedb| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Module Design | CRITICAL | |
| 2 | Table Schema & Indexing | CRITICAL | |
| 3 | Reducer Patterns | HIGH | |
| 4 | Subscription Optimization | HIGH | |
| 5 | Client State Management | MEDIUM-HIGH | |
| 6 | React Integration | MEDIUM | |
| 7 | TypeScript Patterns | MEDIUM | |
| 8 | Real-time Sync | LOW-MEDIUM | |
import { spacetimedb, table, t, ReducerContext } from 'spacetimedb';
// Define tables with the table builder
const Player = table(
{ name: 'player', public: true },
{
identity: t.identity().primaryKey(),
name: t.string(),
score: t.u64().index(),
isOnline: t.bool().index(),
}
);
// Define reducers
spacetimedb.reducer('create_player', { name: t.string() }, (ctx: ReducerContext, { name }) => {
ctx.db.player.insert({
identity: ctx.sender,
name,
score: 0n,
isOnline: true,
});
});
// Lifecycle hooks
spacetimedb.init((ctx: ReducerContext) => { /* module init */ });
spacetimedb.clientConnected((ctx: ReducerContext) => { /* client connected */ });
spacetimedb.clientDisconnected((ctx: ReducerContext) => { /* client disconnected */ });import { DbConnection } from './generated';
// Build connection
const conn = DbConnection.builder()
.withUri('ws://localhost:3000')
.withModuleName('my-module')
.onConnect((ctx, identity, token) => {
// Setup subscriptions
conn.subscription(['SELECT * FROM player WHERE isOnline = true']);
})
.onDisconnect((ctx, error) => { /* handle disconnect */ })
.build();
// Call reducers
await conn.reducers.createPlayer('Alice');
// Access tables
const player = conn.db.player.identity.find(identity);import { useTable, where, eq } from 'spacetimedb/react';
import { DbConnection, Player } from './generated';
function OnlinePlayers() {
const { rows: players } = useTable<DbConnection, Player>(
'player',
where(eq('isOnline', true))
);
return players.map(p => <div key={p.identity.toHexString()}>{p.name}</div>);
}module-single-responsibilitymodule-lifecyclemodule-error-handlingmodule-type-exportstable-primary-keystable-indexing.index()table-relationshipstable-column-typesreducer-atomicityreducer-validationreducer-authorizationreducer-batch-operationssubscription-selectivesubscription-filterssubscription-cleanupsubscription-batchingclient-connection-lifecycleclient-optimistic-updatesclient-error-recoveryclient-identityreact-use-subscriptionreact-table-hooksuseTable<DbConnection, Type>()react-reducer-hooksconn.reducers.*react-connection-statusts-generated-typests-strict-modets-discriminated-unionsts-type-guardssync-conflict-resolutionsync-offline-supportsync-debounce-updatessync-presencerules/module-single-responsibility.md
rules/table-primary-keys.md
rules/_sections.mdAGENTS.md