Every realtime app has the same quiet bug. A phone goes through a tunnel, a laptop sleeps, a Wi-Fi network hiccups for twenty seconds. The WebSocket reconnects, the "connected" dot turns green again, and everything looks fine.
Except whatever was published during those twenty seconds is gone. The chat is missing messages, the dashboard shows an old number, and the game state is out of step with everyone else's. Nobody gets an error. The app is wrong and doesn't know it.
We built SwengleIO, a hosted realtime messaging service (pub/sub over WebSockets, with presence), around one rule: an app should never be left wrong without knowing it. Here's how that works, and the trade-offs we made.
Where messages actually get lost
There are three common ways:
- The disconnect gap. Most pub/sub services are fire-and-forget: a message goes to whoever is connected right now. A client that was offline for a moment just misses it.
- The crash after the "OK". A server acknowledges a publish, then dies before the message is stored or sent anywhere. The sender thinks it was delivered; nobody ever gets it.
- The retry duplicate. The sender's connection drops before the acknowledgement arrives, so it doesn't know if the publish went through. It retries, and now there are two.
Fixing the first one alone isn't enough. If you replay missed messages but can lose acknowledged ones, or duplicate retried ones, the app is still quietly wrong.
Store first, then deliver
Every message is committed to our database before it's delivered or acknowledged. If the server dies halfway, either the message was stored (and will be delivered) or it wasn't (and the sender gets an error, not an "OK").
Each stored message gets an offset: a number that only goes up within its channel. Offsets are what make everything else possible.
Resume from where you left off
The client libraries remember the last offset they received on each channel. When the connection comes back, they don't just resubscribe and hope. They ask for "everything on room:42 after offset 1841", get the missed messages in order, and then carry on with live ones.
Your code doesn't see any of this. It just receives messages in order, as if the connection never dropped.
When the gap is too big: say so
Messages are kept for a retention window (a day on the free tier, up to seven days on your own host). A client that has been gone longer than that can't be caught up.
The tempting thing is to deliver whatever is still there and carry on. That's the worst option: the app ends up with a history that has a hole in it and no idea the hole exists.
So instead the client gets an explicit resync: "messages may have been missed: reload your state". The app fetches fresh state from your backend and starts over from the latest messages. It's never a partial history.
Retries that don't duplicate
Delivery is at least once. A replayed message keeps its original offset, so a client that sees the same offset twice can skip it.
On the publishing side, each publish carries an idempotency key (the library generates one if you don't). If a retry arrives with a key that was already stored, the server answers with the original result instead of storing the message again.
What it looks like in code
Here's a TypeScript service that follows a channel and publishes to it. The library handles the reconnects, the replay and the duplicate-free retries:
import { connect, Credential, json, latest } from "@swengle/notifiers"; const n = await connect( process.env.NOTIFIERS_URL!, Credential.secretKey(process.env.NOTIFIERS_SECRET_KEY!), { onConnect: () => console.log("connected again; presence tracked again") }, ); const sub = await n.subscribe( "room:42", latest(), { message: (m) => console.log(`#${m.offset} ${m.payload.json ?? "(bytes)"}`), broadcast: (m) => console.log(`broadcast ${m.payload.json}`), presence: (p) => console.log(p.snapshot ? `members: ${p.members.length}` : `joined ${p.joined.length}, left ${p.left.length}`), }, { onResync: { start: latest(), fn: () => console.log("messages may have been missed: reload state") } }, ); await n.publish("room:42", json({ text: "hello" })); onResync is the important line: it's the one place your app hears "you missed something you can't get back", so it can reload instead of being silently wrong.
Browsers and mobile apps don't hold the secret key; your server mints each user a short-lived token for the channels they're allowed on, and the library asks for a fresh one before it expires. There are libraries for TypeScript, Python, Go, Java, Kotlin, C#, Swift, Rust, PHP and Ruby.
Not everything needs to be stored. Typing indicators and cursor positions go out as live-only broadcasts: not stored, not replayed and not billed.
The trade-offs
- A little latency. Storing before delivering costs a few milliseconds per message compared with fire-and-forget. For chat, notifications and game state, that's worth it; for 60 fps cursor streams, use broadcasts.
- At least once, not exactly once. Exactly-once delivery over an unreliable network isn't really achievable end to end. Offsets make duplicates trivial to drop instead.
- Retention is a window, not forever. Past it you get a resync, deliberately, rather than a partial history.
Staying up when things break
Most of our engineering time went into failure. A Dedicated or Cluster endpoint can run across two availability zones: if a zone goes down, the standby database and the hosts in the other zone take over, clients pause for 15 to 25 seconds, then reconnect and resume where they left off. With backups on, they're also copied, encrypted, to another region and off the cloud provider.
Try it
SwengleIO runs in 11 regions on AWS, Google Cloud and Oracle Cloud. The Shared plan has 1,000,000 messages free, then $0.99 per million. If you send a lot, a Dedicated host starts at about 5 cents an hour with no per-message charge, billed by the minute, so you can even run one just for a launch or a live event.
We'd love to hear from anyone who's been bitten by missed messages, or who thinks resync is the wrong trade-off. — Brian O'Connor
