All academies
All projects

Real-Time Chat App

medium

Build a real-time chat with rooms, typing indicators, and optimistic message sending over WebSockets. The centerpiece TypeScript skill is a discriminated union protocol: every client and server event is a typed message, so an exhaustive switch guarantees you handle every event the socket can deliver.

Next.jsTypeScriptTailwind CSSSocket.IOZustand

Architecture

  • UI layer: RoomList, MessageList, MessageComposer, and TypingIndicator components rendered from store selectors
  • State layer: a Zustand store keyed by room id, tracking messages, membership, and pending optimistic sends
  • Protocol layer: a shared module defining every socket event as a discriminated union used by both client and server
  • Service layer: a socket client wrapper that owns the connection lifecycle and dispatches decoded events into the store
  • Server layer: a Socket.IO server that validates incoming events against the same protocol types
  • Type layer: Message, Room, and User types plus the ClientEvent and ServerEvent unions

Folder structure

src/
  app/
    page.tsx
    chat/
      [roomId]/
        page.tsx
  components/
    RoomList.tsx
    MessageList.tsx
    MessageComposer.tsx
    TypingIndicator.tsx
  store/
    useChatStore.ts
  lib/
    socketClient.ts
  server/
    socketServer.ts
  types/
    chat.ts
    protocol.ts

Domain type definitions

Start every project by defining its types — they become the contract for every component, service, and API call that follows.

export interface ChatUser {
  id: string;
  name: string;
}

export interface Message {
  id: string;
  roomId: string;
  authorId: string;
  body: string;
  sentAt: number;
  status: "pending" | "sent" | "failed";
}

export interface Room {
  id: string;
  name: string;
  memberIds: string[];
}

export type ClientEvent =
  | { type: "join-room"; roomId: string }
  | { type: "leave-room"; roomId: string }
  | { type: "send-message"; roomId: string; body: string; clientId: string }
  | { type: "typing"; roomId: string; isTyping: boolean };

export type ServerEvent =
  | { type: "message"; message: Message; clientId?: string }
  | { type: "user-joined"; roomId: string; user: ChatUser }
  | { type: "user-left"; roomId: string; userId: string }
  | { type: "typing"; roomId: string; userId: string; isTyping: boolean };

Build steps

  1. 1Define the domain types and the ClientEvent and ServerEvent protocol unions in the shared types modules.
  2. 2Build the Socket.IO server, handling each ClientEvent in an exhaustive switch and broadcasting ServerEvent values.
  3. 3Write socketClient.ts wrapping the connection, exposing a typed send(event: ClientEvent) and an onEvent subscriber.
  4. 4Build the chat store: messages per room, member lists, and typing state, with actions driven only by ServerEvent.
  5. 5Implement optimistic sending — insert a pending Message with a clientId, then reconcile when the echo arrives.
  6. 6Build MessageList with grouping by author and day separators, deriving all display data from typed messages.
  7. 7Build MessageComposer with an Enter-to-send handler and emit typing events with a trailing debounce.
  8. 8Build TypingIndicator from the store's typing state, showing names resolved through the member list.
  9. 9Handle reconnects: rejoin rooms on connect and mark pending messages as failed after a timeout.