rivetkit
RivetKitバックエンドとRivet Actorランタイムの構築および変更ガイド。
npx skills add rivet-dev/skills --skill rivetkitBefore / After 効果比較
1 组RivetKitバックエンドとActorランタイムの構築および変更には、そのアーキテクチャに対する深い理解が必要であり、学習曲線が急です。開発者はしばしば複雑な設定とデバッグの課題に直面します。
このスキルは、RivetKitバックエンドとActorランタイムの開発ガイドを提供します。構築、変更、デバッグのプロセスを簡素化し、開発者がRivetKitを効率的に活用してAIアプリケーションを構築できるよう支援します。
Rivet Actors
Build high-performance, long-lived, in-memory processes on Rivet's actor runtime. Use this skill whenever HTTP endpoints, databases, or queues no longer meet the requirements for AI agents, realtime collaboration, workflow orchestration, or geo-distributed data.
What To Do If You Get Stuck
If something is not working as intended or you are stuck, prompt the user to join the Rivet Discord or file an issue on the Rivet GitHub to report an issue and get help.
Provide the user with a pre-generated report with:
- Symptoms
- If this is happening in local dev, deployed, or both
- The error you're seeing
- Relevant source code related to this
- What you've tried to solve it
- Environment
- RivetKit version
- Runtime (Node, Bun, etc) including version
- If applicable, provider in use (e.g. Vercel, Railway, etc)
- If applicable, HTTP router in use (e.g. Hono, Express, Elysia)
Debugging Actors
Use the inspector HTTP API to examine running actors. These endpoints are accessible through the gateway at /gateway/{actor_id}/inspector/*. Key endpoints:
GET /inspector/summary- full actor snapshot (state, connections, RPCs, queue)GET /inspector/state/PATCH /inspector/state- read/write actor stateGET /inspector/connections- active connectionsGET /inspector/rpcs- available actionsPOST /inspector/action/{name}- execute an action with{"args": [...]}GET /inspector/queue?limit=50- queue statusGET /inspector/traces?startMs=0&endMs=...&limit=1000- trace spans (OTLP JSON)GET /inspector/workflow-history- workflow history and status
In local dev, no auth token is needed. In production, pass Authorization: Bearer <RIVET_INSPECTOR_TOKEN>. See the debugging docs for details.
Citing Sources
When providing information from Rivet documentation, cite the canonical URL so users can learn more. Each reference file includes its canonical URL in the header metadata.
How to cite:
- Use inline links for key concepts: "Use actor keys to uniquely identify instances."
- Add a "Learn more" link after explanations for complex topics
Finding canonical URLs:
The Reference Map below links to reference files. Each file's header contains:
> Canonical URL: https://rivet.dev/docs/actors/actions
Use that canonical URL when citing, not the reference file path.
Examples:
- Actions →
https://rivet.dev/docs/actors/actions - React client →
https://rivet.dev/docs/clients/react - Self-hosting on Kubernetes →
https://rivet.dev/docs/self-hosting/kubernetes
First Steps
- Install RivetKit (latest: 2.1.6)
npm install rivetkit@2.1.6 - Define a registry with
setup({ use: { /* actors */ } }). - Expose
registry.serve()orregistry.handler()(serverless) orregistry.startRunner()(runner mode). Prefer serverless mode unless the user has a specific reason to use runner mode. - Verify
/api/rivet/metadatareturns 200 before deploying. - Configure Rivet Cloud or self-hosted engine
- You must configure versioning for production builds. This is not needed for local development. See Versions & Upgrades.
- Integrate clients (see client guides below for JavaScript, React, or Swift)
- Prompt the user if they want to deploy. If so, go to Deploying Rivet Backends.
For more information, read the quickstart guide relevant to the user's project.
Error Handling Policy
- Prefer fail-fast behavior by default.
- Avoid
try/catchunless it is required for a real recovery path, cleanup boundary, or to add actionable context. - Never swallow errors. If you add a
catch, you must handle the error explicitly, at minimum by logging it. - When you cannot recover, log context and rethrow.
State vs Vars: Persistence Rules
c.vars is ephemeral. Data in c.vars is lost on every restart, crash, upgrade, or sleep/wake cycle. Only use c.vars for non-serializable objects (e.g., physics engines, WebSocket references, event emitters, caches) or truly transient runtime data (e.g., current input direction that doesn't matter after disconnect).
Persistent storage options. Any data that must survive restarts belongs in one of these, NOT in c.vars:
c.state— CBOR-serializable data for small, bounded datasets. Ideal for configuration, counters, small player lists, phase flags, etc. Keep under 128 KB. Do not store unbounded or growing data here (e.g., chat logs, event histories, spawned entity lists that grow without limit). State is read/written as a single blob on every persistence cycle.c.kv— Key-value store for unbounded data. This is whatc.stateuses under the hood. Supports binary values. Use for larger or variable-size data like user inventories, world chunks, file blobs, or any collection that may grow over time. Keys are scoped to the actor instance.c.db— SQLite database for structured or complex data. Use when you need queries, indexes, joins, aggregations, or relational modeling. Ideal for leaderboards, match histories, player pools, or any data that benefits from SQL.
Common mistake: Storing meaningful game/application data in c.vars instead of persisting it. For example, if users can spawn objects in a physics simulation, the spawn definitions (position, size, type) must be persisted in c.state (or c.kv if unbounded), even though the physics engine handles (non-serializable) live in c.vars. On restart, run() should recreate the runtime objects from the persisted data.
Deploying Rivet Backends
Assume the user is deploying to Rivet Cloud, unless otherwise specified. If user is self-hosting, read the self-hosting guides below.
- Verify that Rivet Actors are working in local dev
- Prompt the user to choose a provider to deploy to (see Connect for a list of providers, such as Vercel, Railway, etc)
- Follow the deploy guide for that given provider. You will need to instruct the user when you need manual intervention.
API Reference
The RivetKit OpenAPI specification is available in the skill directory at openapi.json. This file documents all HTTP endpoints for managing actors.
Misc Notes
- The Rivet domain is rivet.dev, not rivet.gg
TypeScript Caveat: Actor Client Inference
- In multi-file TypeScript projects, bidirectional actor calls can create a circular type dependency when both actors use
c.client<typeof registry>(). - Symptoms usually include
c.statebecomingunknown, actor methods becoming possiblyundefined, orTS2322/TS2722errors after the first cross-actor call. - If an action returns the result of another actor call, prefer an explicit return type annotation on that action instead of relying on inference through
c.client<typeof registry>(). - If explicit return types are not enough, use a narrower client or registry type for only the actors that action needs.
- As a last resort, pass
unknownfor the registry type and be explicit that this gives up type safety at that call site.
Features
- Long-Lived, Stateful Compute: Each unit of compute is like a tiny server that remembers things between requests – no need to re-fetch data from a database or worry about timeouts. Like AWS Lambda, but with memory and no timeouts.
- Blazing-Fast Reads & Writes: State is stored on the same machine as your compute, so reads and writes are ultra-fast. No database round trips, no latency spikes. State is persisted to Rivet for long term storage, so it survives server restarts.
- Realtime: Update state and broadcast changes in realtime with WebSockets. No external pub/sub systems, no polling – just built-in low-latency events.
- Infinitely Scalable: Automatically scale from zero to millions of concurrent actors. Pay only for what you use with instant scaling and no cold starts.
- Fault Tolerant: Built-in error handling and recovery. Actors automatically restart on failure while preserving state integrity and continuing operations.
When to Use Rivet Actors
- AI agents & sandboxes: multi-step toolchains, conversation memory, sandbox orchestration.
- Multiplayer or collaborative apps: CRDT docs, shared cursors, realtime dashboards, chat.
- Workflow automation: background jobs, cron, rate limiters, durable queues, backpressure control.
- Data-intensive backends: geo-distributed or per-tenant databases, in-memory caches, sharded SQL.
- Networking workloads: WebSocket servers, custom protocols, local-first sync, edge fanout.
Minimal Project
Backend
actors.ts
import { actor, event, setup } from "rivetkit";
const counter = actor({
state: { count: 0 },
events: {
count: event<number>(),
},
actions: {
increment: (c, amount: number) => {
c.state.count += amount;
c.broadcast("count", c.state.count);
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
server.ts
Integrate with the user's existing server if applicable. Otherwise, default to Hono.
No Framework
import { registry } from "./actors";
export default registry.serve();
Hono
import { Hono } from "hono";
import { registry } from "./actors";
const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));
export default app;
Elysia
import { Elysia } from "elysia";
import { registry } from "./actors";
const app = new Elysia()
.all("/api/rivet/*", (c) => registry.handler(c.request));
export default app;
Client Docs
Use the client SDK that matches your app:
Actor Quick Reference
In-Memory State
Persistent data that survives restarts, crashes, and deployments. State is persisted on Rivet Cloud or Rivet self-hosted, so it survives restarts if the current process crashes or exits.
Static Initial State
import { actor } from "rivetkit";
const counter = actor({
state: { count: 0 },
actions: {
increment: (c) => c.state.count += 1,
},
});
Dynamic Initial State
import { actor } from "rivetkit";
interface CounterState {
count: number;
}
const counter = actor({
state: { count: 0 } as CounterState,
createState: (c, input: { start?: number }): CounterState => ({
count: input.start ?? 0,
}),
actions: {
increment: (c) => c.state.count += 1,
},
});
Keys
Keys uniquely identify actor instances. Use compound keys (arrays) for hierarchical addressing:
import { actor, setup } from "rivetkit";
import { createClient } from "rivetkit/client";
const chatRoom = actor({
state: { messages: [] as string[] },
actions: {
getRoomInfo: (c) => ({ org: c.key[0], room: c.key[1] }),
},
});
const registry = setup({ use: { chatRoom } });
const client = createClient<typeof registry>();
// Compound key: [org, room]
client.chatRoom.getOrCreate(["org-acme", "general"]);
// Access key inside actor via c.key
Don't build keys with string interpolation like "org:${userId}" when userId contains user data. Use arrays instead to prevent key injection attacks.
Input
Pass initialization data when creating actors.
import { actor, setup } from "rivetkit";
import { createClient } from "rivetkit/client";
const game = actor({
createState: (c, input: { mode: string }) => ({ mode: input.mode }),
actions: {},
});
const registry = setup({ use: { game } });
const client = createClient<typeof registry>();
// Client usage
const gameHandle = client.game.getOrCreate(["game-1"], {
createWithInput: { mode: "ranked" }
});
Temporary Variables
Temporary data that doesn't survive restarts. Use for non-serializable objects (event emitters, connections, etc).
Static Initial Vars
import { actor } from "rivetkit";
const counter = actor({
state: { count: 0 },
vars: { lastAccess: 0 },
actions: {
increment: (c) => {
c.vars.lastAccess = Date.now();
return c.state.count += 1;
},
},
});
Dynamic Initial Vars
import { actor } from "rivetkit";
const counter = actor({
state: { count: 0 },
createVars: () => ({
emitter: new EventTarget(),
}),
actions: {
increment: (c) => {
c.vars.emitter.dispatchEvent(new Event("change"));
return c.state.count += 1;
},
},
});
Actions
Actions are the primary way clients and other actors communicate with an actor.
import { actor } from "rivetkit";
const counter = actor({
state: { count: 0 },
actions: {
increment: (c, amount: number) => (c.state.count += amount),
getCount: (c) => c.state.count,
},
});
Events & Broadcasts
Events enable real-time communication from actors to connected clients.
import { actor, event } from "rivetkit";
const chatRoom = actor({
state: { messages: [] as string[] },
events: {
newMessage: event<{ text: string }>(),
},
actions: {
sendMessage: (c, text: string) => {
// Broadcast to ALL connected clients
c.broadcast("newMessage", { text });
},
},
});
Connections
Access the current connection via c.conn or all connected clients via c.conns. Use c.conn.id or c.conn.state to securely identify who is calling an action. Connection state is initialized via connState or createConnState, which receives parameters passed by the client on connect.
Static Connection Initial State
import { actor } from "rivetkit";
const chatRoom = actor({
state: {},
connState: { visitorId: 0 },
onConnect: (c, conn) => {
conn.state.visitorId = Math.random();
},
actions: {
whoAmI: (c) => c.conn.state.visitorId,
},
});
Dynamic Connection Initial State
import { actor } from "rivetkit";
const chatRoom = actor({
state: {},
// params passed from client
createConnState: (c, params: { userId: string }) => ({
userId: params.userId,
}),
actions: {
// Access current connection's state and params
whoAmI: (c) => ({
state: c.conn.state,
params: c.conn.params,
}),
// Iterate all connections with c.conns
notifyOthers: (c, text: string) => {
for (const conn of c.conns.values()) {
if (conn !== c.conn) conn.send("notification", { text });
}
},
},
});
Queues
Use queues to process durable messages in order inside a run loop.
imp
...
ユーザーレビュー (0)
レビューを書く
レビューなし
統計データ
ユーザー評価
この Skill を評価