How tusharrpal/real-time_chat-application works
1. System shape
- SyncChat is a two-process MERN application: a React/Vite frontend and a Node/Express backend.
- The backend serves two communication planes:
- REST API for authentication and message history/persistence.
- Socket.io for real-time presence, typing, and instant message delivery.
2. Frontend application boundary
- The frontend is a single-page app built with React and Vite.
- Routing is handled in App.jsx and split into three main pages: Login, Register, and the authenticated Home chat screen.
- Styling is entirely client-side CSS, with a dark glassmorphism UI theme.
3. Client state and session management
- AuthContext is the main auth/session boundary on the client.
- It likely owns the logged-in user, token/session persistence, and route protection for authenticated screens.
- SocketContext manages the Socket.io client connection and makes realtime events available across chat components.
4. Chat UI composition
- The authenticated view is assembled from three main UI parts:
- Sidebar for user/contact list, online indicators, and unread badges.
- ChatArea for the current conversation and message stream.
- MessageInput for composing text and attaching media.
- This structure suggests the chat screen is stateful, with a selected conversation driving what appears in the main pane.
5. Authentication workflow
- User registration and login are handled on the backend by authRoutes and authController.
- User data is stored in MongoDB through the Mongoose model in backend/models/User.js.
- Passwords are hashed with bcrypt and sessions are represented with JWT, enforced by authMiddleware on protected API routes.
6. Message persistence layer
- Messages are modeled in backend/models/Message.js and persisted in MongoDB.
- messageRoutes and messageController provide the REST-side message operations, likely including:
- fetching conversation history,
- creating new messages,
- possibly marking unread/read state used by the UI badges.
- The database is the source of truth for conversations; sockets are used for live delivery, not long-term storage.
7. Realtime communication plane
- backend/socket/socket.js is the realtime coordination layer.
- Socket.io is used for:
- online/offline presence tracking,
- typing indicators,
- instant message fan-out to the recipient,
- and likely unread-count updates when the recipient is not focused on the current chat.
- This layer complements the REST API by pushing transient events without page refreshes.
8. File/media handling
- The backend has an uploads/ directory, and the README indicates Multer is used for file upload handling.
- This means media attachments are stored on the backend filesystem first, then referenced from messages rather than embedded directly in realtime events.
- The 10MB limit mentioned in the README is an important architectural boundary for attachment payloads.
9. Backend process structure
- server.js is the composition root for the backend:
- connects to MongoDB via config/db.js,
- configures Express middleware and routes,
- initializes the Socket.io server,
- and starts the HTTP listener.
- The backend is therefore both an API server and the realtime transport host.
10. Data flow: login to chat
- A new user registers or logs in through the REST API.
- The frontend stores the authenticated state in AuthContext.
- Once authenticated, the client connects to Socket.io through SocketContext.
- The app then loads chat data from the backend and begins receiving live presence/message events.
11. Data flow: sending a message
- The user composes a message in MessageInput.
- The client submits it through the backend messaging path and/or emits a socket event for realtime delivery.
- The backend persists the message in MongoDB, then broadcasts the event to the recipient socket if online.
- The sender UI updates immediately, while the recipient sees the message without refreshing.
12. Infrastructure and runtime boundaries
- Frontend runs as a Vite dev server during development.
- Backend runs as a separate Node/Express service on its own port.
- The README’s FRONTEND_URL and MONGODB_URI environment variables show a clear separation between UI, API, and database layers.
- This is a classic three-tier architecture with an additional realtime event channel between client and server.
Main entry points: Home.jsx Home · App.jsx App · server.js Server · socket.js Realtime
Components 19
Frontend
React/Vite single-page client
- App · Top-level client routing and page composition
frontend/src/App.jsx - Auth · Client auth/session state and route protection
frontend/src/context/AuthContext.jsx - Socket · Socket.io client connection and shared realtime events
frontend/src/context/SocketContext.jsx - Login · Sign-in screen
frontend/src/pages/Login.jsx - Register · Account creation screen
frontend/src/pages/Register.jsx - Home · Authenticated chat workspace
frontend/src/pages/Home.jsx - Sidebar · Contacts, presence, and unread indicators
frontend/src/components/Sidebar.jsx - Chat Area · Conversation thread and message stream
frontend/src/components/ChatArea.jsx - Message Input · Text and attachment composer
frontend/src/components/MessageInput.jsx
Backend
Node/Express API, realtime, and persistence
- Server · Backend composition root and runtime host
backend/server.js - DB Config · MongoDB connection setup
backend/config/db.js - Auth Routes · Authentication API endpoints
backend/routes/authRoutes.js - Message Routes · Message API endpoints
backend/routes/messageRoutes.js - Auth Ctrl · Registration and login logic
backend/controllers/authController.js - Message Ctrl · Message history and write operations
backend/controllers/messageController.js - Auth Guard · JWT protection for private API routes
backend/middleware/authMiddleware.js - User · User accounts stored in MongoDB
backend/models/User.js - Message · Persisted chat message records
backend/models/Message.js - Realtime · Socket.io presence, typing, and delivery events
backend/socket/socket.js
Connections 26
- App → Login: routes to · Shows login screen for unauthenticated users
- App → Register: routes to · Shows registration screen
- App → Home: routes to · Shows authenticated chat workspace
- App → Auth: uses · Reads auth state for protected routing
- Home → Sidebar: composes · Builds the left navigation pane
- Home → Chat Area: composes · Builds the conversation pane
- Home → Message Input: composes · Builds the message composer
- Home → Socket: uses · Consumes realtime chat events
- Chat Area → Socket: subscribes · Updates on presence and message events
- Message Input → Socket: emits · Sends realtime typing or delivery events
- Auth → Auth Routes: calls · Performs login and registration requests
- Chat Area → Message Routes: fetches · Loads conversation history
- Message Input → Message Routes: posts · Creates new messages through the API
- Server → DB Config: connects · Initializes MongoDB connection
- Server → Auth Routes: mounts · Registers auth API routes
- Server → Message Routes: mounts · Registers message API routes
- Auth Routes → Auth Ctrl: delegates · Routes auth requests to controller logic
- Message Routes → Message Ctrl: delegates · Routes message requests to controller logic
- Auth Ctrl → User: reads/writes · Creates and verifies user records
- Auth Ctrl → Auth Guard: relies on · Uses JWT-protected request flow
- Message Ctrl → Message: reads/writes · Persists and retrieves chat messages
- Message Ctrl → User: joins · Resolves sender and recipient user data
- Server → Realtime: initializes · Attaches Socket.io to the HTTP server
- Socket → Realtime: connects to · Maintains realtime client connection
- Realtime → Message: persists · Stores delivered messages through the message layer
- Realtime → User: tracks · Maps online users and presence