Skip to content
HS®
All projects

(012) / 012

Real-TimeChat

A MERN chat application with Socket.IO: authentication, one-to-one messaging with images, and live online presence.

Category
Full-stack · WebSockets
Year
2025
Last pushed
Sept 2025
Primary language
JavaScript
emit("newMessage")
Conceptual diagram — messages passing between two clients over a socket.

(01) Overview

A full-stack real-time chat application on the MERN stack with Socket.IO. Users sign up, log in, see who is online and exchange text and image messages one to one, with messages delivered live to connected recipients.

(02) Problem statement

HTTP is request and response; chat is not. A message has to reach the other person without them asking for it, and both sides need to know who is currently connected. That means holding a persistent connection per user and keeping track of which connection belongs to whom.

(03) Technical approach

  • REST for state, sockets for push. Messages are created through a normal authenticated API call and stored in MongoDB; Socket.IO is used only to push the new message and presence changes.
  • A presence map on the server. Each socket connects with its user ID, and the server keeps a map from user ID to socket ID.
  • Small client stores. Zustand stores hold auth, chat and theme state.

(04) System architecture

WebSocket event flowConceptual diagram · select a stage

Stage 01 — Connect

The client opens a socket with its userId in the handshake query.

(05) Implementation details

The backend is Express with controllers for auth and messages, Mongoose models for users and messages, and JWT middleware protecting routes. When a message is saved, the controller looks up the receiver's socket ID and, if they are connected, emits a newMessage event to that socket. On every connect and disconnect the server broadcasts getOnlineUsers with the current list of user IDs.

Image messages are uploaded to Cloudinary and stored by URL. The frontend is React on Vite with Tailwind CSS and DaisyUI, and includes login, sign-up, profile and settings pages with loading skeletons.

(06) Main features

  1. 01Sign-up, login and protected routes with JWT authentication.
  2. 02One-to-one conversations with text and image messages.
  3. 03Online status: the server keeps a userId → socketId map and broadcasts it on connect and disconnect.
  4. 04Client state in Zustand stores for auth, chat and theme.
  5. 05Profile and settings pages, with loading skeletons.

(07) Technology stack

JavaScriptReactViteZustandTailwind CSSDaisyUINode.jsExpressSocket.IOMongoDBJWTCloudinary

(08) Engineering challenges

  • Mapping users to connections. Presence is only as correct as the user-to-socket map, which has to be updated on every connect and disconnect.
  • Two channels, one source of truth. Because messages are persisted through REST first, a recipient who is offline still finds them when they next load the conversation.
  • Current limits. The presence map is in process memory, so this design runs on a single server instance; scaling out would need a shared adapter.

(09) Evaluation and results

No tests or benchmarks are published in the repository, so no results are claimed here.

Links