Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit a230b3a

Browse files
committed
chore: implement Registry.run
1 parent 3ff7699 commit a230b3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1159
-616
lines changed

examples/hono-react/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "example-hono-react",
3+
"version": "0.9.0-rc.1",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "concurrently \"npm run dev:backend\" \"npm run dev:frontend\"",
8+
"dev:backend": "tsx --watch src/backend/server.ts",
9+
"dev:frontend": "vite",
10+
"build": "vite build",
11+
"check-types": "tsc --noEmit",
12+
"test": "vitest run"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^22.13.9",
16+
"@types/react": "^18.2.0",
17+
"@types/react-dom": "^18.2.0",
18+
"@vitejs/plugin-react": "^4.2.0",
19+
"concurrently": "^8.2.2",
20+
"rivetkit": "workspace:*",
21+
"tsx": "^3.12.7",
22+
"typescript": "^5.5.2",
23+
"vite": "^5.0.0",
24+
"vitest": "^3.1.1"
25+
},
26+
"dependencies": {
27+
"@hono/node-server": "^1.14.4",
28+
"@rivetkit/memory": "workspace:0.9.0-rc.1",
29+
"@rivetkit/react": "workspace:0.9.0-rc.1",
30+
"hono": "^4.7.0",
31+
"react": "^18.2.0",
32+
"react-dom": "^18.2.0"
33+
},
34+
"example": {
35+
"platforms": [
36+
"*"
37+
]
38+
},
39+
"stableVersion": "0.8.0"
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { worker, setup } from "rivetkit";
2+
3+
export const counter = worker({
4+
onAuth: () => {
5+
// Configure auth here
6+
},
7+
state: { count: 0 },
8+
actions: {
9+
increment: (c, x: number) => {
10+
c.state.count += x;
11+
return c.state.count;
12+
},
13+
},
14+
});
15+
16+
export const registry = setup({
17+
workers: { counter },
18+
});
19+
20+
export type Registry = typeof registry;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { registry } from "./registry";
2+
import { Hono } from "hono";
3+
import { serve } from "@hono/node-server";
4+
import { createMemoryDriver } from "@rivetkit/memory";
5+
6+
// Start RivetKit
7+
const { client, handler } = registry.run({
8+
driver: createMemoryDriver(),
9+
});
10+
11+
// Setup router
12+
const app = new Hono();
13+
14+
// Expose RivetKit to the frontend
15+
app.mount("/registry", handler);
16+
17+
// Example HTTP endpoint
18+
app.post("/increment/:name", async (c) => {
19+
const name = c.req.param("name");
20+
21+
const counter = client.counter.getOrCreate(name);
22+
const newCount = await counter.increment(1);
23+
24+
return c.text(`New Count: ${newCount}`);
25+
});
26+
27+
serve({ fetch: app.fetch, port: 3001 }, (x) =>
28+
console.log("Listening at http://localhost:3001"),
29+
);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useState } from "react";
2+
import { createClient, createRivetKit } from "@rivetkit/react";
3+
import type { Registry } from "../backend/registry";
4+
5+
const client = createClient<Registry>(`${location.origin}/registry`);
6+
const { useWorker } = createRivetKit(client);
7+
8+
function App() {
9+
const [count, setCount] = useState(0);
10+
const [counterName, setCounterName] = useState("test-counter");
11+
12+
const counter = useWorker({
13+
name: "counter",
14+
key: [counterName],
15+
});
16+
17+
const increment = async () => {
18+
console.log(
19+
"incr",
20+
counter.isError,
21+
counter.isConnecting,
22+
counter.handle,
23+
counter.connection,
24+
);
25+
if (counter.connection) {
26+
const newCount = await counter.connection.increment(1);
27+
console.log("count", newCount);
28+
setCount(newCount);
29+
}
30+
31+
// const newCount = await client.counter.getOrCreate().increment(1);
32+
// console.log("count", newCount);
33+
// setCount(newCount);
34+
};
35+
36+
return (
37+
<div>
38+
<h1>Counter: {count}</h1>
39+
<input
40+
type="text"
41+
value={counterName}
42+
onChange={(e) => setCounterName(e.target.value)}
43+
placeholder="Counter name"
44+
/>
45+
<button onClick={increment}>Increment</button>
46+
</div>
47+
);
48+
}
49+
50+
export default App;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Hono React Counter</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/main.tsx"></script>
11+
</body>
12+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import App from "./App";
4+
5+
ReactDOM.createRoot(document.getElementById("root")!).render(
6+
<React.StrictMode>
7+
<App />
8+
</React.StrictMode>,
9+
);

examples/hono-react/tsconfig.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6+
"target": "esnext",
7+
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
8+
"lib": ["esnext", "dom"],
9+
/* Specify what JSX code is generated. */
10+
"jsx": "react-jsx",
11+
12+
/* Specify what module code is generated. */
13+
"module": "esnext",
14+
/* Specify how TypeScript looks up a file from a given module specifier. */
15+
"moduleResolution": "bundler",
16+
/* Specify type package names to be included without being referenced in a source file. */
17+
"types": ["node"],
18+
/* Enable importing .json files */
19+
"resolveJsonModule": true,
20+
21+
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
22+
"allowJs": true,
23+
/* Enable error reporting in type-checked JavaScript files. */
24+
"checkJs": false,
25+
26+
/* Disable emitting files from a compilation. */
27+
"noEmit": true,
28+
29+
/* Ensure that each file can be safely transpiled without relying on other imports. */
30+
"isolatedModules": true,
31+
/* Allow 'import x from y' when a module doesn't have a default export. */
32+
"allowSyntheticDefaultImports": true,
33+
/* Ensure that casing is correct in imports. */
34+
"forceConsistentCasingInFileNames": true,
35+
36+
/* Enable all strict type-checking options. */
37+
"strict": true,
38+
39+
/* Skip type checking all .d.ts files. */
40+
"skipLibCheck": true
41+
},
42+
"include": ["src/**/*"]
43+
}

examples/hono-react/vite.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from "vite";
2+
import react from "@vitejs/plugin-react";
3+
import devServer from "@hono/vite-dev-server";
4+
5+
export default defineConfig({
6+
plugins: [react()],
7+
root: "src/frontend",
8+
build: {
9+
outDir: "../../dist",
10+
},
11+
server: {
12+
host: "0.0.0.0",
13+
proxy: {
14+
"/registry": "http://localhost:3001",
15+
"/increment": "http://localhost:3001",
16+
},
17+
},
18+
});

examples/hono/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "example-hono",
3+
"version": "0.9.0-rc.1",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "tsx --watch src/server.ts",
8+
"check-types": "tsc --noEmit"
9+
},
10+
"devDependencies": {
11+
"@types/node": "^22.13.9",
12+
"rivetkit": "workspace:*",
13+
"tsx": "^3.12.7",
14+
"typescript": "^5.5.2"
15+
},
16+
"dependencies": {
17+
"@hono/node-server": "^1.14.4",
18+
"@rivetkit/memory": "workspace:0.9.0-rc.1",
19+
"@rivetkit/react": "workspace:0.9.0-rc.1",
20+
"hono": "^4.7.0",
21+
"react": "^18.2.0",
22+
"react-dom": "^18.2.0"
23+
},
24+
"example": {
25+
"platforms": ["*"]
26+
},
27+
"stableVersion": "0.8.0"
28+
}

examples/hono/src/registry.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { worker, setup } from "rivetkit";
2+
3+
export const counter = worker({
4+
onAuth: () => {
5+
// Configure auth here
6+
},
7+
state: { count: 0 },
8+
actions: {
9+
increment: (c, x: number) => {
10+
c.state.count += x;
11+
return c.state.count;
12+
},
13+
},
14+
});
15+
16+
export const registry = setup({
17+
workers: { counter },
18+
});
19+
20+
export type Registry = typeof registry;

0 commit comments

Comments
 (0)