Offline Chat with Cloudflare Durable Objects

Chat feels instant when both people are online. The interesting part happens when one of them isn’t.

Alice sends Bob a message. Bob’s iPhone is offline, so there is no live connection to receive it. A useful chat app needs somewhere to hold that message and a way for Bob’s device to catch up later.

Cloudflare Durable Objects offer a remarkably straightforward way to think about this: give each user a mailbox. Messages addressed to Bob go into Bob’s mailbox; messages addressed to Alice go into Alice’s. Devices connect to their owner’s mailbox to synchronize.

This post explores that per-user mailbox as a conceptual architecture, not a description of a shipped multi-device feature. JustCall’s current iOS mailbox is keyed by recipient device, not user. We’ll come back to that distinction.

Alice, Bob, and two mailboxes

Imagine that Alice and Bob each have an iPhone and a corresponding Durable Object:

  • Alice’s mailbox holds messages sent to Alice.
  • Bob’s mailbox holds messages sent to Bob.
  • A Cloudflare Worker authorizes requests and routes them to the right mailbox.
  • Each mailbox uses durable storage for its pending messages and can serve a connection to its owner’s device.

Conceptual architecture: Alice and Bob each have an iPhone and a separate mailbox Durable Object with a list of received messages.

The arrows carry two different kinds of traffic. Sending targets the recipient’s mailbox. Syncing reads from your own mailbox. Alice doesn’t need Bob’s phone to be online when she sends; she needs Bob’s mailbox to accept the message.

What happens when Bob is offline?

Let’s follow one message from Alice to Bob:

  1. Alice sends. Her device encrypts a message for Bob and sends it to the Worker. The Worker checks that Alice is allowed to send and routes the message to Bob’s mailbox.
  2. Bob’s mailbox stores it. The Durable Object writes the encrypted message and the metadata needed for delivery to its SQLite storage. Bob’s phone does not need to be connected.
  3. Bob reconnects. His device establishes an authorized connection and asks for pending messages. The mailbox sends the backlog, in sequence, along with new messages as they arrive.
  4. Bob’s device saves and acknowledges. After saving a message locally, the device acknowledges it so the mailbox can apply its retention policy.

The same flow works in reverse when Bob sends to Alice. A push notification can help wake a device, but it is a hint to reconnect, not the message store or a guarantee of delivery. The mailbox remains the place to catch up when a notification is delayed or missed.

Why Durable Objects fit the mailbox model

A Durable Object gives an application a named place to coordinate work and keep state. In this design, the name comes from the user’s ID:

// Illustrative per-user design, after validating and authorizing the request.
const recipientMailbox = env.MAILBOX.getByName(recipientUserId);

Requests for the same user ID address the same logical mailbox. Alice and Bob get separate objects and separate storage; a mailbox isn’t a shared table that every user’s device reads. A name selects an object, but knowing that name must never be treated as permission to read it.

The building blocks are a good match for chat:

  • A natural unit of state. The mailbox owns a user’s incoming queue and can coordinate writes, reads, and connected devices.
  • Durable storage. SQLite-backed Durable Objects can persist messages in storage private to that object. The in-memory object can be evicted; the message queue is not merely an in-memory array.
  • Real-time connections. A WebSocket can deliver new messages while a device is online and replay pending messages when it reconnects.
  • Idle connections without an always-active object. Cloudflare’s WebSocket hibernation API can keep clients connected while an idle object is evicted from memory. The object resumes when an event arrives.

These are platform primitives, not an automatic chat protocol. The app still has to define message IDs, authorization, retries, synchronization, retention, and what happens when things go wrong. Cloudflare’s documentation has more on Durable Object namespaces, SQLite storage, and WebSocket hibernation.

A mailbox is not the conversation itself

It helps to separate three things that are easy to blur together:

  • A mailbox is the server-side queue of messages addressed to one recipient.
  • A device connects, saves messages locally, and displays the conversation.
  • A live call uses a separate real-time media path. An offline mailbox doesn’t replace the WebRTC connection used for a call.

This distinction also explains why Alice’s outbound message belongs in Bob’s mailbox, not Alice’s. Alice’s app may keep its own local copy for the conversation view, but Bob’s mailbox is responsible for making the message available to Bob.

Privacy needs more than a mailbox

Durable storage is not the same thing as end-to-end encryption. For an encrypted design, Alice’s device encrypts the message for Bob before sending it, and Bob’s device decrypts it after receiving it. The mailbox stores and forwards ciphertext rather than needing the plaintext.

The server still needs some routing and delivery metadata, such as a sender identifier, a message ID, and a sequence number. Authorization matters too: a user ID alone is not a secret, and a client must prove it is entitled to read the mailbox. Sending also needs authorization and abuse controls.

For a shared per-user mailbox, encryption introduces another question: how do all of a user’s authorized devices obtain the keys needed to read the message? Device linking, key distribution, and revocation need their own design. A Durable Object does not solve those problems on its own.

The tricky part: syncing more than one device

One mailbox per user is appealing because the mailbox has a stable home even as devices change. But a shared mailbox does not automatically make multi-device sync correct.

Suppose Bob reads a message on his iPhone before his laptop reconnects. If the server deletes that message as soon as the iPhone acknowledges it, the laptop may never see it. A real design needs to decide:

  • Cursors: Does each authorized device track its own position in the mailbox?
  • Acknowledgements: Does an acknowledgement mean one device saved a message, or that every relevant device has received it?
  • Retention: When can a message be removed? What happens if a device stays offline longer than the retention window?
  • Retries: How do clients deduplicate a message if a connection drops after it was stored but before the acknowledgement arrives?
  • Device changes: What should a newly linked or revoked device be able to read?

Stable message IDs, ordered sequence numbers, idempotent writes, and saving locally before acknowledging are useful ingredients. They are not a substitute for choosing explicit delivery and retention semantics. Storage limits and recovery from missed history need attention as well.

What’s in JustCall today?

The current iOS implementation uses a Mailbox Durable Object per recipient device, addressed by deviceId. It stores end-to-end encrypted message bodies and delivery metadata in SQLite, replays unacknowledged messages when the owner reconnects, and uses signed challenge-response to authorize mailbox reads. Acknowledged messages are cleaned up, and the queue is bounded: the oldest messages can be dropped if the limit is reached.

That makes it a useful starting point for offline delivery to a device. The per-user mailbox in the diagram is a possible evolution, not something the existing device-keyed implementation already provides. In particular, shared multi-device cursors, key distribution, and retention rules are still design work.

The takeaway

Durable Objects make a compelling home for chat mailboxes because the code that coordinates a recipient’s messages can live alongside that mailbox’s durable storage and live connections.

The mental model is simple: send to the recipient’s mailbox, then let their device sync when it can. The careful work comes next: deciding who may read, how messages are encrypted, how retries and acknowledgements behave, and how long each device has to catch up.

Related reading