Skip to main content

Module durable_object

Module durable_object 

Source
Expand description

Durable Object adapter for shards on Cloudflare Workers.

The idea: one DO class per shard type. Each match/zone/document gets its own DO instance (addressed by name). The DO owns the shard’s state, the tick loop (via setAlarm), and all WebSocket connections.

Why DOs specifically: they give you exactly the isolation + persistence semantics that Workers lack otherwise — single-threaded execution per instance, on-instance storage, WebSocket hibernation (clients stay connected even while the DO is idle and not billed).

§Status

This is a scaffold, not a working DO. The real worker crate has a different macro story and tight coupling to wasm-bindgen. A full impl requires:

  • A concrete #[durable_object]-annotated struct (in the user’s Workers bundle, not here).
  • A bridge from incoming WS messages → shard input queue.
  • A bridge from shard snapshots → DO WebSocket send.
  • state.setAlarm() for scheduled ticks in event-driven mode.
  • Storage.get()/put() for persistence across hibernation.

The abstractions below give users a head-start by providing the right trait shapes for their DO implementation to hook into.

Structs§

DoSubscriberHandle

Constants§

DURABLE_OBJECT_TEMPLATE_JS
The boilerplate a user adds to their Workers bundle’s JS entry file.

Traits§

DoStorage
Abstraction over DO storage.put()/get() so the scaffold can express the persistence pattern without depending on the worker crate.

Functions§

do_websocket_sink
Build a SnapshotSink that forwards snapshots to a DO’s WebSocket send.
persist_to_do_storage
Save a shard’s serialized state to DO storage.
register_do_subscriber
Registers a WebSocket-connected player with a shard running inside a DO.
restore_from_do_storage
Restore a shard’s serialized state from DO storage.