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
- Login and a contacts list
- Private and group chats
- Online/offline status and "typing…"
- Read receipts (✓✓)
- Message history with infinite scroll
- Image messages
What you will learn
- WebSockets with Socket.IO
- Rooms and events
- Real-time state in React
- Cursor-based pagination
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
- Push notifications
- Redis adapter to run several servers
- End-to-end encryption
On your resume
- Built a real-time chat app with Socket.IO, React and MongoDB featuring rooms, presence, typing indicators, read receipts and cursor-paginated history.