🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

Real-time chat app (Socket.IO)

Group rooms and one-to-one chat with typing indicators, online status and saved history.

Intermediate⏱ 12–16 hoursđŸĒœ 7 stepsNode.jsExpressSocket.IOReactMongoDB

What you'll learn

  • ◆WebSockets and Socket.IO events
  • ◆Rooms and broadcasting
  • ◆Storing and paginating messages

What you'll build

  • ✓Join rooms by name
  • ✓Live messages, typing indicator, online users
  • ✓Message history loaded on join
  • ✓Unread counts

Step-by-step guide

  1. 1

    Server

    npm i express socket.io; const io = new Server(httpServer, { cors: { origin: CLIENT_URL } }).

  2. 2

    Events

    io.on("connection", s => { s.on("join", room => s.join(room)); s.on("message", m => io.to(m.room).emit("message", m)); }).

  3. 3

    Identify users

    Pass a token in the handshake (io(url, { auth: { token } })) and verify it in io.use middleware.

  4. 4

    Save messages

    Message model {room, sender, text, createdAt}; save before broadcasting; GET /api/rooms/:room/messages?before= for history.

  5. 5

    Client

    React with socket.io-client; useEffect to subscribe and clean up listeners on unmount.

  6. 6

    Typing and presence

    Emit "typing" with a throttle; keep a Map of online user ids on the server and broadcast changes.

  7. 7

    Scale note

    Explain in your README that multiple servers need the Redis adapter (@socket.io/redis-adapter).

🚀 Make it stand out

  • +Send images
  • +Read receipts
  • +End-to-end encryption demo