# avtarsingh97/chitchatapp architecture

> Architecture diagram of the GitHub repository avtarsingh97/chitchatapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/avtarsingh97/chitchatapp
- Repository: https://github.com/avtarsingh97/chitchatapp

## Overview

##### Project type and purpose
- This is a full-stack, real-time messaging web application (CHITCHAT) built on the MERN stack.
- It supports user registration, login, conversation management and live chat via WebSocket (Socket.io).
- Backend runs on Node/Express connected to MongoDB Atlas; frontend is a React single-page app bundled with Vite and styled with Tailwind CSS.

##### File structure
- Top level:
– backend/ : Express server, API controllers, Mongoose models, routes, Socket.io integration.
– frontend/ : React app, Vite config, Tailwind CSS via index.html, components folder, socket client.
- backend:
– Authentication module (Auth.js handles JWT, bcrypt, cookie parsing).
– Controllers for users, conversations, messages.
– Models for User, Conversation, Message.
– Routes mirror controllers.
– connection.js sets up MongoDB via Mongoose.
– index.js wires Express, HTTP server, Socket.io, middleware (CORS, cookie-parser, dotenv).
- frontend:
– src/socket.js establishes WebSocket connection to backend.
– Components folder reflects UI routes: Login, Register, Dashboard, Chats, Conversation, Loader, Avatar selector, Home, NotFound.
– App.jsx holds Router and global state.

##### Key insights from README
- Tech stack list confirms MERN + Socket.io + JWT + Tailwind CSS.
- Features call out protected routes, real-time events, REST APIs for CRUD on messages and conversations.

##### Main components and their interactions
a. Client (Browser/React SPA)
– Renders UI, handles user input, calls REST endpoints (Axios) for login, registration, conversation history.
– Maintains WebSocket connection (socket.io-client) to receive/send real-time messages.
b. API Server (Express)
– Exposes REST endpoints under /api/users, /api/conversations, /api/messages
– Auth middleware verifies JWT tokens on protected routes.
– On HTTP requests, uses Mongoose to persist/fetch data in MongoDB Atlas.
c. WebSocket Server (Socket.io)
– Runs on the same Node/Express HTTP server instance.
– Handles real-time events: join conversation rooms, broadcast new messages, update presence.
d. Database (MongoDB Atlas)
– Stores User, Conversation, Message collections.
– Connection managed by Mongoose.
e. External Hosting Services (Vercel, Render)
– Optionally show as deployment targets.
f. Optional: CDN or asset hosting for frontend static files.

##### Architectural patterns and design principles
- Separation of concerns: frontend vs backend vs database.
- Layered backend: routing → controllers → models → database.
- Stateless REST API complemented by stateful WebSocket channels.
- JWT-based authentication and authorization.
- Use of environment variables for configuration (dotenv).
- Responsive, component-based UI in React.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (React SPA)"
        direction TB
        Main["main.jsx"] 
        App["App.jsx"]
        SocketClient["socket.js"]
        UIComponents["components/ (UI Components)"]
        ViteConfig["vite.config.js"]
        IndexHTML["index.html"]
    end

    %% Backend Layer
    subgraph "Backend (Node/Express & Socket.IO)"
        direction TB
        ServerInit["index.js"]
        Auth["Auth.js (JWT, bcrypt, cookie-parser)"]
        Middleware["Auth & CORS Middleware"]
        DBConn["connection.js"]
        subgraph "REST Controllers"
            direction TB
            UCtrl["user.js"]
            CCtrl["conversation.js"]
            MCtrl["message.js"]
        end
        subgraph "API Routes"
            direction TB
            URt["user.js"]
            CRt["conversation.js"]
            MRt["message.js"]
        end
        subgraph "Models"
            direction TB
            UModel["user.js"]
            CModel["conversation.js"]
            MModel["message.js"]
        end
        SocketHandlers["Socket.IO Event Handlers"]
    end

    %% Database
    DB["MongoDB Atlas"]

    %% External Hosting
    subgraph "External Hosting"
        direction TB
        VercelFE["Vercel (Frontend)"]
        VercelBE["Render/Vercel (Backend)"]
    end

    %% Connections
    Main --> App
    App -->|"HTTP/JSON via Axios"| ServerInit
    App -->|"WebSocket events via Socket.io"| SocketHandlers
    SocketClient -->|"WebSocket events via Socket.io"| SocketHandlers

    ServerInit --> Auth
    ServerInit --> Middleware
    ServerInit --> DBConn
    ServerInit --> URt
    ServerInit --> CRt
    ServerInit --> MRt
    ServerInit --> SocketHandlers

    URt --> UCtrl
    CRt --> CCtrl
    MRt --> MCtrl

    UCtrl --> UModel
    CCtrl --> CModel
    MCtrl --> MModel

    DBConn --> DB
    SocketHandlers -->|"Mongoose queries/updates"| DB
    UCtrl -->|"Mongoose queries/updates"| DB
    CCtrl -->|"Mongoose queries/updates"| DB
    MCtrl -->|"Mongoose queries/updates"| DB

    VercelFE -->|"Deploys React build"| Main
    VercelBE -->|"Runs Node server"| ServerInit

    %% Click Events
    click Main "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/src/main.jsx"
    click App "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/src/App.jsx"
    click SocketClient "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/src/socket.js"
    click UIComponents "https://github.com/avtarsingh97/chitchatapp/tree/main/frontend/src/components/"
    click ViteConfig "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/vite.config.js"
    click IndexHTML "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/index.html"
    click ServerInit "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/index.js"
    click Auth "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Authentication/Auth.js"
    click UCtrl "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Controllers/user.js"
    click CCtrl "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Controllers/conversation.js"
    click MCtrl "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Controllers/message.js"
    click URt "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Routes/user.js"
    click CRt "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Routes/conversation.js"
    click MRt "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Routes/message.js"
    click UModel "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Models/user.js"
    click CModel "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Models/conversation.js"
    click MModel "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Models/message.js"
    click DBConn "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/Database/connection.js"
    click VercelFE "https://github.com/avtarsingh97/chitchatapp/blob/main/frontend/vercel.json"
    click VercelBE "https://github.com/avtarsingh97/chitchatapp/blob/main/backend/vercel.json"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef backend fill:#d4edda,stroke:#28a745,color:#000;
    classDef db fill:#fff3cd,stroke:#ffc107,color:#000,stroke-dasharray: 5 5;
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#000;

    class Main,App,SocketClient,UIComponents,ViteConfig,IndexHTML ui
    class ServerInit,Auth,Middleware,DBConn,UCtrl,CCtrl,MCtrl,URt,CRt,MRt,UModel,CModel,MModel,SocketHandlers backend
    class DB db
    class VercelFE,VercelBE external
```
