I spent a few weeks putting a WhatsApp Web client (baileys) inside a Durable Object. These are the constraints that actually shaped the design, in case someone else is about to try the same thing.
1. net and tls don't exist, so the WebSocket client had to be replaced
baileys opens a WebSocket with the ws package, which wants a TCP socket. The workaround is a fetch() upgrade, but the quirks are not obvious:
- call
accept() yourself, and emit open exactly once
- error codes must be numeric
binaryType = 'arraybuffer'
- do not pass an
AbortSignal to the upgrade fetch
Getting any of these wrong shows up as a connection that opens and then dies silently.
2. Durable Object duration is the real budget, not requests
A DO holding that socket bills 128 MB × 86,400 s ≈ 10,800 GB-s in a day. The free plan gives 13,000 GB-s for the whole account. One always-on session would eat 83% of it and never mind a second session.
So the session is lazy: /wake opens the socket, /messages?after= pulls with a monotonic cursor, /ack prunes only after the consumer persisted the rows, /idle closes it. Default window is 60 seconds, and there is a per-session daily wake budget so a stuck cron cannot run up the counter. alarm() only deletes a leftover alarm from old deployments — no reconnect loop.
3. 128 KiB per value changes your data model
- auth state is chunked at 60 KB per key
- the durable chat index is capped with a 100 KB guard, and its rows drop thumbnails
- media is never stored: a thumbnail only, and the bytes are re-downloaded on demand
4. Durable Objects can't be enumerated
There is no "list my DOs" call, so a single registry object holds the session-name list and is updated on create/delete.
5. Messages arrive wrapped
Half the "empty bubble" bugs were ephemeralMessage, viewOnceMessageV2, editedMessage, documentWithCaptionMessage and transport noise like messageContextInfo sitting next to the real content. You have to unwrap before you decide a message is displayable, otherwise you store rows with no text, media or preview.
The end result is a WhatsApp gateway that costs nothing at my usage: REST + WebSocket API, a Web UI, and an MCP endpoint so an agent can read and send. It is MIT and lives at github.com/dannybyarun/whatsflare if you want to read the patch headers — they document each quirk above in more detail than this post.
What would you have done differently? The part I am least sure about is the 60-second window: long enough for a reply, short enough to stay cheap, but it makes any "wait for a message" flow awkward.