rust-server

Write SpacetimeDB server modules in Rust with tables, reducers, views, and procedures.

25.1k|1.1k|Updated Jun 17, 2023
One-click install
npx skills add https://github.com/clockworklabs/SpacetimeDB --skill rust-server
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-server
Source: https://github.com/clockworklabs/SpacetimeDB/tree/main/codex-plugin/plugins/spacetimedb/skills/rust-server
Command: npx skills add https://github.com/clockworklabs/SpacetimeDB --skill rust-server

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing SpacetimeDB server modules in Rust requires precise knowledge of the SDK's macros, table attributes, reducer signatures, and context APIs, and small mistakes like missing the Table import or wrong accessor casing cause compile errors.

Core Features & Use Cases

  • Table and Schema Definitions: Declare tables with primary keys, auto-increment columns, unique constraints, btree indexes, and migration-safe defaults.
  • Reducers and Lifecycle Hooks: Implement reducers, init/client_connected/client_disconnected hooks, scheduled tables, and deterministic RNG and timestamps via ReducerContext.
  • Views, Procedures, and HTTP: Build anonymous and per-user views, query-builder views with semijoins, procedures with outbound HTTP, and inbound HTTP routers.
  • Use Case: A developer building a multiplayer game backend can define entity tables, connection lifecycle reducers, and scheduled tick timers entirely in Rust using the patterns in this reference.

Quick Start

Write a SpacetimeDB Rust module with a public entity table keyed by Identity and reducers that create entities and add timestamped records for the connected user.

Frequently Asked Questions about rust-server

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define a table in a SpacetimeDB Rust module?

Apply #[spacetimedb::table(accessor = name, public)] to a pub struct with typed fields. Use #[primary_key], #[auto_inc], #[unique], and #[index(btree)] on columns, and access the table through ctx.db using the snake_case accessor name.

How do I write a reducer in SpacetimeDB Rust?

Annotate a function taking &ReducerContext with #[spacetimedb::reducer]. Reducers can return Result<(), String> for validation failures, and you should use try_insert() with ? instead of insert() when returning Result to avoid panics on constraint violations.

Why does ctx.db table insert fail to compile in SpacetimeDB?

The Table trait must be imported from the spacetimedb crate. Without use spacetimedb::Table, methods like insert(), iter(), and find() on table handles produce a 'no method named insert found' compile error.

Does SpacetimeDB Rust support scheduled or recurring tasks?

Yes, declare a table with scheduled(reducer_fn) containing scheduled_id and scheduled_at columns. Insert rows with ScheduleAt::Time for one-time execution or ScheduleAt::Interval for repeating execution, and the row is auto-deleted after the reducer runs.

Can SpacetimeDB Rust modules make outbound HTTP requests?

Yes, procedures using &mut ProcedureContext can call ctx.http with convenience methods like get or build custom requests via Request::builder and ctx.http.send. Responses are not cloneable, so inspect status and headers before consuming the body.