Create Durable Object stubs
A Durable Object stub is a client Object used to send requests to a remote Durable Object.
OBJECT_NAMESPACE.get(id)
creates a Durable Object.
Durable Objects implement E-order semantics. When you make multiple calls to the same Durable Object, it is guaranteed that the calls will be delivered to the remote Durable Object in the order in which you made them. E-order semantics makes many distributed programming problems easier.
However, due to random network disruptions or other transient issues, a Durable Object stub may become disconnected from its remote Durable Object. A disconnected stub is permanently broken. In this scenario, all in-flight calls and future calls will fail with exceptions.
To make new requests to the Durable Object, you must call OBJECT_NAMESPACE.get(id)
again to get a new Durable Object stub. There are no ordering guarantees between requests to the new stub compared to the old one. If ordering is not a concern, you can create a new Durable Object for every request.
1. Obtain a Durable Object stub
let durableObjectStub = OBJECT_NAMESPACE.get(id);
Parameters
id
DurableObjectId
An ID constructed using
newUniqueId()
,idFromName()
, oridFromString()
on this Durable Object namespace.This method constructs an Object, which is a local client that provides access to a remote Object.
If the remote Object does not already exist, it will be created. Thus, there will always be an Object accessible from the stub.
This method always returns the Object immediately, before it has connected to the remote Object. This allows you to begin making requests to the Object right away, without waiting for a network round trip.
2. Send HTTP requests
let response = await durableObjectStub.fetch(request);
// Alternatively, passing a URL directly:
let response = await durableObjectStub.fetch(url, options);
The url
passed to the fetch()
handler of your Durable Object must be a well-formed URL, but does not have to be a publicly-resolvable hostname. You can:
- Pass the client
Request
directly into thefetch()
handler as is. - Use an internal URL scheme, such as
http://do/some-path
, as theurl
parameter in thefetch()
handler. This allows you to construct your own path or query parameter based approach to sharing state between your client-facing Worker and your Durable Object. - Alternatively, you can construct your own
Request
object, which allows you to use aHeaders
object to pass in key-value pairs, representing data you wish to pass to your Durable Objects.
The example below shows you how to construct your own Request
object:
// Constructing a new Request and passing metadata to the Durable Object via headers
let doReq = new Request("http://do/write", { headers: { "user-id": userId }})
let resp = await durableObjectStub.fetch(doReq)
// Alternatively, using URL query params or paths
let resp = await durableObjectStub.fetch(`http://do/write?userId=${userId}`)
3. List Durable Objects
The Cloudflare REST API supports retrieving a list of Durable Objects within a Durable Object namespace and a list of namespaces associated with an account.