Using local DO bindings from another worker script

I am trying to use DO bindings for local development but I am running into some issues. The docs (https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#local-development) seem to indicate that you can bind two local workers by calling wrangler dev on each of them. But that is giving me an issue. My DO worker is defined like this:
export class DurableObjectClassName extends DurableObject<Env> {
async sayHello(name: string): Promise<string> {
return `Hello, ${name}!`;
}
}
export class DurableObjectClassName extends DurableObject<Env> {
async sayHello(name: string): Promise<string> {
return `Hello, ${name}!`;
}
}
here is the wrangler.json:
{
"name": "durable-object-worker",
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["DurableObjectClassName"]
}
]
}
{
"name": "durable-object-worker",
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["DurableObjectClassName"]
}
]
}
Then I am trying to call the DO from another worker like so:
const DO = locals.runtime.env.DO;
const id = DO.idFromName('hello');
const stub = DO.get(id);
const res = await stub.sayHello('world');
const DO = locals.runtime.env.DO;
const id = DO.idFromName('hello');
const stub = DO.get(id);
const res = await stub.sayHello('world');
the wrangler.json is defined as:
{
"durable_objects": {
"bindings": [
{
"class_name": "DurableObjectClassName",
"name": "DO",
"script_name": "durable-object-worker"
}
]
},
}
{
"durable_objects": {
"bindings": [
{
"class_name": "DurableObjectClassName",
"name": "DO",
"script_name": "durable-object-worker"
}
]
},
}
I try to run wrangler dev on the DO worker and then wrangler dev on the main worker, however the main worker gives me this message:
Your worker has access to the following bindings:
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker [not connected]) (local)
Your worker has access to the following bindings:
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker [not connected]) (local)
Then when I try to access the DO, it is giving: DO access error: Error: Cannot access 'DurableObjectClassName#sayHello' as Durable Object RPC is not yet supported between multiple 'wrangler dev' sessions. The docs seem to indicate that it is supported. If I run wrangler dev -c wrangler.json -c do-worker/wrangler.json, the two workers are able to communicate and the binding works correctly. Once I start them separately, then I am running into the issue that they can't talk.
Cloudflare Docs
Service bindings - Runtime APIs · Cloudflare Workers docs
Facilitate Worker-to-Worker communication.
1 Reply
downeydabber
downeydabberOP3mo ago
Ok I was able to do a little digging and figured out the following: Once I added a durable_object binding to the DO worker, it is saying that they are connected when I run wrangler dev. The DO wrangler.json looks like this:
{
"name": "durable-object-worker",
"durable_objects": {
"bindings": [
{
"class_name": "DurableObjectClassName",
"name": "DO",
"script_name": "durable-object-worker"
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["DurableObjectClassName"]
}
]
}
{
"name": "durable-object-worker",
"durable_objects": {
"bindings": [
{
"class_name": "DurableObjectClassName",
"name": "DO",
"script_name": "durable-object-worker"
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["DurableObjectClassName"]
}
]
}
then the output of wrangler dev from the other worker is:
Your worker has access to the following bindings:
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker [connected]) (local)
Your worker has access to the following bindings:
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker [connected]) (local)
So now it says they are connected, but it is still giving the same error Error: Cannot access 'DurableObjectClassName#sayHello' as Durable Object RPC is not yet supported between multiple 'wrangler dev' sessions. When I use wrangler dev -c wrangler.json -c do-worker/wrangler.json, they are binding correctly and the output of wrangler dev is
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker) (local)
- Durable Objects:
- DO: DurableObjectClassName (defined in durable-object-worker) (local)
I guess the error makes sense that it can't use RPC between two wrangler dev sessions, but when it is run as a combined wrangler dev section, then the RPC starts to work. Maybe I will just have to use fetch to access the DO during development? It would be really nice to figure out how to use the RPC though, I don't want to have two sets of logic between local and remote.

Did you find this page helpful?