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

Commit 69062e3

Browse files
committed
chore: add rivet as a default driver
1 parent 06f601c commit 69062e3

File tree

132 files changed

+2692
-4310
lines changed

Some content is hidden

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

132 files changed

+2692
-4310
lines changed

docs/workers/quickstart.mdx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ export const registry = setup({
4242
import { registry } from "./registry";
4343
import { Hono } from "hono";
4444
import { serve } from "@hono/node-server";
45+
import { createNodeWebSocket } from '@hono/node-ws'
46+
47+
// Setup server
48+
const app = new Hono();
4549

4650
// Start RivetKit
4751
//
4852
// State is stored in memory, this can be configured later
49-
const { client, hono } = registry.run();
50-
51-
// Setup server
52-
const app = new Hono();
53+
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app }) // TODO: do this before app
54+
const { client, hono } = registry.run({
55+
getUpgradeWebSocket: () => upgradeWebSocket,
56+
});
5357

5458
// Expose RivetKit to the frontend (optional)
5559
app.route("/registry", hono);
@@ -67,6 +71,7 @@ app.post("/increment/:name", async (c) => {
6771
serve({ fetch: app.fetch, port: 8080 }, (x) =>
6872
console.log("Listening at http://localhost:8080"),
6973
);
74+
injectWebSocket(server)
7075
```
7176

7277
```ts Express.js
@@ -77,6 +82,37 @@ TODO
7782
TODO
7883
```
7984

85+
```ts Hono
86+
import { registry } from "./registry";
87+
import { Hono } from "hono";
88+
89+
// Start RivetKit
90+
//
91+
// State is stored in memory, this can be configured later
92+
const { client, serve } = registry.server();
93+
94+
// Setup server
95+
const app = new Hono();
96+
97+
// Example endpoint
98+
app.post("/increment/:name", async (c) => {
99+
const name = c.req.param("name");
100+
101+
// Communicate with actor
102+
const counter = client.counter.getOrCreate(name);
103+
const newCount = await counter.increment(1);
104+
105+
return c.text(`New Count: ${newCount}`);
106+
});
107+
108+
// Start server
109+
serve(app);
110+
```
111+
112+
TODO: How to serve without registry helper
113+
114+
TODO: Why we need to use our own custom serve fn
115+
80116
</CodeGroup>
81117

82118
<Info>

examples/better-auth/package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@
2525
},
2626
"dependencies": {
2727
"@hono/node-server": "^1.14.4",
28-
"@rivetkit/memory": "workspace:0.9.0-rc.1",
2928
"@rivetkit/react": "workspace:0.9.0-rc.1",
3029
"better-auth": "^1.0.1",
3130
"hono": "^4.7.0",
3231
"react": "^18.2.0",
3332
"react-dom": "^18.2.0"
3433
},
35-
"example": {
36-
"platforms": [
37-
"*"
38-
]
39-
},
4034
"stableVersion": "0.8.0"
4135
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { betterAuth } from "better-auth";
2-
import { sqliteAdapter } from "@better-auth/sqlite";
3-
import Database from "better-sqlite3";
4-
5-
const db = new Database("./auth.db");
6-
7-
export const auth = betterAuth({
8-
database: sqliteAdapter(db),
9-
emailAndPassword: {
10-
enabled: true,
11-
},
12-
session: {
13-
expiresIn: 60 * 60 * 24 * 7, // 7 days
14-
updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated)
15-
},
16-
plugins: [],
17-
});
18-
19-
export type Session = typeof auth.$Infer.Session;
20-
export type User = typeof auth.$Infer.User;
1+
// import { betterAuth } from "better-auth";
2+
// import { sqliteAdapter } from "@better-auth/sqlite";
3+
// import Database from "better-sqlite3";
4+
//
5+
// const db = new Database("./auth.db");
6+
//
7+
// export const auth = betterAuth({
8+
// database: sqliteAdapter(db),
9+
// emailAndPassword: {
10+
// enabled: true,
11+
// },
12+
// session: {
13+
// expiresIn: 60 * 60 * 24 * 7, // 7 days
14+
// updateAge: 60 * 60 * 24, // 1 day (every day the session expiry is updated)
15+
// },
16+
// plugins: [],
17+
// });
18+
//
19+
// export type Session = typeof auth.$Infer.Session;
20+
// export type User = typeof auth.$Infer.User;
Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
import { worker, setup } from "@rivetkit/worker";
2-
import { auth, type Session, type User } from "./auth";
3-
4-
export const chatRoom = worker({
5-
onAuth: async (c) => {
6-
const authResult = await auth.api.getSession({
7-
headers: c.req.headers,
8-
});
9-
10-
if (!authResult?.session || !authResult?.user) {
11-
throw new Error("Unauthorized");
12-
}
13-
14-
return {
15-
userId: authResult.user.id,
16-
user: authResult.user,
17-
session: authResult.session,
18-
};
19-
},
20-
state: {
21-
messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }>
22-
},
23-
actions: {
24-
sendMessage: (c, message: string) => {
25-
const newMessage = {
26-
id: crypto.randomUUID(),
27-
userId: c.auth.userId,
28-
username: c.auth.user.email,
29-
message,
30-
timestamp: Date.now(),
31-
};
32-
33-
c.state.messages.push(newMessage);
34-
c.broadcast("newMessage", newMessage);
35-
36-
return newMessage;
37-
},
38-
getMessages: (c) => {
39-
return c.state.messages;
40-
},
41-
},
42-
});
43-
44-
export const registry = setup({
45-
workers: { chatRoom },
46-
});
47-
48-
export type Registry = typeof registry;
1+
// import { worker, setup } from "@rivetkit/worker";
2+
// import { auth, type Session, type User } from "./auth";
3+
//
4+
// export const chatRoom = worker({
5+
// onAuth: async (c) => {
6+
// const authResult = await auth.api.getSession({
7+
// headers: c.req.headers,
8+
// });
9+
//
10+
// if (!authResult?.session || !authResult?.user) {
11+
// throw new Error("Unauthorized");
12+
// }
13+
//
14+
// return {
15+
// userId: authResult.user.id,
16+
// user: authResult.user,
17+
// session: authResult.session,
18+
// };
19+
// },
20+
// state: {
21+
// messages: [] as Array<{ id: string; userId: string; username: string; message: string; timestamp: number }>
22+
// },
23+
// actions: {
24+
// sendMessage: (c, message: string) => {
25+
// const newMessage = {
26+
// id: crypto.randomUUID(),
27+
// userId: c.auth.userId,
28+
// username: c.auth.user.email,
29+
// message,
30+
// timestamp: Date.now(),
31+
// };
32+
//
33+
// c.state.messages.push(newMessage);
34+
// c.broadcast("newMessage", newMessage);
35+
//
36+
// return newMessage;
37+
// },
38+
// getMessages: (c) => {
39+
// return c.state.messages;
40+
// },
41+
// },
42+
// });
43+
//
44+
// export const registry = setup({
45+
// workers: { chatRoom },
46+
// });
47+
//
48+
// export type Registry = typeof registry;
Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
1-
import { registry } from "./registry";
2-
import { auth } from "./auth";
3-
import { Hono } from "hono";
4-
import { serve } from "@hono/node-server";
5-
import { createMemoryDriver } from "@rivetkit/memory";
6-
7-
// Setup router
8-
const app = new Hono();
9-
10-
// Start RivetKit
11-
const { client, hono } = registry.run({
12-
driver: createMemoryDriver(),
13-
cors: {
14-
// IMPORTANT: Configure origins in production
15-
origin: "*",
16-
},
17-
});
18-
19-
// Mount Better Auth routes
20-
app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw));
21-
22-
// Expose RivetKit to the frontend
23-
app.route("/registry", hono);
24-
25-
// Example HTTP endpoint to join chat room
26-
app.post("/api/join-room/:roomId", async (c) => {
27-
const roomId = c.req.param("roomId");
28-
29-
// Verify authentication
30-
const authResult = await auth.api.getSession({
31-
headers: c.req.header(),
32-
});
33-
34-
if (!authResult?.session || !authResult?.user) {
35-
return c.json({ error: "Unauthorized" }, 401);
36-
}
37-
38-
try {
39-
const room = client.chatRoom.getOrCreate(roomId);
40-
const messages = await room.getMessages();
41-
42-
return c.json({
43-
success: true,
44-
roomId,
45-
messages,
46-
user: authResult.user
47-
});
48-
} catch (error) {
49-
return c.json({ error: "Failed to join room" }, 500);
50-
}
51-
});
52-
53-
serve({ fetch: app.fetch, port: 6420 }, () =>
54-
console.log("Listening at http://localhost:6420"),
55-
);
1+
// import { registry } from "./registry";
2+
// import { auth } from "./auth";
3+
// import { Hono } from "hono";
4+
// import { serve } from "@hono/node-server";
5+
//
6+
// // Setup router
7+
// const app = new Hono();
8+
//
9+
// // Start RivetKit
10+
// const { client, hono } = registry.run({
11+
// driver: createMemoryDriver(),
12+
// cors: {
13+
// // IMPORTANT: Configure origins in production
14+
// origin: "*",
15+
// },
16+
// });
17+
//
18+
// // Mount Better Auth routes
19+
// app.on(["GET", "POST"], "/api/auth/**", (c) => auth.handler(c.req.raw));
20+
//
21+
// // Expose RivetKit to the frontend
22+
// app.route("/registry", hono);
23+
//
24+
// // Example HTTP endpoint to join chat room
25+
// app.post("/api/join-room/:roomId", async (c) => {
26+
// const roomId = c.req.param("roomId");
27+
//
28+
// // Verify authentication
29+
// const authResult = await auth.api.getSession({
30+
// headers: c.req.header(),
31+
// });
32+
//
33+
// if (!authResult?.session || !authResult?.user) {
34+
// return c.json({ error: "Unauthorized" }, 401);
35+
// }
36+
//
37+
// try {
38+
// const room = client.chatRoom.getOrCreate(roomId);
39+
// const messages = await room.getMessages();
40+
//
41+
// return c.json({
42+
// success: true,
43+
// roomId,
44+
// messages,
45+
// user: authResult.user
46+
// });
47+
// } catch (error) {
48+
// return c.json({ error: "Failed to join room" }, 500);
49+
// }
50+
// });
51+
//
52+
// serve({ fetch: app.fetch, port: 6420 }, () =>
53+
// console.log("Listening at http://localhost:6420"),
54+
// );

0 commit comments

Comments
 (0)