Mobile Design Rounds
Simulator

Offline chat simulator: send a message with no connection

A chat app must work when the network is bad. Turn the connection off, send messages, and watch the app save them and send them later.

The idea

When you press send, the app does not wait for the server. It saves the message in a database on the phone. The message shows on screen at once. A list of unsent messages, the outbox, holds it until the server confirms.

The server confirms with a short reply, an acknowledgement (ack). If the reply is lost, the app does not know if the server has the message. So it sends the message again with the same ID. The server sees the same ID and ignores the copy. This is the idea behind an idempotency key. It is the design in the chat app case study.

Try this

  1. Press Send a message. The bubble shows at once, then it changes to Sent.
  2. Press Airplane mode. Press Send a message two times. Both messages stay pending, and the outbox lists them.
  3. Press Airplane mode again to turn it off. The outbox sends the messages in order.
  4. Press Lose next ack, then Send a message. The server saves the message, but the reply is lost. The phone sends it again. Look at the counter called Duplicates ignored.
  5. Now the restart case. Press Lose next ack, then Send a message. Wait for the note that the ack was lost. Then press Restart app before the retry. The screen goes blank, but the database and the outbox on the device stay. The app opens again and sends the same ID. The server ignores the copy.
  6. Now break it. Press New ID on retry, then repeat step 5. The app makes a fresh ID for the retry. The server has never seen that ID, so it stores the message again. The chat shows two bubbles. Look at the counter called Duplicate messages made.

The result of each press is also written in a line of text under the three panes.

Without JavaScript

The simulator needs JavaScript. This table shows what happens in each step above.

What you doOn the phoneOn the server
Press Send a message while onlineThe message is saved on the device as pending. It shows at once. Then it is sent.It stores the message ID once. The reply (ack) arrives and the message is marked sent.
Turn on Airplane mode, then press Send a messageThe message is saved as pending and added to the outbox. You can still read and write. The screen never waits.Nothing arrives. The server has not seen the message.
Turn Airplane mode offThe outbox sends the pending messages in order.It stores each message ID once.
Press Lose next ack, then Send a messageThe phone does not get the reply, so it sends the message again with the same ID.It stores the message the first time. The second time it sees the same ID, ignores the copy and sends the first reply again. Duplicates ignored: 1.
Press Lose next ack, send a message, then press Restart app before the retryThe screen closes. The database and the outbox stay on the device. The app opens again, sees the message is still pending and sends it with the same ID.It already has the ID. It ignores the copy and sends the first reply again. Duplicates ignored: 1.
Turn on New ID on retry and repeat the restart caseThe app makes a fresh ID for the retry. It does not match the first send.It has never seen the new ID, so it stores the message a second time. The chat shows two bubbles. Duplicate messages made: 1.

How the model works

  • Each message gets a short ID on the phone when you press send. This is a client ID. The server stores each ID once.
  • A message is pending until the ack arrives. Then it is sent.
  • In airplane mode, nothing reaches the server, and the outbox grows.
  • The outbox sends one message at a time, in the order you wrote them.
  • After a lost ack, the phone waits a moment and sends again with the same ID. Here the wait is about two seconds. In the case study, the wait starts at 10 seconds and grows.
  • The database and the outbox are saved on the device. Restart app clears only what is in memory: the screen. After the restart, the outbox sends every pending message again.
  • The ID is saved on the row when you press send. A retry, in the same run or after a restart, reads that ID. It never makes a new one.
  • New ID on retry breaks that rule on purpose. Do not do this in a real app.

Why the restart case matters

  • A lost ack and a killed app often happen together: the phone goes into a tunnel, then the system stops the app to free memory.
  • After the restart, the app has no memory of the first try. All it has is the row on disk.
  • So the client ID must be on that row, written in the same transaction as the message. Then the retry can reuse it.
  • If the retry makes a new ID, the server cannot tell it is the same message. It stores it twice, and the other person sees the message two times.
  • The same rule holds for an idempotency key on a payment. See the idempotency keys guide.

What this model leaves out

This is a small model in JavaScript. It has no real network and no background worker. The simulator does not stop the app by itself. You press Restart app. The server gives no order numbers. There are no push messages and no second device. The case study covers these.

Read more