Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/counter-serverless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Counter (Serverless) for RivetKit

Example project demonstrating serverless actor deployment with automatic engine configuration using [RivetKit](https://rivetkit.org).

[Learn More →](https://github.com/rivet-dev/rivetkit)

[Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivetkit/issues)

## Getting Started

### Prerequisites

- Node.js
- RIVET_TOKEN environment variable (for serverless configuration)

### Installation

```sh
git clone https://github.com/rivet-dev/rivetkit
cd rivetkit/examples/counter-serverless
npm install
```

### Development

Set your Rivet token and run the development server:

```sh
export RIVET_TOKEN=your-token-here
npm run dev
```

Run the connect script to interact with the counter:

```sh
tsx scripts/connect.ts
```

## License

Apache 2.0
20 changes: 20 additions & 0 deletions examples/counter-serverless/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "example-counter-serverless",
"version": "2.0.8",
"private": true,
"type": "module",
"scripts": {
"dev": "tsx src/server.ts",
"check-types": "tsc --noEmit",
"connect": "tsx scripts/connect.ts",
"test": "vitest run"
},
"devDependencies": {
"rivetkit": "workspace:*",
"@types/node": "^22.13.9",
"tsx": "^3.12.7",
"typescript": "^5.7.3",
"vitest": "^3.1.1"
},
"stableVersion": "0.8.0"
}
22 changes: 22 additions & 0 deletions examples/counter-serverless/scripts/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createClient } from "rivetkit/client";
import type { Registry } from "../src/registry";

async function main() {
const client = createClient<Registry>("http://localhost:6420");

const counter = client.counter.getOrCreate().connect();

counter.on("newCount", (count: number) => console.log("Event:", count));

for (let i = 0; i < 5; i++) {
const out = await counter.increment(5);
console.log("RPC:", out);

await new Promise((resolve) => setTimeout(resolve, 1000));
}

await new Promise((resolve) => setTimeout(resolve, 10000));
await counter.dispose();
}

main();
23 changes: 23 additions & 0 deletions examples/counter-serverless/src/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { actor, setup } from "rivetkit";

const counter = actor({
state: {
count: 0,
},
actions: {
increment: (c, x: number) => {
c.state.count += x;
c.broadcast("newCount", c.state.count);
return c.state.count;
},
getCount: (c) => {
return c.state.count;
},
},
});

export const registry = setup({
use: { counter },
});

export type Registry = typeof registry;
7 changes: 7 additions & 0 deletions examples/counter-serverless/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { registry } from "./registry";

registry.start({
runnerKind: "serverless",
runEngine: true,
autoConfigureServerless: true,
});
35 changes: 35 additions & 0 deletions examples/counter-serverless/tests/counter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { setupTest } from "rivetkit/test";
import { expect, test } from "vitest";
import { registry } from "../src/registry";

test("it should count", async (test) => {
const { client } = await setupTest(test, registry);
const counter = client.counter.getOrCreate().connect();

// Test initial count
expect(await counter.getCount()).toBe(0);

// Test event emission
let eventCount = -1;
counter.on("newCount", (count: number) => {
eventCount = count;
});

// Test increment
const incrementAmount = 5;
const result = await counter.increment(incrementAmount);
expect(result).toBe(incrementAmount);

// Verify event was emitted with correct count
expect(eventCount).toBe(incrementAmount);

// Test multiple increments
for (let i = 1; i <= 3; i++) {
const newCount = await counter.increment(incrementAmount);
expect(newCount).toBe(incrementAmount * (i + 1));
expect(eventCount).toBe(incrementAmount * (i + 1));
}

// Verify final count
expect(await counter.getCount()).toBe(incrementAmount * 4);
});
43 changes: 43 additions & 0 deletions examples/counter-serverless/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "esnext",
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
"lib": ["esnext"],
/* Specify what JSX code is generated. */
"jsx": "react-jsx",

/* Specify what module code is generated. */
"module": "esnext",
/* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "bundler",
/* Specify type package names to be included without being referenced in a source file. */
"types": ["node"],
/* Enable importing .json files */
"resolveJsonModule": true,

/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"allowJs": true,
/* Enable error reporting in type-checked JavaScript files. */
"checkJs": false,

/* Disable emitting files from a compilation. */
"noEmit": true,

/* Ensure that each file can be safely transpiled without relying on other imports. */
"isolatedModules": true,
/* Allow 'import x from y' when a module doesn't have a default export. */
"allowSyntheticDefaultImports": true,
/* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true,

/* Enable all strict type-checking options. */
"strict": true,

/* Skip type checking all .d.ts files. */
"skipLibCheck": true
},
"include": ["src/**/*.ts", "scripts/**/*.ts", "tests/**/*.ts"]
}
4 changes: 4 additions & 0 deletions examples/counter-serverless/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"]
}
14 changes: 12 additions & 2 deletions packages/rivetkit/src/manager/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { RivetIdSchema } from "@/manager-api/common";
import type { ServerlessActorDriverBuilder } from "@/mod";
import type { RegistryConfig } from "@/registry/config";
import type { RunnerConfig } from "@/registry/run-config";
import { VERSION } from "@/utils";
import type { ActorOutput, ManagerDriver } from "./driver";
import { actorGateway, createTestWebSocketProxy } from "./gateway";
import { logger } from "./log";
Expand Down Expand Up @@ -159,7 +160,11 @@ function addServerlessRoutes(
});

router.get("/health", (c) => {
return c.text("ok");
return c.json({
status: "ok",
runtime: "rivetkit",
version: VERSION,
});
});
}

Expand Down Expand Up @@ -588,7 +593,12 @@ function addManagerRoutes(
}

router.get("/health", (c) => {
return c.text("ok");
return c.json({
status: "ok",
rivetkit: {
version: packageJson.version,
},
});
});

managerDriver.modifyManagerRouter?.(
Expand Down
Loading
Loading