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

Commit 6abf62d

Browse files
committed
chore(example-hono-bun): add frontend to example (#1381)
1 parent 2625964 commit 6abf62d

File tree

9 files changed

+141
-8
lines changed

9 files changed

+141
-8
lines changed

examples/hono-bun/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Hono + Bun Integration for RivetKit
22

3-
Example project demonstrating Hono web framework with Bun runtime integration with [RivetKit](https://rivetkit.org).
3+
Example project demonstrating Hono web framework with Bun runtime and React frontend integration with [RivetKit](https://rivetkit.org).
44

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

@@ -26,7 +26,11 @@ npm install
2626
npm run dev
2727
```
2828

29-
Test the server by running:
29+
This will start both the backend server (on port 8080) and the frontend dev server (on port 5173).
30+
31+
Open your browser to [http://localhost:5173](http://localhost:5173) to see the counter application.
32+
33+
You can also test the server directly by running:
3034

3135
```sh
3236
curl -X POST http://localhost:8080/increment/test

examples/hono-bun/package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"dev": "bun --watch src/server.ts",
7+
"dev": "concurrently \"npm run dev:backend\" \"npm run dev:frontend\"",
8+
"dev:backend": "bun --watch src/backend/server.ts",
9+
"dev:frontend": "vite",
10+
"build": "vite build",
811
"check-types": "tsc --noEmit",
912
"connect": "tsx scripts/connect.ts"
1013
},
1114
"devDependencies": {
1215
"@types/bun": "^1.1.15",
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",
1320
"rivetkit": "workspace:*",
14-
"typescript": "^5.5.2"
21+
"tsx": "^3.12.7",
22+
"typescript": "^5.5.2",
23+
"vite": "^5.0.0"
1524
},
1625
"dependencies": {
17-
"hono": "^4.7.0"
26+
"@rivetkit/react": "workspace:*",
27+
"hono": "^4.7.0",
28+
"react": "^18.2.0",
29+
"react-dom": "^18.2.0"
1830
},
1931
"keywords": [
2032
"rivetkit",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const counter = actor({
55
actions: {
66
increment: (c, x: number) => {
77
c.state.count += x;
8+
c.broadcast("newCount", c.state.count);
89
return c.state.count;
910
},
1011
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Hono } from "hono";
22
import { upgradeWebSocket, websocket } from "hono/bun";
3+
import { cors } from "hono/cors";
34
import { registry } from "./registry";
45

56
const { client, fetch } = registry.start({
@@ -10,11 +11,22 @@ const { client, fetch } = registry.start({
1011
overrideServerAddress: "http://localhost:8080/rivet",
1112
// Specify Hono-specific upgradeWebSocket
1213
getUpgradeWebSocket: () => upgradeWebSocket,
14+
cors: {
15+
origin: "http://localhost:5173",
16+
},
1317
});
1418

1519
// Setup router
1620
const app = new Hono();
1721

22+
app.use(
23+
"*",
24+
cors({
25+
origin: "http://localhost:5173",
26+
credentials: true,
27+
}),
28+
);
29+
1830
app.use("/rivet/*", async (c) => {
1931
return await fetch(c.req.raw, c.env);
2032
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createClient, createRivetKit } from "@rivetkit/react";
2+
import { useState } from "react";
3+
import type { registry } from "../backend/registry";
4+
5+
const client = createClient<typeof registry>("http://localhost:8080/rivet");
6+
const { useActor } = createRivetKit<typeof registry>(client);
7+
8+
function App() {
9+
const [count, setCount] = useState(0);
10+
const [counterName, setCounterName] = useState("test-counter");
11+
12+
const counter = useActor({
13+
name: "counter",
14+
key: [counterName],
15+
});
16+
17+
counter.useEvent("newCount", (x: number) => setCount(x));
18+
19+
const increment = async () => {
20+
await counter.connection?.increment(1);
21+
};
22+
23+
return (
24+
<div>
25+
<h1>Counter: {count}</h1>
26+
<input
27+
type="text"
28+
value={counterName}
29+
onChange={(e) => setCounterName(e.target.value)}
30+
placeholder="Counter name"
31+
/>
32+
<button onClick={increment}>Increment</button>
33+
</div>
34+
);
35+
}
36+
37+
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 Bun 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-bun/vite.config.ts

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

pnpm-lock.yaml

Lines changed: 36 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)