# narendrakumar9867/chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/narendrakumar9867/chat-app
- Repository: https://github.com/narendrakumar9867/chat-app

## Overview

##### Project type and purpose
- This is a full-stack real-time Chat Application (“Chat-App”) consisting of:
- A React single-page application (SPA) frontend built with Vite.
- A Node.js/Express backend exposing REST endpoints and a Socket.IO server for real-time messaging.
- MongoDB as the primary datastore.
- Cloudinary as an external media-storage service.
- Users register/login, send/receive messages in real time, and upload avatar or image assets.

##### File structure
- Top-level split:
- /backend – Node/Express service
– src/controllers (auth.controller.js, message.controller.js)
– src/routes (auth.route.js, MessageRoute.route.js)
– src/middleware (auth.middleware.js – JWT auth)
– src/lib (db.js → Mongo connection; socket.js → Socket.IO setup; cloudinary.js; utils.js)
– src/models (user.model.js, message.model.js)
– index.js – bootstrap Express & Socket.IO
- /frontend – React SPA
– src/pages – high-level views (Login, SignUp, Home, Profile, Settings)
– src/components – ChatContainer, Sidebar, Navbar, MessageInput, etc.
– src/store – Zustand stores for auth, chat, theme
– src/lib/axios.js – configured HTTP client
– src/utils/Firebase.js – optional push notifications or analytics
– Vite config, CSS files, public/index.html

##### Key insights from the README
- Tech stack: React, Node.js, Express, MongoDB.
- Environment variables for PORT, JWT_SECRET, Cloudinary credentials.
- Running commands: `npm start` (backend) and `npm run dev` (frontend).
- Emphasizes using .env for secrets.

a. Main components
- Client (React SPA in browser)
- API Layer (Express REST endpoints: /auth, /messages)
- WebSocket Layer (Socket.IO on same Express server)
- Business logic (controllers + middleware for JWT auth)
- Data layer (MongoDB via Mongoose models)
- External service (Cloudinary for file uploads)
b. Relationships & data flows
- [Client → REST API] HTTP POST/GET JSON calls for sign-up, login, fetching chat history
- [Client ↔ WebSocket] Bidirectional real-time messaging events (join room, new-message events)
- [API/Socket.IO → MongoDB] CRUD operations on users & messages
- [API → Cloudinary] Image upload API calls returning hosted URLs
c. Architectural patterns & principles
- MVC-style layering in backend (routes → controllers → models)
- Stateless JWT authentication enforced by middleware
- Single-page application with client-side routing & state management (Zustand)
- Separation of concerns: HTTP vs. WebSocket transports
d. Key technologies
- Frontend: React, Vite, axios, Zustand, CSS modules
- Backend: Node.js, Express, Mongoose, jsonwebtoken, Socket.IO, Cloudinary SDK

##### Density of detail
- Logical layers (presentation, API, business logic, data)
- JWT authentication step in the flow before protected API/WebSocket calls
- How the client’s Zustand store interacts with axios and socket utilities

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (Browser)"
        direction TB
        SPA["React SPA"]:::frontend
        Axios["HTTP Client (axios)"]:::frontend
        Firebase["Firebase Utils"]:::frontend
        AuthStore["useAuthStore"]:::frontend
        ChatStore["useChatStore"]:::frontend
        ThemeStore["useThemeStore"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Server)"
        direction TB
        Bootstrap["Server Bootstrap (index.js)"]:::backend
        subgraph "REST API Module"
            direction TB
            AuthRoute["Auth Routes (/auth)"]:::backend
            MsgRoute["Message Routes (/messages)"]:::backend
        end
        AuthController["Auth Controller"]:::backend
        MsgController["Message Controller"]:::backend
        JWT["JWT Auth Middleware"]:::backend
        subgraph "WebSocket Server"
            direction TB
            SocketServer["Socket.IO Server"]:::backend
        end
        UserModel["User Model"]:::datastore
        MessageModel["Message Model"]:::datastore
    end

    %% External Services
    subgraph "External Services"
        direction TB
        MongoDB["MongoDB"]:::datastore
        Cloudinary["Cloudinary"]:::external
    end

    %% Connections: Frontend → Backend
    SPA -->|HTTP| AuthRoute
    SPA -->|HTTP| MsgRoute
    SPA <-->|"WebSocket"| SocketServer

    %% Frontend internal
    SPA --> AuthStore
    SPA --> ChatStore
    SPA --> ThemeStore
    SPA --> Axios
    SPA --> Firebase

    %% Backend initialization
    Bootstrap --> AuthRoute
    Bootstrap --> MsgRoute
    Bootstrap --> SocketServer
    Bootstrap --> JWT

    %% REST API flows
    AuthRoute --> JWT
    MsgRoute --> JWT
    AuthRoute --> AuthController
    MsgRoute --> MsgController

    %% Controllers to models
    AuthController --> UserModel
    MsgController --> MessageModel

    %% Controllers to external services
    AuthController --> MongoDB
    MsgController --> MongoDB
    MsgController --> Cloudinary
    SocketServer --> JWT
    SocketServer --> MessageModel
    SocketServer --> MongoDB

    %% Styles
    classDef frontend fill:lightgreen,stroke:#333,stroke-width:1px
    classDef backend fill:lightblue,stroke:#333,stroke-width:1px
    classDef datastore fill:lightorange,stroke:#333,stroke-width:1px
    classDef external fill:plum,stroke:#333,stroke-width:1px

    %% Click events
    click SPA "https://github.com/narendrakumar9867/chat-app/tree/main/frontend/src"
    click Axios "https://github.com/narendrakumar9867/chat-app/blob/main/frontend/src/lib/axios.js"
    click Firebase "https://github.com/narendrakumar9867/chat-app/blob/main/frontend/src/utils/Firebase.js"
    click AuthStore "https://github.com/narendrakumar9867/chat-app/blob/main/frontend/src/store/useAuthStore.js"
    click ChatStore "https://github.com/narendrakumar9867/chat-app/blob/main/frontend/src/store/useChatStore.js"
    click ThemeStore "https://github.com/narendrakumar9867/chat-app/blob/main/frontend/src/store/useThemeStore.js"
    click Bootstrap "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/index.js"
    click AuthRoute "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/routes/auth.route.js"
    click MsgRoute "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/routes/MessageRoute.route.js"
    click AuthController "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/controllers/auth.controller.js"
    click MsgController "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/controllers/message.controller.js"
    click JWT "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/middleware/auth.middleware.js"
    click UserModel "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/models/user.model.js"
    click MessageModel "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/models/message.model.js"
    click SocketServer "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/lib/socket.js"
    click Cloudinary "https://github.com/narendrakumar9867/chat-app/blob/main/backend/src/lib/cloudinary.js"
```
