| 1 | --- |
| 2 | title: Effect RPC |
| 3 | description: Build a typed RPC API with Effect's Rpc module and deploy it as a Cloudflare Worker. |
| 4 | sidebar: |
| 5 | order: 4 |
| 6 | --- |
| 7 | |
| 8 | Effect RPC exposes a typed, schema-validated surface across a |
| 9 | **trust boundary** — a web app or an external service calling into |
| 10 | your Worker. For internal Worker-to-Worker or Worker-to-DO calls, |
| 11 | use [Schemaless RPC](/apis/schemaless) instead; for choosing between |
| 12 | the modalities, see [RPC](/apis). |
| 13 | |
| 14 | The [HTTP API guide](/cloudflare/apis/effect-http-api) showed how to build |
| 15 | REST-style endpoints with schema validation. Effect RPC takes a |
| 16 | different angle — you define **procedures** instead of HTTP |
| 17 | endpoints, and you get a fully typed client for free with no URL |
| 18 | construction or manual serialization. |
| 19 | |
| 20 | The transport is still HTTP under the hood, and both patterns |
| 21 | produce the same `HttpEffect` type, so the wiring story is |
| 22 | identical to the HTTP API guide: |
| 23 | |
| 24 | 1. **Define schemas outside.** Domain types and tagged errors, |
| 25 | importable by both server and client. |
| 26 | 2. **Construct the service inside the Worker's Construction phase.** |
| 27 | `RpcGroup.toLayer` is pure construction — safe to call at plan |
| 28 | time. Don't `yield*` the running server; it can't run without a |
| 29 | request. |
| 30 | 3. **Return `{ fetch }`** where `fetch` is the `HttpEffect` |
| 31 | produced by `RpcServer.toHttpEffect`. |
| 32 | 4. **Bonus:** deploy and call the procedures from a typed client |
| 33 | that shares the exact same `RpcGroup` value. |
| 34 | |
| 35 | ## 1. Define the schemas |
| 36 | |
| 37 | Domain model and error types — pure schemas, no runtime concerns: |
| 38 | |
| 39 | ```typescript |
| 40 | // src/task.ts |
| 41 | import * as Schema from "effect/Schema"; |
| 42 | |
| 43 | export class Task extends Schema.Class<Task>("Task")({ |
| 44 | id: Schema.String, |
| 45 | title: Schema.String, |
| 46 | completed: Schema.Boolean, |
| 47 | }) {} |
| 48 | |
| 49 | export class TaskNotFound extends Schema.TaggedClass<TaskNotFound>()( |
| 50 | "TaskNotFound", |
| 51 | { id: Schema.String }, |
| 52 | ) {} |
| 53 | |
| 54 | export class CreateTaskFailed extends Schema.TaggedClass<CreateTaskFailed>()( |
| 55 | "CreateTaskFailed", |
| 56 | { message: Schema.String }, |
| 57 | ) {} |
| 58 | ``` |
| 59 | |
| 60 | RPC errors are schema-backed tagged classes. The client receives |
| 61 | them as typed values you can `match` on — not raw HTTP status codes. |
| 62 | |
| 63 | ## 2. Declare the RPCs |
| 64 | |
| 65 | Each `Rpc.make` declares one procedure: a name, a payload schema, |
| 66 | a success schema, and an error schema. `RpcGroup.make` collects them |
| 67 | into a single value that both the server and the client will share. |
| 68 | |
| 69 | ```typescript |
| 70 | // src/rpcs.ts |
| 71 | import * as Schema from "effect/Schema"; |
| 72 | import { Rpc, RpcGroup } from "effect/rpc"; |
| 73 | import { Task, TaskNotFound, CreateTaskFailed } from "./task.ts"; |
| 74 | |
| 75 | const getTask = Rpc.make("getTask", { |
| 76 | success: Task, |
| 77 | error: TaskNotFound, |
| 78 | payload: { id: Schema.String }, |
| 79 | }); |
| 80 | |
| 81 | const createTask = Rpc.make("createTask", { |
| 82 | success: Task, |
| 83 | error: CreateTaskFailed, |
| 84 | payload: { title: Schema.String }, |
| 85 | }); |
| 86 | |
| 87 | export class TaskRpcs extends RpcGroup.make(getTask, createTask) {} |
| 88 | ``` |
| 89 | |
| 90 | `TaskRpcs` is just a value-level description. Nothing executes yet. |
| 91 | |
| 92 | ## 3. Build the Worker |
| 93 | |
| 94 | Create `src/worker.ts` with an empty Construction phase: |
| 95 | |
| 96 | ```typescript |
| 97 | // src/worker.ts |
| 98 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 99 | import * as Effect from "effect/Effect"; |
| 100 | |
| 101 | export default Cloudflare.Worker( |
| 102 | "Worker", |
| 103 | { main: import.meta.url }, |
| 104 | Effect.gen(function* () { |
| 105 | return {}; |
| 106 | }), |
| 107 | ); |
| 108 | ``` |
| 109 | |
| 110 | The generator inside `Cloudflare.Worker` is the **Construction phase** — it |
| 111 | runs both at *plan time* and at *runtime*. Only do pure construction |
| 112 | or resource-binding factories here; never `yield*` work that needs |
| 113 | an incoming request. |
| 114 | |
| 115 | ### 3a. Bind an R2 bucket for storage |
| 116 | |
| 117 | Tasks need durable storage. Declare an `Bucket` resource and bind |
| 118 | it inside the constructor — `bind()` returns a typed handle whose `get` / |
| 119 | `put` / `delete` / `list` methods we'll call from the handlers |
| 120 | below. |
| 121 | |
| 122 | ```typescript |
| 123 | // src/bucket.ts |
| 124 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 125 | |
| 126 | export const Tasks = Cloudflare.R2.Bucket("Tasks"); |
| 127 | ``` |
| 128 | |
| 129 | ```diff lang="typescript" |
| 130 | +import { Tasks } from "./bucket.ts"; |
| 131 | |
| 132 | export default Cloudflare.Worker( |
| 133 | "Worker", |
| 134 | { main: import.meta.url }, |
| 135 | Effect.gen(function* () { |
| 136 | + const tasks = yield* Cloudflare.R2.ReadWriteBucket(Tasks); |
| 137 | |
| 138 | return {}; |
| 139 | }), |
| 140 | ); |
| 141 | ``` |
| 142 | |
| 143 | We'll provide the runtime side of this binding |
| 144 | (`Cloudflare.R2.ReadWriteBucketBinding`) in step 3c when we wire up the |
| 145 | `fetch` handler. |
| 146 | |
| 147 | ### 3b. Construct the handlers inside the constructor |
| 148 | |
| 149 | `TaskRpcs.toLayer` takes an `Effect` that returns one handler per |
| 150 | procedure and produces a `Layer`. Like `HttpApiBuilder.group`, this |
| 151 | is pure construction — it builds a value, it doesn't run the server. |
| 152 | |
| 153 | > Don't `yield* TaskRpcs.toLayer(...)` here. Building a layer is |
| 154 | > fine, but actually executing the procedures requires an incoming |
| 155 | > request. The constructor only constructs; the work happens later, on each |
| 156 | > `fetch` call. |
| 157 | |
| 158 | ```diff lang="typescript" |
| 159 | Effect.gen(function* () { |
| 160 | const tasks = yield* Cloudflare.R2.ReadWriteBucket(Tasks); |
| 161 | |
| 162 | + const handlersLayer = TaskRpcs.toLayer({ |
| 163 | + getTask: ({ id }) => |
| 164 | + Effect.gen(function* () { |
| 165 | + const object = yield* tasks.get(id); |
| 166 | + if (!object) { |
| 167 | + return yield* Effect.fail(new TaskNotFound({ id })); |
| 168 | + } |
| 169 | + return Schema.decodeUnknownSync(Task)(JSON.parse(yield* object.text())); |
| 170 | + }).pipe(Effect.orDie), |
| 171 | + createTask: ({ title }) => |
| 172 | + Effect.gen(function* () { |
| 173 | + const task = new Task({ |
| 174 | + id: crypto.randomUUID(), |
| 175 | + title, |
| 176 | + completed: false, |
| 177 | + }); |
| 178 | + yield* tasks.put(task.id, JSON.stringify(task)); |
| 179 | + return task; |
| 180 | + }).pipe( |
| 181 | + Effect.catchTag("R2Error", (error) => |
| 182 | + Effect.fail(new CreateTaskFailed({ message: error.message })), |
| 183 | + ), |
| 184 | + ), |
| 185 | + }); |
| 186 | |
| 187 | return {}; |
| 188 | }), |
| 189 | ``` |
| 190 | |
| 191 | Each handler receives the typed payload and returns an `Effect` |
| 192 | that either succeeds with the declared success schema or fails with |
| 193 | the declared error schema. `getTask` uses `Effect.orDie` to turn |
| 194 | unexpected R2 failures into 500s — `TaskNotFound` is the only |
| 195 | client-visible error. `createTask` maps R2 failures into the |
| 196 | declared `CreateTaskFailed` error so the client can match on it. |
| 197 | |
| 198 | ### 3c. Return `{ fetch }` |
| 199 | |
| 200 | `RpcServer.toHttpEffect` converts the `RpcGroup` into an |
| 201 | `HttpEffect` — exactly the type Workers expect for `fetch`. We |
| 202 | provide two layers to it via `Layer.mergeAll`: |
| 203 | |
| 204 | - The `handlersLayer` we just built. |
| 205 | - `RpcSerialization.layerNdjson` — one frame per line, so streaming |
| 206 | procedures work out of the box and the wire format matches what |
| 207 | `Cloudflare.RpcWorker.bind` clients speak. When nothing streams |
| 208 | and no bound consumer is involved, the buffered |
| 209 | `RpcSerialization.layerJson` is also an option. |
| 210 | |
| 211 | Unlike the HTTP API approach, RPC doesn't need `HttpPlatform.layer` |
| 212 | or `Etag.layer` — the RPC server handles message framing internally. |
| 213 | |
| 214 | ```diff lang="typescript" |
| 215 | return { |
| 216 | + fetch: RpcServer.toHttpEffect(TaskRpcs).pipe( |
| 217 | + Effect.provide( |
| 218 | + Layer.mergeAll(handlersLayer, RpcSerialization.layerNdjson), |
| 219 | + ), |
| 220 | + ), |
| 221 | }; |
| 222 | ``` |
| 223 | |
| 224 | ### Putting it together |
| 225 | |
| 226 | ```typescript |
| 227 | // src/worker.ts |
| 228 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 229 | import * as Effect from "effect/Effect"; |
| 230 | import * as Layer from "effect/Layer"; |
| 231 | import * as Schema from "effect/Schema"; |
| 232 | import { RpcSerialization, RpcServer } from "effect/rpc"; |
| 233 | import { Tasks } from "./bucket.ts"; |
| 234 | import { CreateTaskFailed, Task, TaskNotFound } from "./task.ts"; |
| 235 | import { TaskRpcs } from "./rpcs.ts"; |
| 236 | |
| 237 | export default Cloudflare.Worker( |
| 238 | "Worker", |
| 239 | { main: import.meta.url }, |
| 240 | Effect.gen(function* () { |
| 241 | const tasks = yield* Cloudflare.R2.ReadWriteBucket(Tasks); |
| 242 | |
| 243 | const handlersLayer = TaskRpcs.toLayer({ |
| 244 | getTask: ({ id }) => |
| 245 | Effect.gen(function* () { |
| 246 | const object = yield* tasks.get(id); |
| 247 | if (!object) { |
| 248 | return yield* Effect.fail(new TaskNotFound({ id })); |
| 249 | } |
| 250 | return Schema.decodeUnknownSync(Task)( |
| 251 | JSON.parse(yield* object.text()), |
| 252 | ); |
| 253 | }).pipe(Effect.orDie), |
| 254 | createTask: ({ title }) => |
| 255 | Effect.gen(function* () { |
| 256 | const task = new Task({ |
| 257 | id: crypto.randomUUID(), |
| 258 | title, |
| 259 | completed: false, |
| 260 | }); |
| 261 | yield* tasks.put(task.id, JSON.stringify(task)); |
| 262 | return task; |
| 263 | }).pipe( |
| 264 | Effect.catchTag("R2Error", (error) => |
| 265 | Effect.fail(new CreateTaskFailed({ message: error.message })), |
| 266 | ), |
| 267 | ), |
| 268 | }); |
| 269 | |
| 270 | return { |
| 271 | fetch: RpcServer.toHttpEffect(TaskRpcs).pipe( |
| 272 | Effect.provide( |
| 273 | Layer.mergeAll(handlersLayer, RpcSerialization.layerNdjson), |
| 274 | ), |
| 275 | ), |
| 276 | }; |
| 277 | }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)), |
| 278 | ); |
| 279 | ``` |
| 280 | |
| 281 | ## 4. Add to the Stack |
| 282 | |
| 283 | ```typescript |
| 284 | // alchemy.run.ts |
| 285 | import * as Alchemy from "alchemy"; |
| 286 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 287 | import * as Effect from "effect/Effect"; |
| 288 | import Worker from "./src/worker.ts"; |
| 289 | |
| 290 | export default Alchemy.Stack( |
| 291 | "TaskRpc", |
| 292 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 293 | Effect.gen(function* () { |
| 294 | const worker = yield* Worker; |
| 295 | return { url: worker.url }; |
| 296 | }), |
| 297 | ); |
| 298 | ``` |
| 299 | |
| 300 | ## 5. Deploy |
| 301 | |
| 302 | ```sh |
| 303 | alchemy deploy |
| 304 | ``` |
| 305 | |
| 306 | ## Bonus: call it from a typed client |
| 307 | |
| 308 | Because `TaskRpcs` is just a value, the same group drives a fully |
| 309 | typed client — no codegen. `client.createTask` accepts |
| 310 | `{ title: string }` and returns `Effect<Task, CreateTaskFailed>`. |
| 311 | |
| 312 | ```typescript |
| 313 | // scripts/client.ts |
| 314 | import * as Effect from "effect/Effect"; |
| 315 | import * as Layer from "effect/Layer"; |
| 316 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 317 | import { RpcClient, RpcSerialization } from "effect/rpc"; |
| 318 | import { TaskRpcs } from "../src/rpcs.ts"; |
| 319 | |
| 320 | const program = Effect.gen(function* () { |
| 321 | const client = yield* RpcClient.make(TaskRpcs); |
| 322 | |
| 323 | const task = yield* client.createTask({ title: "Write docs" }); |
| 324 | console.log("Created:", task.id); |
| 325 | |
| 326 | const fetched = yield* client.getTask({ id: task.id }); |
| 327 | console.log("Fetched:", fetched.title); |
| 328 | }); |
| 329 | |
| 330 | Effect.runPromise( |
| 331 | program.pipe( |
| 332 | Effect.scoped, |
| 333 | Effect.provide( |
| 334 | RpcClient.layerProtocolHttp({ url: process.env.TASK_RPC_URL! }).pipe( |
| 335 | Layer.provide(FetchHttpClient.layer), |
| 336 | Layer.provide(RpcSerialization.layerNdjson), |
| 337 | ), |
| 338 | ), |
| 339 | ), |
| 340 | ); |
| 341 | ``` |
| 342 | |
| 343 | Get the URL from the deploy output and run it: |
| 344 | |
| 345 | ```sh |
| 346 | TASK_RPC_URL=https://your-worker.workers.dev bun scripts/client.ts |
| 347 | ``` |
| 348 | |
| 349 | The errors are typed values: `client.getTask` returns |
| 350 | `Effect<Task, TaskNotFound>`, and you can `Effect.catchTag( |
| 351 | "TaskNotFound", ...)` to handle the missing case explicitly. |
| 352 | |
| 353 | ## Bonus: streaming responses |
| 354 | |
| 355 | An RPC's `success` doesn't have to be a single value. Wrapping it in |
| 356 | `RpcSchema.Stream(item, error)` produces a procedure whose handler |
| 357 | returns a `Stream` and whose client method also returns a `Stream`. |
| 358 | The wire format is one frame per item — exactly what the |
| 359 | `RpcSerialization.layerNdjson` layer we're already on emits, so the |
| 360 | `fetch` wiring doesn't change. |
| 361 | |
| 362 | ```typescript |
| 363 | // src/rpcs.ts |
| 364 | import * as RpcSchema from "effect/rpc/RpcSchema"; |
| 365 | |
| 366 | const countTasks = Rpc.make("countTasks", { |
| 367 | payload: { upto: Schema.Number }, |
| 368 | success: RpcSchema.Stream(Schema.Number, Schema.Never), |
| 369 | }); |
| 370 | |
| 371 | export class TaskRpcs extends RpcGroup.make(getTask, createTask, countTasks) {} |
| 372 | ``` |
| 373 | |
| 374 | The handler returns a `Stream<number>` directly: |
| 375 | |
| 376 | ```diff lang="typescript" |
| 377 | +import * as Stream from "effect/Stream"; |
| 378 | |
| 379 | const handlersLayer = TaskRpcs.toLayer({ |
| 380 | getTask: /* ... */, |
| 381 | createTask: /* ... */, |
| 382 | + countTasks: ({ upto }) => |
| 383 | + Stream.fromIterable(Array.from({ length: upto }, (_, i) => i + 1)), |
| 384 | }); |
| 385 | ``` |
| 386 | |
| 387 | On the client, `client.countTasks({ upto: 5 })` is a `Stream<number>` |
| 388 | you consume with `Stream.runCollect`, `Stream.runForEach`, etc. Each |
| 389 | emitted item arrives as soon as the server flushes its frame. |
| 390 | |
| 391 | ## Recap |
| 392 | |
| 393 | - **Schemas** (`Task`, `TaskNotFound`, `CreateTaskFailed`) and the |
| 394 | **RPC group** (`TaskRpcs`) live outside the Worker — pure |
| 395 | descriptions, importable by clients. |
| 396 | - The **handlers** are constructed inside the Worker's Construction phase |
| 397 | closure via `TaskRpcs.toLayer`. We build a `Layer` but never |
| 398 | `yield*` the running server. |
| 399 | - The Worker's surface is `{ fetch }`, where `fetch` is the |
| 400 | `HttpEffect` produced by `RpcServer.toHttpEffect`. |
| 401 | - The same `TaskRpcs` value drives a fully typed client via |
| 402 | `RpcClient.make`, with errors as typed values rather than HTTP |
| 403 | status codes. |
| 404 | |
| 405 | ## Sugar: `Cloudflare.RpcWorker` |
| 406 | |
| 407 | The `Cloudflare.Worker(...)` + `RpcServer.toHttpEffect(...)` recipe is |
| 408 | identical for every RPC Worker, so Alchemy ships a thin wrapper that |
| 409 | takes the `RpcGroup` directly in props and removes the `{ fetch }` |
| 410 | wrapper: |
| 411 | |
| 412 | ```typescript |
| 413 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 414 | import * as Effect from "effect/Effect"; |
| 415 | import * as Layer from "effect/Layer"; |
| 416 | import { RpcSerialization, RpcServer } from "effect/rpc"; |
| 417 | import { TaskRpcs } from "./rpcs.ts"; |
| 418 | |
| 419 | export default class Worker extends Cloudflare.RpcWorker<Worker>()( |
| 420 | "Worker", |
| 421 | { main: import.meta.url, schema: TaskRpcs }, |
| 422 | Effect.gen(function* () { |
| 423 | const handlers = TaskRpcs.toLayer({ getTask: /* ... */ }); |
| 424 | return RpcServer.toHttpEffect(TaskRpcs).pipe( |
| 425 | Effect.provide(Layer.mergeAll(handlers, RpcSerialization.layerNdjson)), |
| 426 | ); |
| 427 | }), |
| 428 | ) {} |
| 429 | ``` |
| 430 | |
| 431 | Functionally identical to the long form above — yielding the class |
| 432 | returns the same `Worker` resource — but `props.schema` lets a second |
| 433 | Worker bind a typed client without re-importing `RpcClient`: |
| 434 | |
| 435 | ```typescript |
| 436 | // INIT: register the binding, get the typed client |
| 437 | const tasks = yield* Cloudflare.RpcWorker.bind(TaskWorker); |
| 438 | |
| 439 | // PER-REQUEST: just call methods directly |
| 440 | proxyGetTask: ({ id }) => tasks.getTask({ id }), |
| 441 | ``` |
| 442 | |
| 443 | The bind goes over the in-account service binding (not the public |
| 444 | internet) and the client is typed by `TaskWorker`'s declared schema. |
| 445 | A `Proxy` defers each call's underlying `RpcClient` construction so |
| 446 | Cloudflare's "no cross-request I/O" rule is satisfied transparently. |
| 447 | For internal Worker-to-Worker calls that don't need a schema at all, |
| 448 | see [Schemaless RPC on Workers](/cloudflare/compute/workers#call-another-worker). |
| 449 | |
| 450 | `RpcWorker` also supports a **modular form** that separates the |
| 451 | class declaration from its runtime — useful when a consumer Worker |
| 452 | should be able to import the class for binding without pulling in |
| 453 | the host's runtime: |
| 454 | |
| 455 | ```typescript |
| 456 | // Modular: class declaration carries no impl — just the schema. |
| 457 | export class TaskWorker extends Cloudflare.RpcWorker<TaskWorker>()( |
| 458 | "TaskWorker", |
| 459 | { schema: TaskRpcs }, |
| 460 | ) {} |
| 461 | |
| 462 | // Runtime lives in a separate Layer — only the host script imports it. |
| 463 | // `make` takes the Worker props (main, etc.) plus the impl. |
| 464 | export default TaskWorker.make( |
| 465 | { main: import.meta.url }, |
| 466 | Effect.gen(function* () { |
| 467 | const handlers = TaskRpcs.toLayer({ /* ... */ }); |
| 468 | return RpcServer.toHttpEffect(TaskRpcs).pipe( |
| 469 | Effect.provide(Layer.mergeAll(handlers, RpcSerialization.layerNdjson)), |
| 470 | ); |
| 471 | }), |
| 472 | ); |
| 473 | ``` |
| 474 | |
| 475 | The class can also declare DOs it publishes via the second type |
| 476 | argument — `RpcWorker<Self, Deps>()` mirrors |
| 477 | `Cloudflare.Worker<Self, Bindings, Deps>` so cross-script |
| 478 | `Counter.from(TaskWorker)` type-checks. |
| 479 | |
| 480 | ## Sugar: `Cloudflare.RpcDurableObject` |
| 481 | |
| 482 | The same shape applies to Durable Objects. |
| 483 | `Cloudflare.RpcDurableObject<Self>()(...)` mirrors the |
| 484 | regular DO class. Return the RPC handler Layer from the inner Effect; |
| 485 | Alchemy supplies the server and serialization. This example uses a shared |
| 486 | `CounterRpcs` group: |
| 487 | |
| 488 | ```typescript |
| 489 | // rpcs.ts |
| 490 | import * as Schema from "effect/Schema"; |
| 491 | import { Rpc, RpcGroup } from "effect/rpc"; |
| 492 | |
| 493 | export class CounterRpcs extends RpcGroup.make( |
| 494 | Rpc.make("setTitle", { |
| 495 | payload: { title: Schema.String }, |
| 496 | success: Schema.Void, |
| 497 | }), |
| 498 | Rpc.make("getTitle", { payload: {}, success: Schema.String }), |
| 499 | ) {} |
| 500 | ``` |
| 501 | |
| 502 | Return its handler Layer from the per-instance Effect: |
| 503 | |
| 504 | ```typescript |
| 505 | // counter.ts |
| 506 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 507 | import * as Effect from "effect/Effect"; |
| 508 | import { CounterRpcs } from "./rpcs.ts"; |
| 509 | |
| 510 | export default class Counter extends Cloudflare.RpcDurableObject<Counter>()( |
| 511 | "Counter", |
| 512 | { schema: CounterRpcs }, |
| 513 | Effect.gen(function* () { |
| 514 | const state = yield* Cloudflare.DurableObjectState; |
| 515 | return Effect.gen(function* () { |
| 516 | return CounterRpcs.toLayer({ |
| 517 | setTitle: ({ title }) => state.storage.put("title", title), |
| 518 | getTitle: () => state.storage.get<string>("title").pipe( |
| 519 | Effect.map((title) => title ?? ""), |
| 520 | ), |
| 521 | }); |
| 522 | }); |
| 523 | }), |
| 524 | ) {} |
| 525 | ``` |
| 526 | |
| 527 | `counters.getByName(id)` returns an `Effect<RpcClient<CounterRpcs>>` |
| 528 | (yield it inside a per-request scope) rather than alchemy's built-in |
| 529 | DO method bridge. Reach for this when an external client connects to |
| 530 | the DO and speaks RPC over a WebSocket connection, or whenever DO |
| 531 | method return values cross a `Schema.Class` boundary — the built-in bridge |
| 532 | `JSON.stringify`s each value and loses class identity, while the RPC |
| 533 | namespace round-trips through the shared `RpcSerialization` codec. |
| 534 | The concept home for this trade-off is |
| 535 | [Effect RPC](/apis/effect-rpc). |
| 536 | |
| 537 | Like `RpcWorker`, the RPC DO supports a **modular form** that |
| 538 | separates the class from its runtime so consumer Workers can bind |
| 539 | to it cross-script: |
| 540 | |
| 541 | ```typescript |
| 542 | // counter.ts — class declaration carries no impl. |
| 543 | export class Counter extends Cloudflare.RpcDurableObject<Counter>()( |
| 544 | "Counter", |
| 545 | { schema: CounterRpcs }, |
| 546 | ) {} |
| 547 | |
| 548 | // Runtime lives in a separate Layer — only the host Worker imports it. |
| 549 | export default Counter.make( |
| 550 | Effect.gen(function* () { |
| 551 | const state = yield* Cloudflare.DurableObjectState; |
| 552 | return Effect.gen(function* () { |
| 553 | return CounterRpcs.toLayer({ |
| 554 | setTitle: ({ title }) => state.storage.put("title", title), |
| 555 | getTitle: () => state.storage.get<string>("title").pipe( |
| 556 | Effect.map((title) => title ?? ""), |
| 557 | ), |
| 558 | }); |
| 559 | }); |
| 560 | }), |
| 561 | ); |
| 562 | ``` |
| 563 | |
| 564 | From a consumer Worker, `Counter.from(HostWorker)` produces a |
| 565 | typed namespace bound to the host's running DO instances: |
| 566 | |
| 567 | ```typescript |
| 568 | const counters = yield* Counter.from(HostWorker); |
| 569 | const stub = yield* counters.getByName("shared"); |
| 570 | yield* stub.setTitle({ title: "hi" }); |
| 571 | ``` |
| 572 | |
| 573 | For internal DO calls that don't need a schema, see |
| 574 | [Schemaless RPC on Durable Objects](/cloudflare/compute/durable-objects#schemaless-rpc). |
| 575 | |
| 576 | ## HTTP and WebSocket RPC |
| 577 | |
| 578 | Returning a handler Layer enables both HTTP and hibernating WebSocket RPC |
| 579 | automatically. Keep the declaration as `{ schema: CounterRpcs }`; the |
| 580 | incoming request selects the transport. |
| 581 | |
| 582 | WebSocket upgrades use Effect RPC's JSON protocol. Ordinary HTTP requests |
| 583 | and existing `getByName` clients continue to use NDJSON. A WebSocket is |
| 584 | opened only when a client connects over one. |
| 585 | |
| 586 | Existing implementations returning `RpcServer.toHttpEffect(...)` remain |
| 587 | HTTP-only. Return the handler Layer instead to let Alchemy provide both |
| 588 | transports. |
| 589 | |
| 590 | ## Forward browser connections |
| 591 | |
| 592 | The hosting Worker maps a URL to a named Durable Object. This Worker |
| 593 | forwards `/counters/alice` to `"alice"` and `/counters/bob` to `"bob"`: |
| 594 | |
| 595 | ```typescript |
| 596 | // worker.ts |
| 597 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 598 | import * as Effect from "effect/Effect"; |
| 599 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 600 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 601 | import Counter from "./counter.ts"; |
| 602 | |
| 603 | export default class CounterWorker extends Cloudflare.Worker<CounterWorker>()( |
| 604 | "CounterWorker", |
| 605 | { main: import.meta.url }, |
| 606 | Effect.gen(function* () { |
| 607 | const counters = yield* Counter; |
| 608 | return { |
| 609 | fetch: Effect.gen(function* () { |
| 610 | const request = yield* HttpServerRequest; |
| 611 | const path = new URL(request.url, "https://worker").pathname; |
| 612 | const name = /^\/counters\/([a-zA-Z0-9_-]+)$/.exec(path)?.[1]; |
| 613 | if (!name) return HttpServerResponse.empty({ status: 404 }); |
| 614 | return yield* counters.fetch(name, request); |
| 615 | }), |
| 616 | }; |
| 617 | }), |
| 618 | ) {} |
| 619 | ``` |
| 620 | |
| 621 | `counters.fetch(name, request)` selects that name in the `Counter` namespace |
| 622 | and forwards the HTTP request or WebSocket upgrade. It is not an application |
| 623 | RPC method, and `/counters/:name` is an application route, not a built-in |
| 624 | Alchemy endpoint. |
| 625 | |
| 626 | Once upgraded, every RPC on that socket goes to the selected object; method |
| 627 | payloads do not need an object ID. Connections to the same name in the same |
| 628 | namespace share the object's state. Different names select different objects. |
| 629 | A socket cannot switch objects: open another connection to target another name. |
| 630 | |
| 631 | This routing-only example accepts any name matching the route. For a private |
| 632 | API, authenticate the request and authorize access to the selected name before |
| 633 | calling `fetch`, or derive the name from the authenticated user or tenant. |
| 634 | Knowing an object's URL must not grant access by itself. |
| 635 | |
| 636 | ## Connect over a WebSocket |
| 637 | |
| 638 | Connect to the Worker's `/counters/alice` route and create the browser's |
| 639 | Effect RPC client in a Layer. This client talks only to the `"alice"` object: |
| 640 | |
| 641 | ```typescript |
| 642 | import * as RpcWebSocketClient from "alchemy/Cloudflare/RpcWebSocketClient"; |
| 643 | import * as Context from "effect/Context"; |
| 644 | import * as Effect from "effect/Effect"; |
| 645 | import * as Layer from "effect/Layer"; |
| 646 | import * as RpcClient from "effect/rpc/RpcClient"; |
| 647 | import type { RpcClientError } from "effect/rpc/RpcClientError"; |
| 648 | import * as Socket from "effect/socket/Socket"; |
| 649 | import { CounterRpcs } from "./rpcs.ts"; |
| 650 | |
| 651 | class CounterClient extends Context.Service< |
| 652 | CounterClient, |
| 653 | RpcClient.FromGroup<typeof CounterRpcs, RpcClientError> |
| 654 | >()("CounterClient") {} |
| 655 | |
| 656 | const CounterClientLive = RpcWebSocketClient.layer( |
| 657 | CounterClient, |
| 658 | CounterRpcs, |
| 659 | "wss://example.com/counters/alice", |
| 660 | ).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal)); |
| 661 | |
| 662 | const program = Effect.gen(function* () { |
| 663 | const counter = yield* CounterClient; |
| 664 | yield* counter.setTitle({ title: "Hello over WebSockets" }); |
| 665 | return yield* counter.getTitle({}); |
| 666 | }).pipe(Effect.provide(CounterClientLive)); |
| 667 | ``` |
| 668 | |
| 669 | The dedicated `alchemy/Cloudflare/RpcWebSocketClient` entrypoint imports only |
| 670 | Effect modules and is safe to bundle for browsers. The wrapper is optional; |
| 671 | it composes Effect's RPC client, socket protocol, WebSocket, and JSON serializer. |
| 672 | Client middleware remains an explicit Layer dependency. Its `socket`, `protocol`, |
| 673 | and `client` options pass through to Effect; `serialization` defaults to JSON |
| 674 | and must match the server. See the |
| 675 | [`RpcWebSocketClient.layer` API reference](/providers/cloudflare/workers/layer). |
| 676 | |
| 677 | The Layer owns the client and connection lifetime, so ordinary calls do not need |
| 678 | an explicit `Effect.scoped`. It closes the connection when the provided program |
| 679 | finishes, fails, or is interrupted. Provide it around the whole application or |
| 680 | component program that shares the client, rather than constructing it for each |
| 681 | call. Concurrent calls and streams share the socket; Effect handles typed errors, |
| 682 | backpressure, and cancellation. |
| 683 | |
| 684 | ### Keep a client for a browser session |
| 685 | |
| 686 | For a UI driven by callbacks, keep one managed runtime for the session: |
| 687 | |
| 688 | ```typescript |
| 689 | import * as ManagedRuntime from "effect/ManagedRuntime"; |
| 690 | |
| 691 | const runtime = ManagedRuntime.make(CounterClientLive); |
| 692 | |
| 693 | const getTitle = () => runtime.runPromise( |
| 694 | Effect.gen(function* () { |
| 695 | const counter = yield* CounterClient; |
| 696 | return yield* counter.getTitle({}); |
| 697 | }), |
| 698 | ); |
| 699 | ``` |
| 700 | |
| 701 | On application or component teardown, release that runtime: |
| 702 | |
| 703 | ```typescript |
| 704 | await runtime.dispose(); |
| 705 | ``` |
| 706 | |
| 707 | Your application wires teardown to its UI lifecycle; the Layer does not detect |
| 708 | component unmounts. Keeping the browser connection open does not prevent |
| 709 | Cloudflare from hibernating an idle Durable Object. When calling from another |
| 710 | Cloudflare Worker rather than a browser, provide the client Layer inside each |
| 711 | request; Worker sockets cannot be shared across request contexts. |
| 712 | |
| 713 | ### Subscription scopes |
| 714 | |
| 715 | Ordinary streaming RPCs return an Effect `Stream`; consume them with stream |
| 716 | operators while the client Layer is alive. Requesting `{ asQueue: true }` |
| 717 | returns a queue acquisition that still requires a consumer `Scope`. The Layer |
| 718 | owns the shared connection, not the lifetime of each subscription. Scope those |
| 719 | queue consumers separately so ending one subscription does not close the client. |
| 720 | |
| 721 | ### Compose the built-in Effect layers directly |
| 722 | |
| 723 | The convenience wrapper is equivalent to this composition, using the same |
| 724 | `CounterClient` service and RPC group: |
| 725 | |
| 726 | ```typescript |
| 727 | import * as RpcSerialization from "effect/rpc/RpcSerialization"; |
| 728 | |
| 729 | const CounterClientLive = Layer.effect( |
| 730 | CounterClient, |
| 731 | RpcClient.make(CounterRpcs), |
| 732 | ).pipe( |
| 733 | Layer.provide( |
| 734 | RpcClient.layerProtocolSocket().pipe( |
| 735 | Layer.provide([ |
| 736 | Socket.layerWebSocket("wss://example.com/counters/alice"), |
| 737 | RpcSerialization.layerJson, |
| 738 | ]), |
| 739 | Layer.provide(Socket.layerWebSocketConstructorGlobal), |
| 740 | ), |
| 741 | ), |
| 742 | ); |
| 743 | ``` |
| 744 | |
| 745 | ## WebSocket RPC and hibernation |
| 746 | |
| 747 | Idle connections can survive Durable Object hibernation and accept new |
| 748 | calls after the instance wakes. Alchemy restores connection metadata |
| 749 | from socket attachments and handles protocol heartbeats internally. A restored |
| 750 | socket with a different recorded serializer content type is reset. This is not |
| 751 | a schema-version check: changes that keep the same content type are not detected, |
| 752 | so coordinate incompatible schema or codec changes with clients. |
| 753 | |
| 754 | In-flight calls and streams are not replayed. If the instance is |
| 755 | reconstructed with unfinished work, the affected socket closes with |
| 756 | code `1012`; clients must handle the interruption. A platform reset can |
| 757 | close the connection before Alchemy restores it, so other transport errors |
| 758 | are also possible. Durable checkpoints and resumable subscriptions are |
| 759 | not part of this API. Application RPC methods do not need acknowledgment |
| 760 | or lifecycle methods. |
| 761 | |
| 762 | ## Under the hood: the manual DO bridge |
| 763 | |
| 764 | `RpcDurableObject.getByName(id)` above returns a wired `RpcClient` |
| 765 | directly — you rarely need more. This section shows what it does |
| 766 | internally, and doubles as the escape hatch when the `RpcServer` |
| 767 | runs inside a plain `Cloudflare.DurableObject` and you build the |
| 768 | bridge yourself: `Cloudflare.toHttpClient(stub)` turns the DO stub |
| 769 | into an `HttpClient` that plugs into `RpcClient.layerProtocolHttp`. |
| 770 | |
| 771 | ### Split the RPCs into two groups |
| 772 | |
| 773 | Put the procedures the DO implements into one group, and the |
| 774 | Worker-only proxies into another. `merge` produces the public group |
| 775 | the Worker exposes: |
| 776 | |
| 777 | ```typescript |
| 778 | // src/rpcs.ts |
| 779 | export const InnerRpcs = RpcGroup.make(getTask, createTask, countTasks); |
| 780 | |
| 781 | export const DoRpcs = RpcGroup.make( |
| 782 | Rpc.make("getTaskDO", { |
| 783 | payload: { id: Schema.String }, |
| 784 | success: Task, |
| 785 | error: TaskNotFound, |
| 786 | }), |
| 787 | Rpc.make("createTaskDO", { |
| 788 | payload: { title: Schema.String }, |
| 789 | success: Task, |
| 790 | error: CreateTaskFailed, |
| 791 | }), |
| 792 | ); |
| 793 | |
| 794 | export class TaskRpcs extends InnerRpcs.merge(DoRpcs) {} |
| 795 | ``` |
| 796 | |
| 797 | ### Implement the DO |
| 798 | |
| 799 | The DO's constructor returns `{ fetch }` produced by |
| 800 | `RpcServer.toHttpEffect(InnerRpcs)`. State lives in |
| 801 | `state.storage` instead of R2: |
| 802 | |
| 803 | ```typescript |
| 804 | // src/object.ts |
| 805 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 806 | import * as Effect from "effect/Effect"; |
| 807 | import * as Layer from "effect/Layer"; |
| 808 | import * as Stream from "effect/Stream"; |
| 809 | import * as RpcSerialization from "effect/rpc/RpcSerialization"; |
| 810 | import * as RpcServer from "effect/rpc/RpcServer"; |
| 811 | import { InnerRpcs } from "./rpcs.ts"; |
| 812 | import { Task, TaskNotFound } from "./task.ts"; |
| 813 | |
| 814 | export default class TasksObject extends Cloudflare.DurableObject<TasksObject>()( |
| 815 | "TasksObject", |
| 816 | Effect.gen(function* () { |
| 817 | const state = yield* Cloudflare.DurableObjectState; |
| 818 | return Effect.gen(function* () { |
| 819 | const handlersLayer = InnerRpcs.toLayer({ |
| 820 | getTask: ({ id }) => |
| 821 | state.storage.get<Task>(id).pipe( |
| 822 | Effect.flatMap((task) => |
| 823 | task ? Effect.succeed(task) : Effect.fail(new TaskNotFound({ id })), |
| 824 | ), |
| 825 | ), |
| 826 | createTask: ({ title }) => |
| 827 | Effect.sync(() => new Task({ id: crypto.randomUUID(), title, completed: false })) |
| 828 | .pipe(Effect.tap((task) => state.storage.put(task.id, task))), |
| 829 | countTasks: ({ upto }) => |
| 830 | Stream.fromIterable(Array.from({ length: upto }, (_, i) => i + 1)), |
| 831 | }); |
| 832 | |
| 833 | return { |
| 834 | fetch: RpcServer.toHttpEffect(InnerRpcs).pipe( |
| 835 | Effect.provide(Layer.mergeAll(handlersLayer, RpcSerialization.layerNdjson)), |
| 836 | ), |
| 837 | }; |
| 838 | }); |
| 839 | }), |
| 840 | ) {} |
| 841 | ``` |
| 842 | |
| 843 | ### Bridge the DO into the Worker |
| 844 | |
| 845 | `Cloudflare.toHttpClient(stub)` produces an `HttpClient` whose |
| 846 | `execute` calls into the DO's `fetch`. Plug it into |
| 847 | `RpcClient.layerProtocolHttp` to get a typed `RpcClient<InnerRpcs>` |
| 848 | — the group the DO actually serves — then the `*DO` handlers just |
| 849 | forward the typed call. Here's the |
| 850 | whole Worker — the R2-backed handlers are unchanged, the new |
| 851 | `makeDOClient` and `getTaskDO` / `createTaskDO` proxies are added: |
| 852 | |
| 853 | ```typescript |
| 854 | // src/worker.ts |
| 855 | export default Cloudflare.Worker( |
| 856 | "Worker", |
| 857 | { main: import.meta.url }, |
| 858 | Effect.gen(function* () { |
| 859 | const tasks = yield* Cloudflare.R2.ReadWriteBucket(Tasks); |
| 860 | const tasksDO = yield* TasksObject; |
| 861 | |
| 862 | // A per-request typed RpcClient that short-circuits to the DO's |
| 863 | // `fetch`. The `url` is a placeholder — the request never hits the |
| 864 | // network; it routes straight to `tasksDO.getByName(id).fetch`. |
| 865 | const makeDOClient = (id: string = "default") => |
| 866 | RpcClient.make(InnerRpcs).pipe( |
| 867 | Effect.provide( |
| 868 | RpcClient.layerProtocolHttp({ url: "http://localhost" }).pipe( |
| 869 | Layer.provide( |
| 870 | Layer.succeed( |
| 871 | HttpClient.HttpClient, |
| 872 | Cloudflare.toHttpClient(tasksDO.getByName(id)), |
| 873 | ), |
| 874 | ), |
| 875 | Layer.provide(RpcSerialization.layerNdjson), |
| 876 | ), |
| 877 | ), |
| 878 | ); |
| 879 | |
| 880 | const handlersLayer = TaskRpcs.toLayer({ |
| 881 | // R2-backed handlers — unchanged |
| 882 | getTask: /* ... R2 implementation ... */, |
| 883 | createTask: /* ... R2 implementation ... */, |
| 884 | // DO-backed proxies — forward the typed call to the DO |
| 885 | getTaskDO: (payload) => |
| 886 | makeDOClient().pipe(Effect.flatMap((client) => client.getTask(payload))), |
| 887 | createTaskDO: (payload) => |
| 888 | makeDOClient().pipe(Effect.flatMap((client) => client.createTask(payload))), |
| 889 | }); |
| 890 | |
| 891 | return { |
| 892 | fetch: RpcServer.toHttpEffect(TaskRpcs).pipe( |
| 893 | Effect.provide( |
| 894 | Layer.mergeAll(handlersLayer, RpcSerialization.layerNdjson), |
| 895 | ), |
| 896 | ), |
| 897 | }; |
| 898 | }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)), |
| 899 | ); |
| 900 | ``` |
| 901 | |
| 902 | For streaming RPCs the same proxy pattern works with `Stream.unwrap` — |
| 903 | declare a streaming `countTasksDO` in `DoRpcs` (same shape as |
| 904 | `countTasks`, with `stream: true`) and forward: |
| 905 | |
| 906 | ```typescript |
| 907 | countTasksDO: (payload) => |
| 908 | Stream.unwrap( |
| 909 | makeDOClient().pipe(Effect.map((client) => client.countTasks(payload))), |
| 910 | ), |
| 911 | ``` |
| 912 | |
| 913 | `getTaskDO` / `createTaskDO` now hit the DO; `getTask` / `createTask` |
| 914 | still hit R2. One `TaskRpcs` value, one client, two storage backends. |
| 915 | |
| 916 | ## Choosing a modality |
| 917 | |
| 918 | - **Internal calls** (Worker-to-Worker, Worker-to-DO) — |
| 919 | [Schemaless RPC](/apis/schemaless): typed clients with no schema |
| 920 | and no per-request validation cost. |
| 921 | - **External Effect/TypeScript consumers** — this page. |
| 922 | - **External plain-HTTP consumers** — |
| 923 | [Effect HTTP](/cloudflare/apis/effect-http-api): the same typed |
| 924 | interface over real REST endpoints. |
| 925 | |
| 926 | The full decision guide lives at [RPC](/apis). |