Edge Real-time (Durable Objects) — Realtime

A Durable Object is one uniquely named Cloudflare object with its own storage, living in one place, and single-threaded (it does one thing at a time). The…

Eko's tablet in London and a customer's phone in Manchester open the same board. On Cloudflare (a company with servers in many cities), both can connect to one object. So Eko's news reaches that phone with no Redis and no sticky sessions. That object is called a Durable Object.

The edge means your code runs in many data centres (buildings full of servers) around the world, close to users, instead of on one server. A Durable Object is one Cloudflare object with a unique name. It lives in one place, has its own storage (a place to keep data), and runs one request at a time. Every request that names it, from anywhere, reaches the same object.

Picture the order board at Mrs. Rini's Laundry, kept by one record keeper. Anyone who wants to read or change a status, from London or Manchester, talks to that record keeper. Because only one person holds the board, no two writes can overwrite each other. In this picture, the record keeper is the Durable Object and the board is the data in its storage. The people who talk to the record keeper are the screens connected over WebSocket.

One name, one object

The code has two parts: a Worker (a function that takes requests in the data centre nearest the user) and a Durable Object class. The class is called Board, and the Worker is the export default at the bottom:

import { DurableObject } from 'cloudflare:workers'

export class Board extends DurableObject {
  async fetch(request) {
    const user = new URL(request.url).searchParams.get('user') ?? 'guest'
    const [client, server] = Object.values(new WebSocketPair())
    this.ctx.acceptWebSocket(server)     // Hibernation API: the runtime holds the connection
    server.serializeAttachment({ user })  // kept even while the object sleeps
    const orders = this.ctx.storage.sql.exec('SELECT * FROM orders ORDER BY id').toArray()
    server.send(JSON.stringify({ type: 'snapshot', orders }))
    return new Response(null, { status: 101, webSocket: client })
  }

  async webSocketMessage(ws, message) {
    const { user } = ws.deserializeAttachment()
    const m = JSON.parse(message)
    this.ctx.storage.sql.exec('UPDATE orders SET status = ? WHERE id = ?', m.status, m.id)
    const news = JSON.stringify({ type: 'status', id: m.id, status: m.status, by: user })
    for (const s of this.ctx.getWebSockets()) s.send(news) // every socket of this board is here
  }

  async webSocketClose(ws, code, reason) {
    ws.close(code, reason)
  }
}

export default {
  async fetch(request, env) {
    if (new URL(request.url).pathname === '/board' && request.headers.get('Upgrade') === 'websocket') {
      return env.BOARD.getByName('rini-laundry').fetch(request) // same name, same object
    }
    return new Response('Not found', { status: 404 })
  },
}

(The constructor, which runs when the object is created, is not shown above. It creates the orders table and fills in #12 to #15.) This code ran locally with wrangler dev (wrangler 4.149.0, Cloudflare's command-line program), with no deploy and no login. Two ws clients, Eko and Mrs. Rini, sent (->) and received (<-):

rini <- {"type":"snapshot","orders":[{"id":12,"name":"Mr. Darto","kg":3,"status":"Washing"}, ...]}
eko  -> {"type":"status","id":12,"status":"Ready for pickup"}   (53 bytes)
eko  <- {"type":"status","id":12,"status":"Ready for pickup","by":"eko"}
rini <- {"type":"status","id":12,"status":"Ready for pickup","by":"eko"}

After restarting wrangler dev, the next snapshot already had #12 at Ready for pickup. So the object's SQLite storage was kept. GET /board without the Upgrade header gets 404.

So Eko's tablet and the phone in Manchester, both connecting through env.BOARD.getByName('rini-laundry'), meet in one object, and no Redis is needed. Every WebSocket of this board is connected to that object, so broadcasting takes one loop over this.ctx.getWebSockets(). Compare the Pub/Sub and Scaling WebSocket lessons: two Node processes need sticky sessions and the Redis adapter.

Hibernation: the object sleeps, the connections stay

this.ctx.acceptWebSocket(server) uses the WebSocket Hibernation API, which lets the object sleep (hibernate) when it is quiet. Cloudflare's runtime (the program that runs your code) holds the connection, not the object. The runtime calls webSocketMessage, webSocketClose or webSocketError when something happens. According to Cloudflare's docs:

One thread per object

Each object is single-threaded: it has only one thread (one line of work), so one event is processed to the end before the next. So two tablets changing #12 at the same moment cannot overwrite each other halfway, even without locks (guards for shared data). In return, one object has a limit. The docs give a soft limit of 1,000 requests per second per object: not an exact cutoff, but an object that receives too many requests answers with an overloaded error. Every incoming WebSocket message is processed by that same thread too.

So decide what one object holds, by its load:

An object is created close to where it is first requested, and is not copied to other regions (areas of the world). For a laundry in London, its board lives near London, close to all its users. That is its latency gain over a realtime server in America, where all news crosses the Atlantic. A user in Manchester comes in through a Worker in their nearest data centre, then is passed over Cloudflare's network to the same object.

Limits and prices

From Cloudflare's docs, checked on 11 October 2026 (the pricing page was updated on 30 September 2026):

Workers Free Workers Paid
Requests 100,000 per day 1 million per month, then $0.15 per million
Duration 13,000 GB-seconds per day 400,000 GB-seconds per month, then $12.50 per million GB-seconds
Storage SQLite only SQLite, 10 GB per object

Other options

⚠️ Objects that forget, never sleep, or are overloaded

TL;DR: A Durable Object is one uniquely named Cloudflare object with its own storage, living in one place, and single-threaded (it does one thing at a time). The Worker at the edge (the data centre nearest the user) passes requests to env.BOARD.getByName('rini-laundry'), so every WebSocket of Mrs. Rini's board gathers in one object. Broadcasting is one loop over this.ctx.getWebSockets(), with no Redis and no sticky sessions. With the Hibernation API (acceptWebSocket, webSocketMessage, webSocketClose), the object sleeps after 10 quiet seconds without dropping connections and without duration charges (charges for time in memory). But when it wakes, its constructor (the function that creates the object) runs again, so keep state in storage or serializeAttachment (data per connection). Its limit is about 1,000 requests per second per object. Incoming messages count 20 to 1 (20 messages = 1 request), outgoing ones are free. The alternatives are PartyKit (partyserver), managed services such as Ably and Pusher, or your own server.

⚡ One name, one object

Write:

return env.BOARD.getByName('rini-laundry').fetch(request) // same name, same object

Meaning: A Durable Object is a uniquely named Cloudflare object that lives in one place. This line is in the Worker (the code that takes requests) and passes the request to the object rini-laundry, Mrs. Rini's board. Every WebSocket of that board is connected to the same object, so broadcasting is one loop over this.ctx.getWebSockets(). No Redis, no sticky sessions.

⚡ Close to the users

Meaning: The edge means code runs in many data centres (buildings full of servers), close to users. A Durable Object is created close to where it is first requested, so a laundry board in London lives near London. Its latency (the delay before news appears) is lower than from a server in America.

⚡ The object sleeps, the connections stay

Guess first: after 10 quiet seconds the object hibernates: it sleeps and is removed from memory, but its WebSocket connections stay connected. When the next message comes, does a memory variable such as this.count still hold its value?

No. When the object wakes, its constructor (the function that creates the object) runs again, so memory variables start over. Its WebSocket connections stay connected.

Meaning: Keep what matters in this.ctx.storage (it stays even while the object sleeps), or per connection with serializeAttachment (a small piece of data attached to one connection).

What you will learn