downeydabber
downeydabber
CDCloudflare Developers
Created by downeydabber on 2/1/2025 in #workers-help
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.
2 replies