Real-time chat app (Socket.IO)
Group rooms and one-to-one chat with typing indicators, online status and saved history.
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
Server
npm i express socket.io; const io = new Server(httpServer, { cors: { origin: CLIENT_URL } }).
- 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
Identify users
Pass a token in the handshake (io(url, { auth: { token } })) and verify it in io.use middleware.
- 4
Save messages
Message model {room, sender, text, createdAt}; save before broadcasting; GET /api/rooms/:room/messages?before= for history.
- 5
Client
React with socket.io-client; useEffect to subscribe and clean up listeners on unmount.
- 6
Typing and presence
Emit "typing" with a throttle; keep a Map of online user ids on the server and broadcast changes.
- 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