# manasarora33/real-time-chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/manasarora33/real-time-chat-app
- Repository: https://github.com/manasarora33/real-time-chat-app

## Overview

1. **Overall shape**
   NexChat is a two-part MERN application: a React/Vite frontend in `client/` and an Express/MongoDB backend in `server/`. The app combines classic REST APIs for auth/user/chat data with Socket.io for real-time messaging, presence, typing, and read/delivery updates.

2. **Frontend entry and shell**
   The frontend boots from `client/src/main.jsx`, which likely wires the top-level app, global providers, and router. `App.jsx` and the page components under `client/src/pages/` form the screen-level shell: Home, Login, Register, and the main ChatDashboard.

3. **Frontend state boundaries**
   Global app state is split into three React contexts:
   - `AuthContext` for login/session state
   - `ChatContext` for active conversations, selected chat, and message state
   - `ThemeContext` for light/dark mode  
   This tells you the UI is organized around shared state rather than prop drilling, with chat data and user session handled separately.

4. **Frontend feature composition**
   The chat UI is broken into focused components under `client/src/components/chat/`: `Sidebar`, `MessageList`, `MessageInput`, and `MessageHeader`, plus a shared `Loader`. This is the main presentation layer for conversation browsing, message viewing, composing, and chat metadata.

5. **Frontend service layer**
   Network access is abstracted through `client/src/services/`:
   - `api.js` for the HTTP client/base API setup
   - `authService.js` for login/register/session actions
   - `chatService.js` for chat and message operations  
   The frontend is therefore split cleanly into UI, context/state, and request adapters.

6. **Backend entry and runtime**
   The backend starts from `server/src/server.js`, which is the composition point for Express middleware, routes, database connection, and Socket.io initialization. The backend is explicitly designed as a long-running Node.js service rather than serverless functions.

7. **Backend API boundaries**
   REST endpoints are organized by domain under `server/src/routes/`:
   - `authRoutes.js`
   - `userRoutes.js`
   - `chatRoutes.js`
   - `messageRoutes.js`  
   These route files map to corresponding controllers, making the API layer thin and domain-focused.

8. **Backend controllers and domain logic**
   Business logic is concentrated in `server/src/controllers/`:
   - `authController.js` handles registration/login/session behavior
   - `userController.js` supports user lookup/discovery
   - `chatController.js` manages chat lifecycle and conversation retrieval
   - `messageController.js` handles message creation/history/status updates  
   This is where request/response handling is translated into model operations and socket events.

9. **Data model layer**
   MongoDB is accessed through Mongoose models in `server/src/models/`:
   - `User.js`
   - `Chat.js`
   - `Message.js`  
   The repository’s data model is centered on users, one-to-one chats, and persisted messages, which aligns with the app’s direct-messaging workflow.

10. **Authentication and request protection**
   `server/src/middleware/authMiddleware.js` likely protects private routes by validating JWT-based sessions. The README indicates HTTP-only cookie auth with JWTs, so authenticated REST calls depend on middleware-enforced identity rather than client-side token storage.

11. **Real-time messaging path**
   Socket.io is isolated in `server/src/sockets/socket.js`, which likely wires socket connection handling, room joins, and event broadcasts. The README highlights room-based delivery using `chatId` rooms, so live message events, read receipts, typing, and presence updates flow through sockets rather than HTTP polling.

12. **Primary data flow**
   Typical flow is: the frontend authenticates via REST, then loads chat/message history via API, and finally establishes a Socket.io connection for live interaction. Message send/create actions probably persist through the backend, then emit socket events to the relevant chat room so both participants receive updates immediately.

13. **Deployment boundary**
   The repository is built for a split deployment: the `server/` directory is Dockerized and can run as a web service, while the `client/` directory is a separate Vite static build. Environment variables in both apps define the API and socket endpoints, so frontend and backend are independently deployed but tightly linked at runtime.

14. **What matters for an architecture diagram**
   The key diagram boundaries are:
   - Browser UI layer
   - React state/context layer
   - REST API layer
   - Socket.io real-time channel
   - Express controller/model layer
   - MongoDB persistence layer  
   The important arrows are: browser → REST for auth/history, browser ↔ Socket.io for live chat events, and backend → MongoDB for durable chat/user/message storage.

Main entry points: [`client/src/App.jsx`](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/App.jsx) (App shell), [`server/src/server.js`](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/server.js) (server), [`client/src/pages/ChatDashboard.jsx`](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/ChatDashboard.jsx) (Chat dashboard), [`server/src/sockets/socket.js`](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/sockets/socket.js) (Socket layer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Frontend"]
  node_client_main["main<br/>entrypoint<br/>[main.jsx]"]
  node_client_app["App shell<br/>router-shell<br/>[App.jsx]"]
  node_client_auth_ctx(("Auth state<br/>react-context<br/>[AuthContext.jsx]"))
  node_client_chat_ctx(("Chat state<br/>react-context<br/>[ChatContext.jsx]"))
  node_client_theme_ctx(("Theme state<br/>react-context<br/>[ThemeContext.jsx]"))
  node_client_home["Home<br/>page<br/>[Home.jsx]"]
  node_client_login["Login<br/>page<br/>[Login.jsx]"]
  node_client_register["Register<br/>page<br/>[Register.jsx]"]
  node_client_dashboard["Chat dashboard<br/>page<br/>[ChatDashboard.jsx]"]
  node_client_chat_ui["Chat UI<br/>feature"]
  node_client_sidebar["Sidebar<br/>chat-component<br/>[Sidebar.jsx]"]
  node_client_messages["Message list<br/>chat-component<br/>[MessageList.jsx]"]
  node_client_input["Message input<br/>chat-component<br/>[MessageInput.jsx]"]
  node_client_header["Message header<br/>chat-component<br/>[MessageHeader.jsx]"]
  node_client_loader(("Loader<br/>ui-component<br/>[Loader.jsx]"))
  node_client_api["API client<br/>http-client<br/>[api.js]"]
  node_client_auth_svc["Auth service<br/>[authService.js]"]
  node_client_chat_svc["Chat service<br/>[chatService.js]"]
end

subgraph group_server["Backend"]
  node_server_entry["server<br/>entrypoint<br/>[server.js]"]
  node_server_db["DB config<br/>database-config<br/>[db.js]"]
  node_server_auth_mw["Auth middleware<br/>[authMiddleware.js]"]
  node_server_routes["API routes<br/>route-layer"]
  node_server_auth_ctrl["Auth controller<br/>[authController.js]"]
  node_server_user_ctrl["User controller<br/>[userController.js]"]
  node_server_chat_ctrl["Chat controller<br/>[chatController.js]"]
  node_server_msg_ctrl["Message controller"]
  node_server_models[("Data models<br/>mongoose-models")]
  node_server_socket["Socket layer<br/>realtime<br/>[socket.js]"]
  node_server_user_model[("User<br/>model<br/>[User.js]")]
  node_server_chat_model[("Chat<br/>model<br/>[Chat.js]")]
  node_server_message_model[("Message<br/>model<br/>[Message.js]")]
end

node_mongo[("MongoDB<br/>datastore")]

node_client_main -->|"boots"| node_client_app
node_client_app -->|"uses"| node_client_auth_ctx
node_client_app -->|"uses"| node_client_chat_ctx
node_client_app -->|"uses"| node_client_theme_ctx
node_client_app -->|"routes"| node_client_home
node_client_app -->|"routes"| node_client_login
node_client_app -->|"routes"| node_client_register
node_client_app -->|"routes"| node_client_dashboard
node_client_dashboard -->|"composes"| node_client_chat_ui
node_client_chat_ui -->|"contains"| node_client_sidebar
node_client_chat_ui -->|"contains"| node_client_header
node_client_chat_ui -->|"contains"| node_client_messages
node_client_chat_ui -->|"contains"| node_client_input
node_client_dashboard -->|"shares"| node_client_loader
node_client_auth_ctx -->|"calls"| node_client_auth_svc
node_client_chat_ctx -->|"calls"| node_client_chat_svc
node_client_auth_svc -->|"uses"| node_client_api
node_client_chat_svc -->|"uses"| node_client_api
node_server_entry -->|"connects"| node_server_db
node_server_entry -->|"mounts"| node_server_routes
node_server_entry -->|"starts"| node_server_socket
node_server_routes -->|"auth"| node_server_auth_ctrl
node_server_routes -->|"users"| node_server_user_ctrl
node_server_routes -->|"chats"| node_server_chat_ctrl
node_server_routes -->|"messages"| node_server_msg_ctrl
node_server_routes -->|"protects"| node_server_auth_mw
node_server_auth_ctrl -->|"persists"| node_server_models
node_server_user_ctrl -->|"reads"| node_server_models
node_server_chat_ctrl -->|"reads/writes"| node_server_models
node_server_msg_ctrl -->|"writes"| node_server_models
node_server_models -->|"includes"| node_server_user_model
node_server_models -->|"includes"| node_server_chat_model
node_server_models -->|"includes"| node_server_message_model
node_server_user_model -->|"stored in"| node_mongo
node_server_chat_model -->|"stored in"| node_mongo
node_server_message_model -->|"stored in"| node_mongo
node_server_socket -->|"emits"| node_server_msg_ctrl
node_server_socket -->|"updates"| node_server_chat_ctrl

click node_client_main "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/main.jsx"
click node_client_app "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/App.jsx"
click node_client_auth_ctx "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/AuthContext.jsx"
click node_client_chat_ctx "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/ChatContext.jsx"
click node_client_theme_ctx "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/ThemeContext.jsx"
click node_client_home "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Home.jsx"
click node_client_login "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Login.jsx"
click node_client_register "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Register.jsx"
click node_client_dashboard "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/ChatDashboard.jsx"
click node_client_sidebar "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/Sidebar.jsx"
click node_client_messages "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageList.jsx"
click node_client_input "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageInput.jsx"
click node_client_header "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageHeader.jsx"
click node_client_loader "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/ui/Loader.jsx"
click node_client_api "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/api.js"
click node_client_auth_svc "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/authService.js"
click node_client_chat_svc "https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/chatService.js"
click node_server_entry "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/server.js"
click node_server_db "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/config/db.js"
click node_server_auth_mw "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/middleware/authMiddleware.js"
click node_server_auth_ctrl "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/authController.js"
click node_server_user_ctrl "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/userController.js"
click node_server_chat_ctrl "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/chatController.js"
click node_server_msg_ctrl "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/messageController.js"
click node_server_socket "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/sockets/socket.js"
click node_server_user_model "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/User.js"
click node_server_chat_model "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/Chat.js"
click node_server_message_model "https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/Message.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_client_main,node_client_app,node_client_auth_ctx,node_client_chat_ctx,node_client_theme_ctx,node_client_home,node_client_login,node_client_register,node_client_dashboard,node_client_chat_ui,node_client_sidebar,node_client_messages,node_client_input,node_client_header,node_client_loader,node_client_api,node_client_auth_svc,node_client_chat_svc toneBlue
class node_server_entry,node_server_db,node_server_auth_mw,node_server_routes,node_server_auth_ctrl,node_server_user_ctrl,node_server_chat_ctrl,node_server_msg_ctrl,node_server_models,node_server_socket,node_server_user_model,node_server_chat_model,node_server_message_model toneAmber
class node_mongo toneNeutral
```

## Components

### Frontend

React/Vite UI, shared state, and request adapters.

- [main](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/main.jsx): entrypoint · Bootstraps the React app and global providers. · `client/src/main.jsx`
- [App shell](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/App.jsx): router-shell · Top-level UI composition and route layout. · `client/src/App.jsx`
- [Auth state](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/AuthContext.jsx): react-context · Session and login state shared across the app. · `client/src/context/AuthContext.jsx`
- [Chat state](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/ChatContext.jsx): react-context · Active chat, messages, and conversation state. · `client/src/context/ChatContext.jsx`
- [Theme state](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/context/ThemeContext.jsx): react-context · Light and dark theme state. · `client/src/context/ThemeContext.jsx`
- [Home](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Home.jsx): page · Landing page for unauthenticated users. · `client/src/pages/Home.jsx`
- [Login](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Login.jsx): page · Sign-in screen. · `client/src/pages/Login.jsx`
- [Register](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/Register.jsx): page · Account creation screen. · `client/src/pages/Register.jsx`
- [Chat dashboard](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/pages/ChatDashboard.jsx): page · Primary chat workspace and screen coordinator. · `client/src/pages/ChatDashboard.jsx`
- **Chat UI**: feature · Conversation browsing, reading, and composing components.
- [Sidebar](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/Sidebar.jsx): chat-component · Conversation list and navigation. · `client/src/components/chat/Sidebar.jsx`
- [Message list](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageList.jsx): chat-component · Displays conversation history and status. · `client/src/components/chat/MessageList.jsx`
- [Message input](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageInput.jsx): chat-component · Message composer and send controls. · `client/src/components/chat/MessageInput.jsx`
- [Message header](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/chat/MessageHeader.jsx): chat-component · Conversation metadata and header actions. · `client/src/components/chat/MessageHeader.jsx`
- [Loader](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/components/ui/Loader.jsx): ui-component · Shared loading indicator. · `client/src/components/ui/Loader.jsx`
- [API client](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/api.js): http-client · Shared HTTP base configuration. · `client/src/services/api.js`
- [Auth service](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/authService.js): service · Login, register, and session requests. · `client/src/services/authService.js`
- [Chat service](https://github.com/manasarora33/real-time-chat-app/blob/main/client/src/services/chatService.js): service · Chat and message request helpers. · `client/src/services/chatService.js`

### Backend

Express API, domain logic, sockets, and Mongo models.

- [server](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/server.js): entrypoint · Express app composition, database setup, and sockets. · `server/src/server.js`
- [DB config](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/config/db.js): database-config · MongoDB connection setup. · `server/src/config/db.js`
- [Auth middleware](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/middleware/authMiddleware.js): middleware · Protects private routes with session validation. · `server/src/middleware/authMiddleware.js`
- **API routes**: route-layer · Domain REST endpoints for auth, users, chats, and messages.
- [Auth controller](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/authController.js): controller · Registration, login, and session handling. · `server/src/controllers/authController.js`
- [User controller](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/userController.js): controller · User lookup and discovery. · `server/src/controllers/userController.js`
- [Chat controller](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/chatController.js): controller · Chat lifecycle and conversation retrieval. · `server/src/controllers/chatController.js`
- [Message controller](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/controllers/messageController.js): controller · Message creation, history, and status updates. · `server/src/controllers/messageController.js`
- **Data models**: mongoose-models · User, chat, and message persistence models.
- [Socket layer](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/sockets/socket.js): realtime · Room-based real-time messaging and presence events. · `server/src/sockets/socket.js`
- [User](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/User.js): model · User account documents. · `server/src/models/User.js`
- [Chat](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/Chat.js): model · Conversation documents. · `server/src/models/Chat.js`
- [Message](https://github.com/manasarora33/real-time-chat-app/blob/main/server/src/models/Message.js): model · Persisted message documents. · `server/src/models/Message.js`

### Other components

- **MongoDB**: datastore · Persistent document database.

## Connections

- main → App shell: boots · React entry renders the application shell.
- App shell → Auth state: uses · App shell depends on authentication state.
- App shell → Chat state: uses · App shell depends on conversation state.
- App shell → Theme state: uses · App shell depends on theme state.
- App shell → Home: routes · Public landing page.
- App shell → Login: routes · Authentication page.
- App shell → Register: routes · Registration page.
- App shell → Chat dashboard: routes · Main authenticated workspace.
- Chat dashboard → Chat UI: composes · Dashboard assembles the chat experience.
- Chat UI → Sidebar: contains · Conversation navigation.
- Chat UI → Message header: contains · Conversation header.
- Chat UI → Message list: contains · Message history view.
- Chat UI → Message input: contains · Message composer.
- Chat dashboard → Loader: shares · Common loading feedback.
- Auth state → Auth service: calls · Session state is backed by auth requests.
- Chat state → Chat service: calls · Chat state is backed by chat requests.
- Auth service → API client: uses · Auth requests share the HTTP client.
- Chat service → API client: uses · Chat requests share the HTTP client.
- server → DB config: connects · Service initializes MongoDB access.
- server → API routes: mounts · Express mounts the REST API.
- server → Socket layer: starts · Socket.io is initialized with the server.
- API routes → Auth controller: auth · Auth endpoints map to auth logic.
- API routes → User controller: users · User endpoints map to user logic.
- API routes → Chat controller: chats · Chat endpoints map to chat logic.
- API routes → Message controller: messages · Message endpoints map to message logic.
- API routes → Auth middleware: protects · Private routes are guarded by auth middleware.
- Auth controller → Data models: persists · Auth operations read and write models.
- User controller → Data models: reads · User discovery uses models.
- Chat controller → Data models: reads/writes · Chat lifecycle uses models.
- Message controller → Data models: writes · Message operations use models.
- Data models → User: includes · User document model.
- Data models → Chat: includes · Chat document model.
- Data models → Message: includes · Message document model.
- User → MongoDB: stored in · User documents persist in MongoDB.
- Chat → MongoDB: stored in · Chat documents persist in MongoDB.
- Message → MongoDB: stored in · Message documents persist in MongoDB.
- Socket layer → Message controller: emits · Real-time message events follow message creation.
- Socket layer → Chat controller: updates · Presence and room events depend on chat context.
