Chat on a bad network
An offline queue is a product feature, not a retry wrapper. The MBS chat sync had to prove that.
School chat is not a nice-to-have. A parent sending a message to a teacher expects it to arrive, even when they walk out of Wi-Fi and onto a weak mobile connection.
The first version retried requests. That is not enough. Retries duplicate messages after a reconnect, they lose ordering, and they keep running after the user signs out.
The outbox
Messages are written to a local SQLite outbox first. A sync loop drains the outbox when the network returns, and the server accepts messages idempotently. The client syncs incrementally from a cursor instead of re-sending the whole conversation.
Stop when the session stops
One subtle bug mattered more than the transport: stopping the chat engine cleared the timers but not the user identity. An in-flight sync could fire against the next session. The fix was to clear the identity before clearing the timers.
- Write to a local outbox before attempting the network.
- Sync incrementally from a cursor, not from the start.
- Make message submission idempotent on the server.
- Clear user identity before stopping sync timers on sign-out.
- Use Redis pub/sub and presence when the API runs on more than one instance.
