$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-durable-objectsComprehensive guide for Cloudflare Durable Objects - globally unique, stateful objects for coordination, real-time communication, and persistent state management. Use when: building real-time applications, creating WebSocket servers with hibernation, implementing chat rooms or mu
| 1 | # Cloudflare Durable Objects |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-22 |
| 5 | **Dependencies**: cloudflare-worker-base (recommended) |
| 6 | **Latest Versions**: wrangler@4.43.0+, @cloudflare/workers-types@4.20251014.0+ |
| 7 | **Official Docs**: https://developers.cloudflare.com/durable-objects/ |
| 8 | |
| 9 | --- |
| 10 | |
| 11 | ## What are Durable Objects? |
| 12 | |
| 13 | Cloudflare Durable Objects are **globally unique, stateful objects** that provide: |
| 14 | |
| 15 | - **Single-point coordination** - Each Durable Object instance is globally unique across Cloudflare's network |
| 16 | - **Strong consistency** - Transactional, serializable storage (ACID guarantees) |
| 17 | - **Real-time communication** - WebSocket Hibernation API for thousands of connections per instance |
| 18 | - **Persistent state** - Built-in SQLite database (up to 1GB) or key-value storage |
| 19 | - **Scheduled tasks** - Alarms API for future task execution |
| 20 | - **Global distribution** - Automatically routed to optimal location |
| 21 | - **Automatic scaling** - Millions of independent instances |
| 22 | |
| 23 | **Use Cases**: |
| 24 | - Chat rooms and real-time collaboration |
| 25 | - Multiplayer game servers |
| 26 | - Rate limiting and session management |
| 27 | - Leader election and coordination |
| 28 | - WebSocket servers with hibernation |
| 29 | - Stateful workflows and queues |
| 30 | - Per-user or per-room logic |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## Quick Start (10 Minutes) |
| 35 | |
| 36 | ### Option 1: Scaffold New DO Project |
| 37 | |
| 38 | ```bash |
| 39 | npm create cloudflare@latest my-durable-app -- \ |
| 40 | --template=cloudflare/durable-objects-template \ |
| 41 | --ts \ |
| 42 | --git \ |
| 43 | --deploy false |
| 44 | |
| 45 | cd my-durable-app |
| 46 | npm install |
| 47 | npm run dev |
| 48 | ``` |
| 49 | |
| 50 | **What this creates:** |
| 51 | - Complete Durable Objects project structure |
| 52 | - TypeScript configuration |
| 53 | - wrangler.jsonc with bindings and migrations |
| 54 | - Example DO class implementation |
| 55 | - Worker to call the DO |
| 56 | |
| 57 | ### Option 2: Add to Existing Worker |
| 58 | |
| 59 | ```bash |
| 60 | cd my-existing-worker |
| 61 | npm install -D @cloudflare/workers-types |
| 62 | ``` |
| 63 | |
| 64 | **Create a Durable Object class** (`src/counter.ts`): |
| 65 | |
| 66 | ```typescript |
| 67 | import { DurableObject } from 'cloudflare:workers'; |
| 68 | |
| 69 | export class Counter extends DurableObject { |
| 70 | async increment(): Promise<number> { |
| 71 | // Get current value from storage (default to 0) |
| 72 | let value: number = (await this.ctx.storage.get('value')) || 0; |
| 73 | |
| 74 | // Increment |
| 75 | value += 1; |
| 76 | |
| 77 | // Save back to storage |
| 78 | await this.ctx.storage.put('value', value); |
| 79 | |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | async get(): Promise<number> { |
| 84 | return (await this.ctx.storage.get('value')) || 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // CRITICAL: Export the class |
| 89 | export default Counter; |
| 90 | ``` |
| 91 | |
| 92 | **Configure wrangler.jsonc:** |
| 93 | |
| 94 | ```jsonc |
| 95 | { |
| 96 | "$schema": "node_modules/wrangler/config-schema.json", |
| 97 | "name": "my-worker", |
| 98 | "main": "src/index.ts", |
| 99 | "compatibility_date": "2025-10-22", |
| 100 | |
| 101 | // Durable Objects binding |
| 102 | "durable_objects": { |
| 103 | "bindings": [ |
| 104 | { |
| 105 | "name": "COUNTER", // How you access it: env.COUNTER |
| 106 | "class_name": "Counter" // MUST match exported class name |
| 107 | } |
| 108 | ] |
| 109 | }, |
| 110 | |
| 111 | // REQUIRED: Migration for new DO class |
| 112 | "migrations": [ |
| 113 | { |
| 114 | "tag": "v1", // Unique migration identifier |
| 115 | "new_sqlite_classes": [ // Use SQLite backend (recommended) |
| 116 | "Counter" |
| 117 | ] |
| 118 | } |
| 119 | ] |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | **Call from Worker** (`src/index.ts`): |
| 124 | |
| 125 | ```typescript |
| 126 | import { Counter } from ' |