Use KV from Durable Objects
Read and write to/from KV within a Durable Object
The following Worker script shows you how to configure a Durable Object to read from and/or write to a Workers KV namespace. This is useful when using a Durable Object to coordinate between multiple clients, and allows you to serialize writes to KV and/or broadcast a single read from KV to hundreds or thousands of clients connected to a single Durable Object using WebSockets.
Prerequisites:
- A KV namespace created via the Cloudflare dashboard or the wrangler CLI.
- A configured binding for the
kv_namespace
in the Cloudflare dashboard orwrangler.toml
file. - A Durable Object namespace binding.
Configure your wrangler.toml
file as follows: