A real-time chat application where users can send and receive messages instantly, see who is typing, share attachments, react to messages with emoji, and pick up where they left off after going offline. Think WhatsApp Web or Discord — but we are focusing on the frontend architecture that makes this possible.
The hard part is not the UI. It is the data layer. A chat application must show messages before the server confirms them (optimistic rendering), queue them when offline, handle reconnection without losing state, and sync message status across multiple devices. Each of these is a design decision that shapes the entire frontend architecture.
Here is the API shape I settled on — the consumer invokes the root component with a conversation ID and optional render slots:
type MessageStatus = "sending" | "sent" | "failed";
interface Message {
id: string;
conversationId: string;
senderId: string;
text: string;
status: MessageStatus;
timestamp: number;
editedAt?: number;
replyTo?: string;
reactions?: Record<string, string[]>;
attachments?: Attachment[];
}
interface Attachment {
id: string;
type: "image" | "file" | "voice";
url: string;
name: string;
sizeBytes: number;
}
type ConnectionStatus =
| "connected"
| "disconnected"
| "reconnecting";The component owns everything: WebSocket lifecycle, local state, offline persistence, and render scheduling. The consumer provides a conversation ID and optionally overrides the message bubble renderer. Let us walk through how I arrived at this design.