Durable Objects
The Cloudflare Durable Objects platform leverages Cloudflare Workers to run ActorCore.
ActorCore is still pre-v1.0. Please help us by report bugs on GitHub Issues!
Create New Project
Create Project with CLI
Run this command:
Follow the prompts:
- Where would you like to create your project? - Choose your project directory
- To which platform would you like to deploy? - Select Cloudflare Workers
- Which template would you like to use? - Select counter, or your template of choice
The CLI will set up your project and install all dependencies automatically.
Start Development Server
Start your development server with:
This will start your ActorCore server in development mode with Wrangler.
Test
In a separate terminal, run the auto-generated test client:
Run this again to see the state update.
Deploy
-
Create a new KV namespace with:
-
After creating the KV namespace, you will receive an ID. Update your
wrangler.json
file by replacing the placeholder in thekv_namespaces
section with this ID. It should look like this: -
Deploy your project to Cloudflare Workers by running:
-
Update
tests/client.ts
(or wherever your client lives) to use the deployed endpoint. Replace the local endpoint inclient.ts
with your Cloudflare Workers URL:Ensure you replace
your-worker-subdomain
with the actual subdomain assigned to your worker.
Integrating With Existing Projects
If you already have a Cloudflare Workers project and want to add ActorCore, you can follow these steps for manual integration. This approach gives you more control over how ActorCore fits into your existing Cloudflare Workers project.
Install Packages
Create Actor
Create a new file for your actor at src/counter.ts
:
Configure Cloudflare
Update both src/index.ts
and wrangler.json
to look like this:
In src/index.ts
, handler
is used to handle HTTP requests made to the worker. ActorHandler
is a Durable Object used to provide state for your actors.
If you already have an existing application and want to mount ActorCore on a subpath, see our Hono integration guide. Remember to specify the same path in config.basePath
as where you mount the router.
Create Client & Test
To start your development server, run:
Now, write a file named client.ts
with this:
Run the file in a second terminal with:
Run this again to see the state update.
Deploy
-
Create a new KV namespace with:
-
After creating the KV namespace, you will receive an ID. Update your
wrangler.json
file by replacing the placeholder in thekv_namespaces
section with this ID. It should look like this: -
Deploy your project to Cloudflare Workers by running:
-
Update
tests/client.ts
(or wherever your client lives) to use the deployed endpoint. Replace the local endpoint inclient.ts
with your Cloudflare Workers URL:Ensure you replace
your-worker-subdomain
with the actual subdomain assigned to your worker.
Accessing Cloudflare Context And Bindings
Cloudflare-specific DurableObjectState
and environment bindings can be accessed from createVars
.
Available Regions
See available regions here.
Cloudflare does not guarantee your code will run in the requested region.