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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 8 additions & 43 deletions docs/workers/quickstart.mdx → docs/actors/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,16 @@ export const registry = setup({
```ts Hono
import { registry } from "./registry";
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { createNodeWebSocket } from '@hono/node-ws'

// Setup server
const app = new Hono();

// Start RivetKit
//
// State is stored in memory, this can be configured later
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app }) // TODO: do this before app
const { client, hono } = registry.run({
getUpgradeWebSocket: () => upgradeWebSocket,
});
const { client, serve } = registry.server();

// Expose RivetKit to the frontend (optional)
app.route("/registry", hono);
// Setup server
const app = new Hono();

// Example endpoint
app.post("/increment/:name", async (c) => {
const name = c.req.param("name");

Expand All @@ -68,12 +61,11 @@ app.post("/increment/:name", async (c) => {
return c.text(`New Count: ${newCount}`);
});

serve({ fetch: app.fetch, port: 8080 }, (x) =>
console.log("Listening at http://localhost:8080"),
);
injectWebSocket(server)
// Start server
serve(app);
```


```ts Express.js
TODO
```
Expand All @@ -82,33 +74,6 @@ TODO
TODO
```

```ts Hono
import { registry } from "./registry";
import { Hono } from "hono";

// Start RivetKit
//
// State is stored in memory, this can be configured later
const { client, serve } = registry.server();

// Setup server
const app = new Hono();

// Example endpoint
app.post("/increment/:name", async (c) => {
const name = c.req.param("name");

// Communicate with actor
const counter = client.counter.getOrCreate(name);
const newCount = await counter.increment(1);

return c.text(`New Count: ${newCount}`);
});

// Start server
serve(app);
```

TODO: How to serve without registry helper

TODO: Why we need to use our own custom serve fn
Expand Down Expand Up @@ -165,7 +130,7 @@ curl -X POST localhost:8080/increment/foo

<Tab title="Rivet">

```json
```json rivet.json
{
"rivetkit": {
"registry": "src/registry.ts",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions examples/cloudflare-workers-hono/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Cloudflare Workers with Hono for RivetKit

Example project demonstrating Cloudflare Workers deployment with Hono router using [RivetKit](https://rivetkit.org).

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

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

## Getting Started

### Prerequisites

- Node.js
- Cloudflare account with Actors enabled
- Wrangler CLI installed globally (`npm install -g wrangler`)

### Installation

```sh
git clone https://github.com/rivet-gg/rivetkit
cd rivetkit/examples/cloudflare-workers-hono
npm install
```

### Development

```sh
npm run dev
```

This will start the Cloudflare Workers development server locally at http://localhost:8787.

### Testing the Application

You can test the Hono router endpoint by making a POST request to increment a counter:

```sh
curl -X POST http://localhost:8787/increment/my-counter
```

Or run the client script to interact with your actors:

```sh
npm run client
```

### Deploy to Cloudflare

First, authenticate with Cloudflare:

```sh
wrangler login
```

Then deploy:

```sh
npm run deploy
```

## License

Apache 2.0
25 changes: 25 additions & 0 deletions examples/cloudflare-workers-hono/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "example-cloudflare-workers-hono",
"version": "0.9.0-rc.1",
"private": true,
"type": "module",
"scripts": {
"dev": "wrangler dev",
"deploy": "wrangler deploy",
"check-types": "tsc --noEmit",
"client": "tsx scripts/client.ts"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20250129.0",
"@rivetkit/actor": "workspace:*",
"@types/node": "^22.13.9",
"tsx": "^3.12.7",
"typescript": "^5.5.2",
"wrangler": "^3.0.0"
},
"dependencies": {
"@rivetkit/cloudflare-workers": "workspace:*",
"hono": "^4.8.0"
},
"stableVersion": "0.8.0"
}
9 changes: 9 additions & 0 deletions examples/cloudflare-workers-hono/scripts/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async function main() {
const endpoint = process.env.RIVETKIT_ENDPOINT || "http://localhost:8787";
const res = await fetch(`${endpoint}/increment/foo`, {
method: "POST"
});
console.log("Output:", await res.text());
}

main();
22 changes: 22 additions & 0 deletions examples/cloudflare-workers-hono/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createServer } from "@rivetkit/cloudflare-workers";
import { Hono } from "hono";
import { registry } from "./registry";

const { client, createHandler } = createServer(registry);

// Setup router
const app = new Hono();

// Example HTTP endpoint
app.post("/increment/:name", async (c) => {
const name = c.req.param("name");

const counter = client.counter.getOrCreate(name);
const newCount = await counter.increment(1);

return c.text(`New Count: ${newCount}`);
});

const { handler, ActorHandler } = createHandler(app);

export { handler as default, ActorHandler };
19 changes: 19 additions & 0 deletions examples/cloudflare-workers-hono/src/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { actor, setup } from "@rivetkit/actor";

export const counter = actor({
onAuth: () => {
// Configure auth here
},
state: { count: 0 },
actions: {
increment: (c, x: number) => {
c.state.count += x;
return c.state.count;
},
},
});

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

Expand Down Expand Up @@ -40,5 +39,5 @@
/* Skip type checking all .d.ts files. */
"skipLibCheck": true
},
"include": ["**/*.ts"]
"include": ["src/**/*"]
}
30 changes: 30 additions & 0 deletions examples/cloudflare-workers-hono/wrangler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "rivetkit-cloudflare-workers-example",
"main": "src/index.ts",
"compatibility_date": "2025-01-20",
"compatibility_flags": ["nodejs_compat"],
"migrations": [
{
"tag": "v1",
"new_classes": ["ActorHandler"]
}
],
"durable_objects": {
"bindings": [
{
"name": "ACTOR_DO",
"class_name": "ActorHandler"
}
]
},
"kv_namespaces": [
{
"binding": "ACTOR_KV",
"id": "example_namespace",
"preview_id": "example_namespace_preview"
}
],
"observability": {
"enabled": true
}
}
6 changes: 4 additions & 2 deletions examples/cloudflare-workers/scripts/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createClient } from "@rivetkit/actor/client";
import type { Registry } from "../src/registry.js";
import type { registry } from "../src/registry";

// Create RivetKit client
const client = createClient<Registry>("http://localhost:8787");
const client = createClient<typeof registry>(
process.env.RIVETKIT_ENDPOINT ?? "http://localhost:8787",
);

async function main() {
console.log("🚀 Cloudflare Workers Client Demo");
Expand Down
11 changes: 5 additions & 6 deletions examples/cloudflare-workers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// import { createHandler } from "@rivetkit/cloudflare-workers";
// import { registry } from "./registry";
//
// const { handler, ActorHandler } = createHandler(registry);
//
// export { handler as default, ActorHandler };
import { createServerHandler } from "@rivetkit/cloudflare-workers";
import { registry } from "./registry";

const { handler, ActorHandler } = createServerHandler(registry);
export { handler as default, ActorHandler };
1 change: 0 additions & 1 deletion examples/cloudflare-workers/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ export const registry = setup({
use: { counter },
});

export type Registry = typeof registry;
3 changes: 2 additions & 1 deletion examples/hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"dev": "tsx --watch src/server.ts",
"check-types": "tsc --noEmit"
"check-types": "tsc --noEmit",
"client": "tsx scripts/client.ts"
},
"devDependencies": {
"@types/node": "^22.13.9",
Expand Down
9 changes: 9 additions & 0 deletions examples/hono/scripts/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async function main() {
const endpoint = process.env.RIVETKIT_ENDPOINT || "http://localhost:8080";
const res = await fetch(`${endpoint}/increment/foo`, {
method: "POST"
});
console.log("Output:", await res.text());
}

main();
49 changes: 20 additions & 29 deletions examples/hono/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
// import { registry } from "./registry";
// import { Hono } from "hono";
// import { serve } from "@hono/node-server";
// import { createMemoryDriver } from "@rivetkit/memory";
//
// // Start RivetKit
// const { client, hono } = registry.server({
// driver: createMemoryDriver(),
// });
//
// // Setup router
// const app = new Hono();
//
// // Expose RivetKit to the frontend (optinoal)
// app.route("/registry", hono);
//
// // Example HTTP endpoint
// app.post("/increment/:name", async (c) => {
// const name = c.req.param("name");
//
// const counter = client.counter.getOrCreate(name);
// const newCount = await counter.increment(1);
//
// return c.text(`New Count: ${newCount}`);
// });
//
// serve({ fetch: app.fetch, port: 6420 }, (x) =>
// console.log("Listening at http://localhost:6420"),
// );
import { registry } from "./registry";
import { Hono } from "hono";

// Start RivetKit
const { client, serve } = registry.createServer();

// Setup router
const app = new Hono();

// Example HTTP endpoint
app.post("/increment/:name", async (c) => {
const name = c.req.param("name");

const counter = client.counter.getOrCreate(name);
const newCount = await counter.increment(1);

return c.text(`New Count: ${newCount}`);
});

serve(app);
1 change: 0 additions & 1 deletion examples/rivet/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { registry } from "./registry";

console.log("new version");
registry.runServer();
Loading
Loading