req.env using the same type-safe pattern as every other binding, so forwarding a request to a specific DO instance is just a few lines of code.
Configure the binding
1
Declare the Durable Object in wrangler.toml
wrangler.toml
2
Define the Env type
src/types/env.ts
3
Pass Env to createApp
src/index.ts
The Durable Object class (Then re-export it from your entry point:
ChatRoom in the example above) must be defined
and exported separately in your Worker entry point. The class must extend
DurableObject and implement a fetch(request) method. Blaze handles the
HTTP routing layer; your DO class handles the stateful logic.src/durable-objects/chat-room.ts
src/index.ts
Getting a DO instance
Every Durable Object instance is identified by aDurableObjectId. Derive an ID in one of two ways:
Forwarding requests to a DO
The most common pattern is to forward the entire incoming request — including a WebSocket upgrade — directly to the Durable Object. The DO handles the upgrade and owns the persistent connection state.Named instances
idFromName() is the building block for stable, long-lived DO instances tied to application entities. Use a consistent naming scheme so every part of your application resolves to the same instance for a given entity.
Named Durable Objects created with
idFromName() are permanent — they are
not garbage-collected until you explicitly delete them via the Cloudflare API
or Wrangler. Design your naming scheme carefully; a new DO instance is
provisioned the first time an ID is accessed, and storage costs accrue from
that point forward.