// server.ts
import { actor, setup } from "@rivetkit/actor";
// Optionally define the events that can be emitted by the actor and their value
const counter = actor<{ event: { newCount: [number] } }>({
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
// Type safety inside actions when trying to broadcast/send incorrectly typed events
c.broadcast("newCount", `${c.state.count}`); // ❌ Argument of type 'string' is not assignable to parameter of type 'number'
c.broadcast("setCount", c.state.count); // ❌ Argument of type '"setCount"' is not assignable to parameter of type '"newCount"'
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
// client.ts
import { createClient } from "@rivetkit/actor/client";
import type { registry } from "./server";
const client = createClient<typeof registry>("http://localhost:8080");
const myCounter = client.counter.getOrCreate(["myCounter"]);
const connection = myCounter.connect();
// Type safety on the client for incorrectly subscribing to or using events
connection.on("newCount", (message) =>
console.log(message.newCount) // ❌ Property 'newCount' does not exist on type 'number'
);
connection.on("resetCount", () => {}); // ❌ Argument of type '"resetCount"' is not assignable to parameter of type '"newCount"'
// server.ts
import { actor, setup } from "@rivetkit/actor";
// Optionally define the events that can be emitted by the actor and their value
const counter = actor<{ event: { newCount: [number] } }>({
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
// Type safety inside actions when trying to broadcast/send incorrectly typed events
c.broadcast("newCount", `${c.state.count}`); // ❌ Argument of type 'string' is not assignable to parameter of type 'number'
c.broadcast("setCount", c.state.count); // ❌ Argument of type '"setCount"' is not assignable to parameter of type '"newCount"'
return c.state.count;
},
},
});
export const registry = setup({
use: { counter },
});
// client.ts
import { createClient } from "@rivetkit/actor/client";
import type { registry } from "./server";
const client = createClient<typeof registry>("http://localhost:8080");
const myCounter = client.counter.getOrCreate(["myCounter"]);
const connection = myCounter.connect();
// Type safety on the client for incorrectly subscribing to or using events
connection.on("newCount", (message) =>
console.log(message.newCount) // ❌ Property 'newCount' does not exist on type 'number'
);
connection.on("resetCount", () => {}); // ❌ Argument of type '"resetCount"' is not assignable to parameter of type '"newCount"'