docs/docs/00100-intro/00200-quickstarts/00300-nodejs.md
import { InstallCardLink } from "@site/src/components/InstallCardLink"; import { StepByStep, Step, StepText, StepCode } from "@site/src/components/Steps";
Get a SpacetimeDB Node.js app running in under 5 minutes.
This starts the local SpacetimeDB server, publishes your module, generates TypeScript bindings, and runs the Node.js client.
</StepText>
<StepCode>
spacetime dev --template nodejs-ts
</StepCode>
Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `src/main.ts` to build your Node.js client.
</StepText>
<StepCode>
my-spacetime-app/
├── spacetimedb/ # Your SpacetimeDB module
│ └── src/
│ └── index.ts # Server-side logic
├── src/
│ ├── main.ts # Node.js client script
│ └── module_bindings/ # Auto-generated types
└── package.json
</StepCode>
Tables store your data. Reducers are functions that modify data — they're the only way to write to the database.
</StepText>
<StepCode>
import { schema, table, t } from 'spacetimedb/server';
const spacetimedb = schema({
person: table(
{ public: true },
{
name: t.string(),
}
),
});
export default spacetimedb;
export const add = spacetimedb.reducer(
{ name: t.string() },
(ctx, { name }) => {
ctx.db.person.insert({ name });
}
);
export const sayHello = spacetimedb.reducer(ctx => {
for (const person of ctx.db.person.iter()) {
console.info(`Hello, ${person.name}!`);
}
console.info('Hello, World!');
});
</StepCode>
spacetime call say_hello
spacetime sql "SELECT * FROM person"
</StepCode>
</Step>
<Step title="Understand the client code">
<StepText>
Open `src/main.ts` to see the Node.js client. It uses `DbConnection.builder()` to connect to SpacetimeDB, subscribes to tables, and registers callbacks for insert/delete events. Unlike browser apps, Node.js stores the authentication token in a file instead of localStorage.
</StepText>
<StepCode>
```typescript
import { DbConnection } from './module_bindings/index.js';
DbConnection.builder()
.withUri(HOST)
.withDatabaseName(DB_NAME)
.withToken(loadToken()) // Load saved token from file
.onConnect((conn, identity, token) => {
console.log('Connected! Identity:', identity.toHexString());
saveToken(token); // Save token for future connections
// Subscribe to all tables
conn.subscriptionBuilder()
.onApplied((ctx) => {
// Show current people
const people = [...ctx.db.person.iter()];
console.log('Current people:', people.length);
})
.subscribeToAllTables();
// Listen for table changes
conn.db.person.onInsert((ctx, person) => {
console.log(`[Added] ${person.name}`);
});
})
.build();
</StepCode>
spacetime sql "SELECT * FROM person" name
"Alice" "Bob" "Charlie"
spacetime call say_hello
spacetime logs 2025-01-13T12:00:00.000000Z INFO: Hello, Alice! 2025-01-13T12:00:00.000000Z INFO: Hello, Bob! 2025-01-13T12:00:00.000000Z INFO: Hello, Charlie! 2025-01-13T12:00:00.000000Z INFO: Hello, World!
</StepCode>
</Step>
<Step title="Node.js considerations">
<StepText>
**WebSocket support:** Node.js 22+ has native WebSocket support. For Node.js 18-21, the SDK automatically uses the `undici` package (included in devDependencies).
**Environment variables:** Configure the connection using `SPACETIMEDB_HOST` and `SPACETIMEDB_DB_NAME` environment variables.
**Exiting:** Press Ctrl+C to stop the client.
</StepText>
<StepCode>
```bash
# Configure via environment variables
SPACETIMEDB_HOST=ws://localhost:3000 \
SPACETIMEDB_DB_NAME=my-app \
npm run start
# Or use a .env file with dotenv
</StepCode>