Real-Time Chat Starts With Durable Messages

Two speech bubbles linked to stored message cards, with a separate retry path.
Concept illustration of persisted conversation state and recovery after an interruption.

In a service app, a message can change what happens next. A customer sends a photo before the technician arrives. A technician asks about access or explains a delay. If that information disappears or arrives twice, the conversation becomes another problem the two people have to solve.

I worked on Flutter chat flows tied to orders. The interface needed to feel immediate, while the underlying system needed to preserve a coherent conversation. Those goals require different pieces of state.


Authorize access to each conversation

An order-scoped conversation gives messages a clear purpose. The backend still needs to verify that the current user is allowed to read or write that conversation. The server must check membership for the requested conversation. Firebase’s security rules guide illustrates the separate work of checking authentication and access to particular data.

The message model should include a stable ID, conversation ID, sender, content type, and server-assigned ordering information. Media references and system events need explicit types rather than overloaded text fields.

A client should not be able to claim another sender ID or author a trusted system event. “Service has started” should derive from a verified order transition, not simply appear because a device submitted that sentence.


Keep local display and persistence distinct

Optimistic UI can show a message immediately. That local message should carry a sending state until the server acknowledges it. If transmission fails, the user needs a clear retry action rather than a bubble that appears delivered. Flutter’s optimistic state guide makes the same distinction between immediate feedback and the result of the underlying operation.

This illustrative model makes the distinction visible:

enum DeliveryState { sending, sent, failed }

class PendingMessage {
  const PendingMessage({
    required this.clientId,
    required this.text,
    required this.deliveryState,
  });
  final String clientId;
  final String text;
  final DeliveryState deliveryState;
}

A stable client-generated ID can help reconcile the optimistic item with the persisted one. Reusing that ID during a retry gives the backend a way to recognize the same logical message. Preventing duplicate storage still requires the server to enforce uniqueness within an appropriate conversation and sender scope.

“Sent,” “delivered,” and “read” should identify separate milestones: server persistence, receipt by another device, and the recipient’s read acknowledgement.


Reconcile messages after reconnecting

A stream, WebSocket, or polling endpoint can supply updates. After reconnecting, the client may receive items it already has or discover a gap. Merging by ID and fetching after a known cursor is safer than appending every event to a list. Firestore documents snapshot listeners and query cursors as separate tools; the application still has to define its own reconciliation behavior.

Server timestamps are useful, but ordering may need a tie-breaker or sequence. A device clock should not decide the authoritative order of a multi-device conversation.

Pagination matters for history. Loading a bounded recent page keeps startup manageable; older messages can load when requested. The merge should preserve scroll position rather than jump the reader away from the message they are inspecting.


Media has its own lifecycle

Selecting a photo, uploading it, and sending the message are separate steps. The UI should show upload progress or failure and avoid claiming a media message is sent before the attachment is available.

The server needs content validation, access controls, and a retention policy. A public URL that anyone can open may not be appropriate for private service details. Cancelling or abandoning an upload also raises a cleanup question.


Notifications bring the person back

Push notifications help when the conversation is not open. They should point to the persisted conversation, which the client loads after checking the session and access.

An old notification might refer to a completed order or a conversation the user can no longer access. The destination needs to handle that honestly. Notification previews should also consider what private content becomes visible on a lock screen.


Test the conversation as a sequence

I would test sending while offline, retrying after an ambiguous timeout, reconnecting after several messages, duplicate events, pagination, media failure, and leaving the screen during an async response. Multi-device read state adds another useful set of cases.

My review would follow the states that matter to the person using the chat: whether a retry creates a duplicate, whether a photo has uploaded, and where an old notification leads. The interface should make each of those states understandable.