Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Projects › Intermediate

Real-Time Chat App

One-to-one and group chat with online status, typing indicators and read receipts, all live with WebSockets.

Level: Intermediate · Time: 1–2 weeks · Tech stack: Socket.IO, Node.js, React, MongoDB

Features to build

What you will learn

Step-by-step guide

1. Server

const io = new Server(httpServer, { cors: { origin: CLIENT_URL } });
io.use(authenticateSocket);                  // verify the JWT sent in socket.handshake.auth
io.on('connection', (socket) => {
  socket.join(`user:${socket.user.id}`);
  socket.on('message', async ({ chatId, text }, ack) => {
    const msg = await Message.create({ chat: chatId, sender: socket.user.id, text });
    io.to(`chat:${chatId}`).emit('message', msg);
    ack({ ok: true, id: msg._id });            // tell the sender it was saved
  });
});

Save first, then broadcast — so nothing is lost.

2. Rooms

A socket joins chat:<id> for every chat it belongs to. Group chat = the same room with more members.

3. Presence and typing

Keep userId → count of open sockets in memory; broadcast online/offline when it changes. Throttle "typing" events (once per 2 s).

4. History

GET /api/chats/:id/messages?before=<id>&limit=30 — cursor pagination is stable while new messages arrive.

5. Read receipts

When a chat is open, emit read with the last message id; store lastRead per member.

6. React

One socket in a context; append incoming messages to state; auto-scroll unless the user scrolled up. Reconnect handling: refetch missed messages after connect.

Level it up

On your resume

Learn on Codify