Use Queues from Durable Objects
Publish to a queue from within a Durable Object.
The following example shows you how to write a Worker script to publish to Cloudflare Queues from within a Durable Object.
Prerequisites:
- A queue created via the Cloudflare dashboard or the wrangler CLI.
- A configured producer binding in the Cloudflare dashboard or
wrangler.tomlfile. - A Durable Object namespace binding.
Configure your wrangler.toml file as follows:
The following Worker script:
- Creates a Durable Object stub, or retrieves an existing one based on a userId.
- Passes request data to the Durable Object.
- Publishes to a queue from within the Durable Object.
The constructor() in the Durable Object makes your Environment available (in scope) on this.env to the fetch() handler in the Durable Object.