# tusharrpal/real-time_chat-application architecture

> Architecture diagram of the GitHub repository tusharrpal/real-time_chat-application, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tusharrpal/real-time_chat-application
- Repository: https://github.com/tusharrpal/real-time_chat-application

## Overview

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: [`frontend/src/pages/Home.jsx`](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Home.jsx) (Home), [`frontend/src/App.jsx`](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/App.jsx) (App), [`backend/server.js`](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/server.js) (Server), [`backend/socket/socket.js`](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/socket/socket.js) (Realtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App<br/>spa router<br/>[App.jsx]"]
  node_frontend_auth(("Auth<br/>[AuthContext.jsx]"))
  node_frontend_socket(("Socket<br/>realtime context<br/>[SocketContext.jsx]"))
  node_frontend_login["Login<br/>page<br/>[Login.jsx]"]
  node_frontend_register["Register<br/>page<br/>[Register.jsx]"]
  node_frontend_home["Home<br/>chat page<br/>[Home.jsx]"]
  node_frontend_sidebar["Sidebar<br/>chat nav<br/>[Sidebar.jsx]"]
  node_frontend_chatarea["Chat Area<br/>chat view<br/>[ChatArea.jsx]"]
  node_frontend_input["Message Input<br/>composer<br/>[MessageInput.jsx]"]
end

subgraph group_backend["Backend"]
  node_backend_server["Server<br/>app bootstrap<br/>[server.js]"]
  node_backend_db[("DB Config<br/>mongo bootstrap<br/>[db.js]")]
  node_backend_auth_routes["Auth Routes<br/>http routes<br/>[authRoutes.js]"]
  node_backend_message_routes["Message Routes<br/>http routes<br/>[messageRoutes.js]"]
  node_backend_auth_ctrl["Auth Ctrl<br/>controller<br/>[authController.js]"]
  node_backend_message_ctrl["Message Ctrl<br/>controller"]
  node_backend_auth_mw["Auth Guard<br/>middleware<br/>[authMiddleware.js]"]
  node_backend_user_model[("User<br/>mongoose model<br/>[User.js]")]
  node_backend_message_model[("Message<br/>mongoose model<br/>[Message.js]")]
  node_backend_socket{{"Realtime<br/>socket coordinator<br/>[socket.js]"}}
end

node_frontend_app -->|"routes to"| node_frontend_login
node_frontend_app -->|"routes to"| node_frontend_register
node_frontend_app -->|"routes to"| node_frontend_home
node_frontend_app -->|"uses"| node_frontend_auth
node_frontend_home -->|"composes"| node_frontend_sidebar
node_frontend_home -->|"composes"| node_frontend_chatarea
node_frontend_home -->|"composes"| node_frontend_input
node_frontend_home -->|"uses"| node_frontend_socket
node_frontend_chatarea -->|"subscribes"| node_frontend_socket
node_frontend_input -->|"emits"| node_frontend_socket
node_frontend_auth -->|"calls"| node_backend_auth_routes
node_frontend_chatarea -->|"fetches"| node_backend_message_routes
node_frontend_input -->|"posts"| node_backend_message_routes
node_backend_server -->|"connects"| node_backend_db
node_backend_server -->|"mounts"| node_backend_auth_routes
node_backend_server -->|"mounts"| node_backend_message_routes
node_backend_auth_routes -->|"delegates"| node_backend_auth_ctrl
node_backend_message_routes -->|"delegates"| node_backend_message_ctrl
node_backend_auth_ctrl -->|"reads/writes"| node_backend_user_model
node_backend_auth_ctrl -->|"relies on"| node_backend_auth_mw
node_backend_message_ctrl -->|"reads/writes"| node_backend_message_model
node_backend_message_ctrl -->|"joins"| node_backend_user_model
node_backend_server -->|"initializes"| node_backend_socket
node_frontend_socket -->|"connects to"| node_backend_socket
node_backend_socket -->|"persists"| node_backend_message_model
node_backend_socket -->|"tracks"| node_backend_user_model

click node_frontend_app "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/App.jsx"
click node_frontend_auth "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/context/AuthContext.jsx"
click node_frontend_socket "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/context/SocketContext.jsx"
click node_frontend_login "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Login.jsx"
click node_frontend_register "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Register.jsx"
click node_frontend_home "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Home.jsx"
click node_frontend_sidebar "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/Sidebar.jsx"
click node_frontend_chatarea "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/ChatArea.jsx"
click node_frontend_input "https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/MessageInput.jsx"
click node_backend_server "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/server.js"
click node_backend_db "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/config/db.js"
click node_backend_auth_routes "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/routes/authRoutes.js"
click node_backend_message_routes "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/routes/messageRoutes.js"
click node_backend_auth_ctrl "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/controllers/authController.js"
click node_backend_message_ctrl "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/controllers/messageController.js"
click node_backend_auth_mw "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/middleware/authMiddleware.js"
click node_backend_user_model "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/models/User.js"
click node_backend_message_model "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/models/Message.js"
click node_backend_socket "https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/socket/socket.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_auth,node_frontend_socket,node_frontend_login,node_frontend_register,node_frontend_home,node_frontend_sidebar,node_frontend_chatarea,node_frontend_input toneBlue
class node_backend_server,node_backend_db,node_backend_auth_routes,node_backend_message_routes,node_backend_auth_ctrl,node_backend_message_ctrl,node_backend_auth_mw,node_backend_user_model,node_backend_message_model,node_backend_socket toneAmber
```

## Components

### Frontend

React/Vite single-page client

- [App](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/App.jsx): spa router · Top-level client routing and page composition · `frontend/src/App.jsx`
- [Auth](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/context/AuthContext.jsx): auth context · Client auth/session state and route protection · `frontend/src/context/AuthContext.jsx`
- [Socket](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/context/SocketContext.jsx): realtime context · Socket.io client connection and shared realtime events · `frontend/src/context/SocketContext.jsx`
- [Login](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Login.jsx): page · Sign-in screen · `frontend/src/pages/Login.jsx`
- [Register](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Register.jsx): page · Account creation screen · `frontend/src/pages/Register.jsx`
- [Home](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/pages/Home.jsx): chat page · Authenticated chat workspace · `frontend/src/pages/Home.jsx`
- [Sidebar](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/Sidebar.jsx): chat nav · Contacts, presence, and unread indicators · `frontend/src/components/Sidebar.jsx`
- [Chat Area](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/ChatArea.jsx): chat view · Conversation thread and message stream · `frontend/src/components/ChatArea.jsx`
- [Message Input](https://github.com/tusharrpal/real-time_chat-application/blob/main/frontend/src/components/MessageInput.jsx): composer · Text and attachment composer · `frontend/src/components/MessageInput.jsx`

### Backend

Node/Express API, realtime, and persistence

- [Server](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/server.js): app bootstrap · Backend composition root and runtime host · `backend/server.js`
- [DB Config](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/config/db.js): mongo bootstrap · MongoDB connection setup · `backend/config/db.js`
- [Auth Routes](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/routes/authRoutes.js): http routes · Authentication API endpoints · `backend/routes/authRoutes.js`
- [Message Routes](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/routes/messageRoutes.js): http routes · Message API endpoints · `backend/routes/messageRoutes.js`
- [Auth Ctrl](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/controllers/authController.js): controller · Registration and login logic · `backend/controllers/authController.js`
- [Message Ctrl](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/controllers/messageController.js): controller · Message history and write operations · `backend/controllers/messageController.js`
- [Auth Guard](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/middleware/authMiddleware.js): middleware · JWT protection for private API routes · `backend/middleware/authMiddleware.js`
- [User](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/models/User.js): mongoose model · User accounts stored in MongoDB · `backend/models/User.js`
- [Message](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/models/Message.js): mongoose model · Persisted chat message records · `backend/models/Message.js`
- [Realtime](https://github.com/tusharrpal/real-time_chat-application/blob/main/backend/socket/socket.js): socket coordinator · Socket.io presence, typing, and delivery events · `backend/socket/socket.js`

## Connections

- 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
