Skip to main content
Version: 2.0.0

Reducer Context

Every reducer receives a special context parameter as its first argument. This context provides read-write access to the database, information about the caller, and additional utilities like random number generation.

The reducer context is required for accessing tables, executing database operations, and retrieving metadata about the current reducer invocation.

Accessing the Database

The primary purpose of the reducer context is to provide access to the module's database tables.

import { schema, table, t } from 'spacetimedb/server';

const user = table(
  { name: 'user', public: true },
  {
    id: t.u64().primaryKey().autoInc(),
    name: t.string(),
  }
);

const spacetimedb = schema({ user });
export default spacetimedb;

export const create_user = spacetimedb.reducer({ name: t.string() }, (ctx, { name }) => {
  ctx.db.user.insert({ id: 0n, name });
});

Caller Information

The context provides information about who invoked the reducer and when.

Sender Identity

Every reducer invocation has an associated caller identity.

import { schema, table, t, type Identity } from 'spacetimedb/server';

const player = table(
  { name: 'player', public: true },
  {
    identity: t.identity().primaryKey(),
    name: t.string(),
    score: t.u32(),
  }
);

const spacetimedb = schema({ player });
export default spacetimedb;

export const update_score = spacetimedb.reducer({ newScore: t.u32() }, (ctx, { newScore }) => {
  // Get the caller's identity
  const caller = ctx.sender;
  
  // Find and update their player record
  const existingPlayer = ctx.db.player.identity.find(caller);
  if (existingPlayer) {
    ctx.db.player.identity.update({
      ...existingPlayer,
      score: newScore,
    });
  }
});

Connection ID

The connection ID identifies the specific client connection that invoked the reducer. This is useful for tracking sessions or implementing per-connection state.

note

The connection ID may be None/null/undefined for reducers invoked by the system (such as scheduled reducers or lifecycle reducers) or when called via the CLI without specifying a connection.

Timestamp

The timestamp indicates when the reducer was invoked. This value is consistent throughout the reducer execution and is useful for timestamping events or implementing time-based logic.

Random Number Generation

The context provides access to a random number generator that is deterministic and reproducible. This ensures that reducer execution is consistent across all nodes in a distributed system.

warning

Never use external random number generators (like Random in C# without using the context). These are non-deterministic and will cause different nodes to produce different results, breaking consensus.

Module Identity

The context provides access to the module's own identity, which is useful when a reducer needs to refer to the database itself.

Scheduled reducers and procedures are private by default in SpacetimeDB 2.x, so you do not need to compare the sender against the module identity to prevent ordinary clients from calling them directly. If you need both a scheduled function and a client-callable entry point, keep the scheduled function private and define a separate public reducer that wraps the shared logic.

import { schema, table, t } from 'spacetimedb/server';

const scheduledTask = table(
  { name: 'scheduled_task', scheduled: (): any => send_reminder },
  {
    taskId: t.u64().primaryKey().autoInc(),
    scheduledAt: t.scheduleAt(),
    message: t.string(),
  }
);

const spacetimedb = schema({ scheduledTask });
export default spacetimedb;

export const send_reminder = spacetimedb.reducer({ arg: scheduledTask.rowType }, (_ctx, { arg }) => {
  console.log(`Reminder: ${arg.message}`);
});

Context Properties Reference

PropertyTypeDescription
dbDbViewAccess to the module's database tables
senderIdentityIdentity of the caller
senderAuthAuthCtxAuthorization context for the caller (includes JWT claims and internal call detection)
connectionIdConnectionId | undefinedConnection ID of the caller, if available
timestampTimestampTime when the reducer was invoked
randomRandomRandom number generator (deterministic, seeded by SpacetimeDB)