# pratham2137/encryptchatapp architecture

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

- Interactive diagram: https://gitdiagram.com/pratham2137/encryptchatapp
- Repository: https://github.com/pratham2137/encryptchatapp

## Overview

##### Project type and purpose
- EncryptChatApp is a full-stack real-time chat application built on the MERN stack (MongoDB, Express, React, Node).
- It provides end-to-end encryption (ECDH for key exchange, AES-GCM for message encryption), user authentication (JWT), and real-time messaging via WebSockets.

##### File structure
Top Level
- client/ – React+TypeScript+Vite frontend
- server/ – Node.js+Express backend with MVC pattern
- docs/ – system-design documentation
client/
- src/components – UI split into Auth, Chats, Contacts, Groups, Settings, Navigation, Home, NotFound
- src/utils – AuthContext, ProtectedRoute, socket wrapper, loading overlay
- Tailwind CSS config, Vite config, TS configs
server/
- controllers/ – authenticationController, messageController, userController
- routes/ – authenticationRoutes, messageRoutes, userRoutes
- models/ – userModel, messageModel (Mongoose schemas)
- services/ – socket.js (WebSocket event handling)
- utils/ – generateTokens (JWT), other helpers
- middlewares/ – authTokenMiddleware (JWT verification)
- config/ – db.js (MongoDB connection)
- server.js – Express app initialization, WebSocket integration

##### README for additional insights
- Emphasizes modular, scalable architecture.
- Lists core features: E2E encryption, secure key exchange, real-time via WebSockets, self-destructing messages, typing indicators, message delivery status.
- Tech stack highlights Web Crypto API on client, Express & MongoDB on server, and WebSockets for real-time channels.

## Diagram (Mermaid)

```mermaid
flowchart TD
    title["EncryptChatApp System Architecture"]:::title
    
    subgraph "Frontend (Browser)"
        subgraph "UI Layer"
            Layout["Layout Wrapper"]:::frontend
            Login["Login"]:::frontend
            Logout["Logout"]:::frontend
            Register["Register"]:::frontend
            Chats["Chats"]:::frontend
            Contacts["Contacts"]:::frontend
            Groups["Groups"]:::frontend
            Home["Home"]:::frontend
            Sidebar["Navigation Sidebar"]:::frontend
            Profile["Profile Settings"]:::frontend
            Settings["Settings"]:::frontend
            NotFound["NotFound"]:::frontend
        end
        subgraph "Client Utils & Config"
            AuthCtx["AuthContext"]:::frontend
            Protected["ProtectedRoute"]:::frontend
            Loading["LoadingOverlay"]:::frontend
            SocketClient["WebSocket Client"]:::crypto
            RESTClient["REST API Client"]:::frontend
            CryptoMod["Crypto Module (ECDH & AES-GCM)"]:::crypto
            ViteCfg["Vite Config"]:::frontend
            TailwindCfg["Tailwind & Styles"]:::frontend
        end
    end

    subgraph "Backend (Server)"
        Server["Express App & WS Integration"]:::backend
        subgraph "Config & Database Connector"
            DBConn["Database Connector"]:::backend
        end
        subgraph "Middleware & Security"
            AuthMid["JWT Verification Middleware"]:::backend
        end
        subgraph "API Layer (Routes)"
            AuthRoutes["authenticationRoutes"]:::backend
            UserRoutes["userRoutes"]:::backend
            MsgRoutes["messageRoutes"]:::backend
        end
        subgraph "Controller Layer"
            AuthCtrl["authenticationController"]:::backend
            UserCtrl["userController"]:::backend
            MsgCtrl["messageController"]:::backend
        end
        subgraph "Services & Utils"
            SocketSvc["WebSocket Service"]:::backend
            TokenUtil["JWT Generation Utility"]:::backend
        end
    end

    DB["MongoDB"]:::database

    %% Frontend interactions
    Layout -->|renders UI| Login
    Layout -->|renders UI| Logout
    Layout -->|renders UI| Register
    Layout -->|renders UI| Chats
    Layout -->|renders UI| Contacts
    Layout -->|renders UI| Groups
    Layout -->|renders UI| Home
    Layout -->|renders UI| Sidebar
    Layout -->|renders UI| Profile
    Layout -->|renders UI| Settings
    Layout -->|renders UI| NotFound

    Login -->|uses| RESTClient
    Logout -->|uses| RESTClient
    Register -->|uses| RESTClient

    RESTClient -->|POST /auth/login/register| AuthRoutes
    AuthRoutes -->|calls| AuthCtrl
    AuthCtrl -->|uses| TokenUtil
    AuthCtrl -->|write/read| DBConn
    AuthCtrl -->|returns JWT| RESTClient
    RESTClient -->|store JWT| AuthCtx
    AuthCtx -->|provides auth| Protected
    Protected -->|guards| Layout

    CryptoMod -->|encrypt/decrypt| RESTClient

    SocketClient -->|connect with JWT| Server
    Server -->|uses| AuthMid
    AuthMid -->|verifies JWT| TokenUtil
    AuthMid -->|allows| SocketSvc
    SocketSvc -->|ECDH Key Exchange| CryptoMod
    SocketSvc -->|message relay| SocketClient
    SocketSvc -->|persists| MsgCtrl

    AuthCtrl -->|persist user| DBConn
    UserCtrl -->|read/write| DBConn
    MsgCtrl -->|read/write| DBConn

    DBConn -->|connects| DB

    %% Click events
    click Layout "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/layout/Layout.tsx"
    click Login "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Auth/Login.tsx"
    click Logout "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Auth/Logout.tsx"
    click Register "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Auth/Register.tsx"
    click Chats "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Chats/Chats.tsx"
    click Contacts "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Contacts/Contacts.tsx"
    click Groups "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Groups/Groups.tsx"
    click Home "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Home/Home.tsx"
    click Sidebar "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Navigation/Sidebar.tsx"
    click Profile "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Settings/Profile.tsx"
    click Settings "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/Settings/Settings.tsx"
    click NotFound "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/components/NotFound.tsx"
    click AuthCtx "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/utils/AuthContext.tsx"
    click Protected "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/utils/ProtectedRoute.tsx"
    click Loading "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/utils/LoadingOverlay.tsx"
    click SocketClient "https://github.com/pratham2137/encryptchatapp/blob/master/client/src/utils/socket.tsx"
    click ViteCfg "https://github.com/pratham2137/encryptchatapp/blob/master/client/vite.config.ts"
    click TailwindCfg "https://github.com/pratham2137/encryptchatapp/blob/master/client/index.css"
    click Server "https://github.com/pratham2137/encryptchatapp/blob/master/server/server.js"
    click DBConn "https://github.com/pratham2137/encryptchatapp/blob/master/server/config/db.js"
    click AuthRoutes "https://github.com/pratham2137/encryptchatapp/blob/master/server/routes/authenticationRoutes.js"
    click UserRoutes "https://github.com/pratham2137/encryptchatapp/blob/master/server/routes/userRoutes.js"
    click MsgRoutes "https://github.com/pratham2137/encryptchatapp/blob/master/server/routes/messageRoutes.js"
    click AuthCtrl "https://github.com/pratham2137/encryptchatapp/blob/master/server/controllers/authenticationController.js"
    click UserCtrl "https://github.com/pratham2137/encryptchatapp/blob/master/server/controllers/userController.js"
    click MsgCtrl "https://github.com/pratham2137/encryptchatapp/blob/master/server/controllers/messageController.js"
    click AuthMid "https://github.com/pratham2137/encryptchatapp/blob/master/server/middlewares/authTokenMiddleware.js"
    click SocketSvc "https://github.com/pratham2137/encryptchatapp/blob/master/server/services/socket.js"
    click TokenUtil "https://github.com/pratham2137/encryptchatapp/blob/master/server/utils/generateTokens.js"
    click DB "https://github.com/pratham2137/encryptchatapp/blob/master/server/models/userModel.js"
    click DB "https://github.com/pratham2137/encryptchatapp/blob/master/server/models/messageModel.js"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef crypto fill:#98FB98,stroke:#333,stroke-width:1px
    classDef backend fill:#FFA500,stroke:#333,stroke-width:1px
    classDef database fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef title fill:#FFFFFF,stroke:none,font-size:16px,font-weight:bold
```
