rivetkit-client-javascript by rivet-dev/skills
npx skills add https://github.com/rivet-dev/skills --skill rivetkit-client-javascript在构建连接 Rivet Actors 的 JavaScript 客户端(浏览器、Node.js 或 Bun)时使用此技能,需配合 rivetkit/client。
安装客户端(最新版本:2.1.6)
npm install rivetkit@2.1.6
使用 createClient() 创建客户端并调用执行器操作。
try/catch。catch,请显式处理错误,至少应记录日志。有关入门信息,请参阅 后端快速入门指南。
import { createClient } from "rivetkit/client";
import type { registry } from "./actors";
const client = createClient<typeof registry>({
endpoint: "https://my-namespace:pk_...@api.rivet.dev",
});
const counter = client.counter.getOrCreate(["my-counter"]);
const count = await counter.increment(1);
import { actor, setup } from "rivetkit";
export const counter = actor({
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
import { createClient } from "rivetkit/client";
const client = createClient();
const handle = client.counter.getOrCreate(["my-counter"]);
// 无状态:每次调用都是独立的
await handle.increment(1);
// 有状态:保持连接打开以接收实时事件
const conn = handle.connect();
conn.on("count", (value) => console.log(value));
await conn.increment(1);
import { createClient } from "rivetkit/client";
const client = createClient();
const room = client.chatRoom.getOrCreate(["room-42"]);
const existing = client.chatRoom.get(["room-42"]);
const created = await client.game.create(["game-1"], {
input: { mode: "ranked" },
});
const byId = client.chatRoom.getForId("actor-id");
const resolvedId = await room.resolve();
import { createClient } from "rivetkit/client";
const client = createClient();
const chat = client.chatRoom.getOrCreate(["general"], {
params: { authToken: "jwt-token-here" },
});
const conn = chat.connect();
import { createClient } from "rivetkit/client";
const client = createClient();
const conn = client.chatRoom.getOrCreate(["general"]).connect();
conn.on("message", (msg) => console.log(msg));
conn.once("gameOver", () => console.log("done"));
import { createClient } from "rivetkit/client";
const client = createClient();
const conn = client.chatRoom.getOrCreate(["general"]).connect();
conn.onOpen(() => console.log("connected"));
conn.onClose(() => console.log("disconnected"));
conn.onError((err) => console.error("error:", err));
conn.onStatusChange((status) => console.log("status:", status));
await conn.dispose();
对于实现了 onRequest 或 onWebSocket 的执行器,可以直接调用它们:
import { createClient } from "rivetkit/client";
const client = createClient();
const handle = client.chatRoom.getOrCreate(["general"]);
const response = await handle.fetch("history");
const history = await response.json();
const ws = await handle.webSocket("stream");
ws.addEventListener("message", (event) => {
console.log("message:", event.data);
});
ws.send("hello");
import { Hono } from "hono";
import { createClient } from "rivetkit/client";
const app = new Hono();
const client = createClient();
app.post("/increment/:name", async (c) => {
const counterHandle = client.counter.getOrCreate([c.req.param("name")]);
const newCount = await counterHandle.increment(1);
return c.json({ count: newCount });
});
import { ActorError } from "rivetkit/client";
import { createClient } from "rivetkit/client";
const client = createClient();
try {
await client.user.getOrCreate(["user-123"]).updateUsername("ab");
} catch (error) {
if (error instanceof ActorError) {
console.log(error.code, error.metadata);
}
}
键唯一标识执行器实例。使用复合键(数组)进行分层寻址:
import { createClient } from "rivetkit/client";
import type { registry } from "./actors";
const client = createClient<typeof registry>();
// 复合键:[组织, 房间]
client.chatRoom.getOrCreate(["org-acme", "general"]);
import { actor, setup } from "rivetkit";
export const chatRoom = actor({
state: { messages: [] as string[] },
actions: {
getRoomInfo: (c) => ({ org: c.key[0], room: c.key[1] }),
},
});
export const registry = setup({
use: { chatRoom },
});
当 userId 包含用户数据时,不要使用字符串插值(如 "org:${userId}")来构建键。应使用数组以防止键注入攻击。
createClient() 自动读取:
RIVET_ENDPOINT (端点)RIVET_NAMESPACERIVET_TOKENRIVET_RUNNER如果未设置,在浏览器中默认为 window.location.origin + "/api/rivet",在服务器上默认为 http://127.0.0.1:6420。
端点支持 URL 认证语法:
https://namespace:token@api.rivet.dev
您也可以传递不带认证信息的端点,并单独提供 RIVET_NAMESPACE 和 RIVET_TOKEN。对于无服务器部署,请使用您应用的 /api/rivet URL。详情请参阅 端点。
包: rivetkit
请参阅 RivetKit 客户端概述。
createClient - 创建客户端createEngineDriver - 引擎驱动DriverConfig - 驱动配置Client - 客户端类型如果您需要更多关于 Rivet Actors、注册表或服务器端 RivetKit 的信息,请添加主技能:
npx skills add rivet-dev/skills
然后使用 rivetkit 技能获取后端指导。
每周安装量
2.0K
仓库
GitHub 星标数
8
首次出现
2026年1月26日
安全审计
安装在
github-copilot2.0K
codex1.2K
opencode1.2K
gemini-cli1.2K
amp1.2K
kimi-cli1.2K
Use this skill when building JavaScript clients (browser, Node.js, or Bun) that connect to Rivet Actors with rivetkit/client.
Install the client (latest: 2.1.6)
npm install rivetkit@2.1.6
Create a client with createClient() and call actor actions.
try/catch unless absolutely needed.catch is used, handle the error explicitly, at minimum by logging it.See the backend quickstart guide for getting started.
import { createClient } from "rivetkit/client";
import type { registry } from "./actors";
const client = createClient<typeof registry>({
endpoint: "https://my-namespace:pk_...@api.rivet.dev",
});
const counter = client.counter.getOrCreate(["my-counter"]);
const count = await counter.increment(1);
import { actor, setup } from "rivetkit";
export const counter = actor({
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
import { createClient } from "rivetkit/client";
const client = createClient();
const handle = client.counter.getOrCreate(["my-counter"]);
// Stateless: each call is independent
await handle.increment(1);
// Stateful: keep a connection open for realtime events
const conn = handle.connect();
conn.on("count", (value) => console.log(value));
await conn.increment(1);
import { createClient } from "rivetkit/client";
const client = createClient();
const room = client.chatRoom.getOrCreate(["room-42"]);
const existing = client.chatRoom.get(["room-42"]);
const created = await client.game.create(["game-1"], {
input: { mode: "ranked" },
});
const byId = client.chatRoom.getForId("actor-id");
const resolvedId = await room.resolve();
import { createClient } from "rivetkit/client";
const client = createClient();
const chat = client.chatRoom.getOrCreate(["general"], {
params: { authToken: "jwt-token-here" },
});
const conn = chat.connect();
import { createClient } from "rivetkit/client";
const client = createClient();
const conn = client.chatRoom.getOrCreate(["general"]).connect();
conn.on("message", (msg) => console.log(msg));
conn.once("gameOver", () => console.log("done"));
import { createClient } from "rivetkit/client";
const client = createClient();
const conn = client.chatRoom.getOrCreate(["general"]).connect();
conn.onOpen(() => console.log("connected"));
conn.onClose(() => console.log("disconnected"));
conn.onError((err) => console.error("error:", err));
conn.onStatusChange((status) => console.log("status:", status));
await conn.dispose();
For actors that implement onRequest or onWebSocket, call them directly:
import { createClient } from "rivetkit/client";
const client = createClient();
const handle = client.chatRoom.getOrCreate(["general"]);
const response = await handle.fetch("history");
const history = await response.json();
const ws = await handle.webSocket("stream");
ws.addEventListener("message", (event) => {
console.log("message:", event.data);
});
ws.send("hello");
import { Hono } from "hono";
import { createClient } from "rivetkit/client";
const app = new Hono();
const client = createClient();
app.post("/increment/:name", async (c) => {
const counterHandle = client.counter.getOrCreate([c.req.param("name")]);
const newCount = await counterHandle.increment(1);
return c.json({ count: newCount });
});
import { ActorError } from "rivetkit/client";
import { createClient } from "rivetkit/client";
const client = createClient();
try {
await client.user.getOrCreate(["user-123"]).updateUsername("ab");
} catch (error) {
if (error instanceof ActorError) {
console.log(error.code, error.metadata);
}
}
Keys uniquely identify actor instances. Use compound keys (arrays) for hierarchical addressing:
import { createClient } from "rivetkit/client";
import type { registry } from "./actors";
const client = createClient<typeof registry>();
// Compound key: [org, room]
client.chatRoom.getOrCreate(["org-acme", "general"]);
import { actor, setup } from "rivetkit";
export const chatRoom = actor({
state: { messages: [] as string[] },
actions: {
getRoomInfo: (c) => ({ org: c.key[0], room: c.key[1] }),
},
});
export const registry = setup({
use: { chatRoom },
});
Don't build keys with string interpolation like "org:${userId}" when userId contains user data. Use arrays instead to prevent key injection attacks.
createClient() automatically reads:
RIVET_ENDPOINT (endpoint)RIVET_NAMESPACERIVET_TOKENRIVET_RUNNERDefaults to window.location.origin + "/api/rivet" in the browser or http://127.0.0.1:6420 on the server when unset.
Endpoints support URL auth syntax:
https://namespace:token@api.rivet.dev
You can also pass the endpoint without auth and provide RIVET_NAMESPACE and RIVET_TOKEN separately. For serverless deployments, use your app's /api/rivet URL. See Endpoints for details.
Package: rivetkit
See the RivetKit client overview.
createClient - Create a clientcreateEngineDriver - Engine driverDriverConfig - Driver configurationClient - Client typeIf you need more about Rivet Actors, registries, or server-side RivetKit, add the main skill:
npx skills add rivet-dev/skills
Then use the rivetkit skill for backend guidance.
Weekly Installs
2.0K
Repository
GitHub Stars
8
First Seen
Jan 26, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot2.0K
codex1.2K
opencode1.2K
gemini-cli1.2K
amp1.2K
kimi-cli1.2K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装