|
| 1 | +--- |
| 2 | +title: Better Auth |
| 3 | +--- |
| 4 | + |
| 5 | +<Steps> |
| 6 | +<Step title="Install"> |
| 7 | + |
| 8 | +```sh |
| 9 | +npm install better-auth |
| 10 | +``` |
| 11 | + |
| 12 | +</Step> |
| 13 | + |
| 14 | +<Step title="Update server"> |
| 15 | + |
| 16 | +<CodeGroup> |
| 17 | + |
| 18 | +```ts Hono |
| 19 | +import { registry } from "./registry"; |
| 20 | +import { Hono } from "hono"; |
| 21 | + |
| 22 | +// Start RivetKit |
| 23 | +// |
| 24 | +// State is stored in memory, this can be configured later |
| 25 | +const { client, hono } = registry.run(); |
| 26 | + |
| 27 | +// Setup server |
| 28 | +const app = new Hono(); |
| 29 | + |
| 30 | +// Expose RivetKit to the frontend (optional) |
| 31 | +app.route("/registry", hono); |
| 32 | + |
| 33 | +app.post("/increment/:name", async (c) => { |
| 34 | + const name = c.req.param("name"); |
| 35 | + |
| 36 | + // Communicate with actor |
| 37 | + const counter = client.counter.getOrCreate(name); |
| 38 | + const newCount = await counter.increment(1); |
| 39 | + |
| 40 | + return c.text(`New Count: ${newCount}`); |
| 41 | +}); |
| 42 | + |
| 43 | +// app.fetch will be used to run the server |
| 44 | +export { registry, default: app.fetch }; |
| 45 | +``` |
| 46 | + |
| 47 | +```ts Express.js |
| 48 | +TODO |
| 49 | +``` |
| 50 | + |
| 51 | +```ts Elysia.js |
| 52 | +TODO |
| 53 | +``` |
| 54 | + |
| 55 | +</CodeGroup> |
| 56 | + |
| 57 | +<Info> |
| 58 | + TODO: Exporting `registry` and `app.fetch` (as `default`) is important. |
| 59 | +</Info> |
| 60 | + |
| 61 | +_If you want to run without export fetch (i.e. standalone Node.js), see below._ |
| 62 | + |
| 63 | +</Step> |
| 64 | + |
| 65 | +<Step title="Integrate with RivetKit"> |
| 66 | + |
| 67 | +<CodeGroup> |
| 68 | + |
| 69 | +```ts Worker |
| 70 | +import { worker, setup } from "@rivetkit/worker"; |
| 71 | + |
| 72 | +export const counter = worker({ |
| 73 | + onAuth: () => { ... }, |
| 74 | + state: { count: 0 }, |
| 75 | + actions: { |
| 76 | + increment: (c, x: number) => { |
| 77 | + c.state.count += x; |
| 78 | + return c.state.count; |
| 79 | + }, |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +export const registry = setup({ |
| 84 | + workers: { counter }, |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +```ts Workflow |
| 89 | +``` |
| 90 | + |
| 91 | +</CodeGroup> |
| 92 | + |
| 93 | +</Step> |
| 94 | + |
| 95 | +<Step title="Run server"> |
| 96 | + |
| 97 | +<CodeGroup> |
| 98 | + |
| 99 | +```sh Node.js |
| 100 | +npx serve-fetch src/server.ts |
| 101 | +``` |
| 102 | + |
| 103 | +```sh Bun |
| 104 | +bun serve src/server.ts |
| 105 | +``` |
| 106 | + |
| 107 | +</CodeGroup> |
| 108 | + |
| 109 | +</Step> |
| 110 | + |
| 111 | +<Step title="Connect to backend"> |
| 112 | + |
| 113 | +<CodeGroup> |
| 114 | + |
| 115 | +```ts fetch |
| 116 | +const res = await fetch("http://localhost:8080/increment/foo", { |
| 117 | + method: "POST" |
| 118 | +}); |
| 119 | +console.log("Output:", await res.text()); |
| 120 | +``` |
| 121 | + |
| 122 | +```sh curl |
| 123 | +curl -X POST localhost:8080/increment/foo |
| 124 | +``` |
| 125 | + |
| 126 | +</CodeGroup> |
| 127 | + |
| 128 | + |
| 129 | +</Step> |
| 130 | + |
| 131 | +<Step title="Deploy"> |
| 132 | + |
| 133 | +<Tabs> |
| 134 | + |
| 135 | +<Tab title="Rivet"> |
| 136 | + |
| 137 | +```sh |
| 138 | +npx rivet-cli deploy src/server.ts |
| 139 | +``` |
| 140 | + |
| 141 | +Your endpoint is now TODO |
| 142 | + |
| 143 | +Test it with TODO |
| 144 | + |
| 145 | +</Tab> |
| 146 | + |
| 147 | +<Tab title="Cloudflare Workers"> |
| 148 | +TODO |
| 149 | +</Tab> |
| 150 | + |
| 151 | +<Tab title="Redis"> |
| 152 | + |
| 153 | +```sh |
| 154 | +npm install @rivetkit/redis |
| 155 | +``` |
| 156 | + |
| 157 | +```ts server.ts |
| 158 | +import { registry } from "./registry"; |
| 159 | +import { createRedisDriver } from "@rivetkit/redis"; |
| 160 | + |
| 161 | +// Start RivetKit |
| 162 | +const { client, hono } = registry.run({ |
| 163 | + driver: createRedisDriver({ |
| 164 | + host: "127.0.0.1", |
| 165 | + port: 6379, |
| 166 | + }), |
| 167 | +}); |
| 168 | + |
| 169 | +// ...rest of code... |
| 170 | +``` |
| 171 | + |
| 172 | +</Tab> |
| 173 | + |
| 174 | +<Tab title="File System"> |
| 175 | + |
| 176 | +```sh |
| 177 | +npm install @rivetkit/file-system |
| 178 | +``` |
| 179 | + |
| 180 | +```ts server.ts |
| 181 | +import { registry } from "./registry"; |
| 182 | +import { createFileSystemDriver } from "@rivetkit/file-system"; |
| 183 | + |
| 184 | +// Start RivetKit |
| 185 | +const { client, hono } = registry.run({ |
| 186 | + driver: createFileSystemDriver(), |
| 187 | +}); |
| 188 | + |
| 189 | +// ...rest of code... |
| 190 | +``` |
| 191 | + |
| 192 | +</Tab> |
| 193 | + |
| 194 | +</Tabs> |
| 195 | + |
| 196 | +</Step> |
| 197 | + |
| 198 | +</Steps> |
| 199 | + |
| 200 | +## Configuration Options |
| 201 | + |
| 202 | +### Connect your frontend to the Rivet Worker |
| 203 | + |
| 204 | +TODO: Quick summary of why you would want to connect your frontend |
| 205 | + |
| 206 | +Connect your frontend: |
| 207 | + |
| 208 | +<CodeGroup> |
| 209 | + |
| 210 | +```ts JavaScript |
| 211 | +import { createClient } from "@rivetkit/worker/client"; |
| 212 | +import type { registry } from "./registry.js"; |
| 213 | + |
| 214 | +const client = createClient<typeof registry>("http://localhost:8080/registry"); |
| 215 | + |
| 216 | +const result = await client.myWorker.getOrCreate().myAction("Hello, world!"); |
| 217 | +``` |
| 218 | + |
| 219 | +```ts React |
| 220 | +import { useState } from "react"; |
| 221 | +import { createClient, createRivetKit } from "@@rivetkit/worker/react"; |
| 222 | +import type { registry } from "./registry"; |
| 223 | + |
| 224 | +const client = createClient<typeof registry>(`http://localhost:8080/registry`); |
| 225 | +const { useWorker } = createRivetKit(client); |
| 226 | + |
| 227 | +function App() { |
| 228 | + const [count, setCount] = useState(0); |
| 229 | + const [counterName, setCounterName] = useState("test-counter"); |
| 230 | + |
| 231 | + const counter = useWorker({ |
| 232 | + name: "counter", |
| 233 | + key: [counterName], |
| 234 | + }); |
| 235 | + |
| 236 | + counter.useEvent("newCount", (x: number) => setCount(x)); |
| 237 | + |
| 238 | + const increment = async () => { |
| 239 | + await counter.connection?.increment(1); |
| 240 | + }; |
| 241 | + |
| 242 | + return ( |
| 243 | + <div> |
| 244 | + <h1>Counter: {count}</h1> |
| 245 | + <input |
| 246 | + type="text" |
| 247 | + value={counterName} |
| 248 | + onChange={(e) => setCounterName(e.target.value)} |
| 249 | + placeholder="Counter name" |
| 250 | + /> |
| 251 | + <button onClick={increment}>Increment</button> |
| 252 | + </div> |
| 253 | + ); |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +</CodeGroup> |
| 258 | + |
| 259 | +TODO: Learn more under the XXXX docs |
| 260 | + |
| 261 | +<Note> |
| 262 | + TODO: Link to onAuth docs |
| 263 | +</Note> |
| 264 | + |
| 265 | +<Note> |
| 266 | + TODO: Note that `/registry` must be exposed |
| 267 | +</Note> |
| 268 | + |
| 269 | +### Run as standalone server (no fetch handler) |
| 270 | + |
| 271 | +TODO: Intro |
| 272 | + |
| 273 | +```ts |
| 274 | +import { registry } from "./registry"; |
| 275 | +import { Hono } from "hono"; |
| 276 | +import { serve } from "@hono/node-server"; |
| 277 | + |
| 278 | +// Start RivetKit |
| 279 | +const { client, hono } = registry.run(); |
| 280 | + |
| 281 | +// Setup server |
| 282 | +const app = new Hono(); |
| 283 | + |
| 284 | +// ...setup routes... |
| 285 | + |
| 286 | +serve({ fetch: app.fetch, port: 8080 }, (x) => |
| 287 | + console.log("Listening at http://localhost:8080"), |
| 288 | +); |
| 289 | +``` |
| 290 | + |
| 291 | +IMPORTANT: You'll need to do special stuff to support deploying to Rivet or Cloudflare Workers |
| 292 | + |
| 293 | +## Next Steps |
| 294 | + |
| 295 | +TODO |
| 296 | + |
| 297 | +<CardGroup> |
| 298 | + <Card title="Overview" href="/concepts/overview" icon="square-plus" horizontal={true} /> |
| 299 | + <Card title="Interacting with Workers" href="/concepts/interacting-with-workers" icon="square-plus" horizontal={true} /> |
| 300 | + <Card title="Actions" href="/concepts/actions" icon="bolt" horizontal={true} /> |
| 301 | + <Card title="State" href="/concepts/state" icon="floppy-disk" horizontal={true} /> |
| 302 | +</CardGroup> |
| 303 | + |
0 commit comments