# bytemaster-1/chatty architecture

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

- Interactive diagram: https://gitdiagram.com/bytemaster-1/chatty
- Repository: https://github.com/bytemaster-1/chatty

## Overview

##### Project type and purpose
- This is a full-stack, real-time chat application built with the MERN stack (MongoDB, Express, React, Node.js), enhanced with Socket.io for real-time messaging, JWT for auth, TailwindCSS/DaisyUI for styling, Zustand for client state, and Cloudinary for media storage.

##### High-level components
a. Frontend (React + Vite)
b. Backend API & Socket.io server (Node.js + Express)
c. Database (MongoDB)
d. External services: Cloudinary for image uploads
e. Client-side global state (Zustand)
f. Authentication (JWT)

##### Relationships & data flows
- The React UI issues HTTP requests (login, signup, fetch chats) to the Express REST API.
- On successful auth, the backend returns a JWT. The client stores it (e.g., in localStorage) and includes it in subsequent HTTP headers.
- For real-time messaging, the client opens a WebSocket connection (Socket.io) to the same Node.js server, passing the JWT for verification.
- The Socket.io server authenticates the token, maintains an online-users list, and rebroadcasts messages to target clients.
- Express controllers handle REST endpoints for auth (/auth), user profile (/users), and message CRUD (/messages).
- Database layer: models (User, Message) interact with MongoDB through a db.js helper.
- Cloudinary library handles media upload in message.controller.
- Seed scripts can prepopulate the database with test users/messages.

##### Architectural patterns & principles
- Layered architecture:
- Presentation Layer (React components & pages)
- API Layer (Express routes → controllers → models)
- Data Layer (MongoDB via Mongoose-like abstractions in models)
- Event-driven real-time layer (Socket.io) for push-based updates.
- JWT-based stateless authentication.
- Separation of concerns: controllers, routes, middlewares.
- Modular design: libs (db, cloudinary, socket, utils) extracted into reusable modules.
- Global state via Zustand to simplify passing data between components.

##### Capturing essential details
- Differentiate authentication flow vs. messaging flow.
- Error handling paths if desired (e.g., 401 on invalid token).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (React + Vite)"
        direction TB
        Browser["User's Browser"]:::client
        ReactApp["React App"]:::client
        UI["UI Components"]:::client
        Pages["React Pages"]:::client
        AuthStore["useAuthStore"]:::client
        ChatStore["useChatStore"]:::client
        ThemeStore["useThemeStore"]:::client
        Axios["HTTP Client (Axios)"]:::client
        UtilsFE["Frontend Utils"]:::client
        Tailwind["Tailwind/DaisyUI Config"]:::client
        EntryMain["src/main.jsx"]:::client
        EntryApp["src/App.jsx"]:::client
        SocketClient["Socket.io Client"]:::client
    end

    %% Backend Layer
    subgraph "Backend (Express + Socket.io)"
        direction TB
        EntryServer["src/index.js"]:::server
        RoutesAuth["Auth Routes"]:::server
        RoutesMsg["Message Routes"]:::server
        AuthCtrl["Auth Controller"]:::server
        MsgCtrl["Message Controller"]:::server
        ProtectMW["JWT Auth Middleware"]:::server
        UserModel["User Model"]:::server
        MessageModel["Message Model"]:::server
        SocketLib["Socket.io Setup"]:::server
        DBLib["DB Connection Helper"]:::server
        CloudinaryLib["Cloudinary Helper"]:::server
        UtilsBE["Backend Utils"]:::server
        SeedScripts["Seed Scripts"]:::server
    end

    %% External Services
    MongoDBC[(MongoDB Atlas)]:::database
    CloudinaryExt["Cloudinary"]:::external

    %% Connections Frontend
    Browser --> ReactApp
    ReactApp -->|HTTP REST: /auth, /messages| EntryServer
    ReactApp -->|WebSocket: join, message| SocketLib
    ReactApp --> SocketClient

    %% Internal Backend Flow
    EntryServer --> RoutesAuth
    EntryServer --> RoutesMsg
    RoutesAuth --> AuthCtrl
    RoutesMsg --> MsgCtrl
    AuthCtrl --> ProtectMW
    MsgCtrl --> ProtectMW
    AuthCtrl --> UserModel
    MsgCtrl --> MessageModel
    EntryServer --> SocketLib
    EntryServer --> DBLib
    EntryServer --> CloudinaryLib
    EntryServer --> UtilsBE
    SeedScripts --> DBLib

    %% Data Stores
    EntryServer -->|Mongoose-like ORM| MongoDBC
    EntryServer -->|Media upload| CloudinaryExt

    %% Click Events Frontend
    click Browser "https://github.com/bytemaster-1/chatty/tree/main/FRONTEND/"
    click UI "https://github.com/bytemaster-1/chatty/tree/main/FRONTEND/src/components/"
    click Pages "https://github.com/bytemaster-1/chatty/tree/main/FRONTEND/src/pages/"
    click AuthStore "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/store/useAuthStore.js"
    click ChatStore "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/store/useChatStore.js"
    click ThemeStore "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/store/useThemeStore.js"
    click Axios "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/lib/axios.js"
    click UtilsFE "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/lib/utils.js"
    click Tailwind "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/tailwind.config.js"
    click EntryMain "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/main.jsx"
    click EntryApp "https://github.com/bytemaster-1/chatty/blob/main/FRONTEND/src/App.jsx"

    %% Click Events Backend
    click EntryServer "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/index.js"
    click RoutesAuth "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/routes/auth.route.js"
    click RoutesMsg "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/routes/message.route.js"
    click AuthCtrl "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/controllers/auth.controller.js"
    click MsgCtrl "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/controllers/message.controller.js"
    click UserModel "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/models/user.model.js"
    click MessageModel "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/models/message.model.js"
    click ProtectMW "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/middlewares/protectRoute.js"
    click SocketLib "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/lib/socket.js"
    click DBLib "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/lib/db.js"
    click CloudinaryLib "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/lib/cloudinary.js"
    click UtilsBE "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/lib/utils.js"
    click SeedScripts "https://github.com/bytemaster-1/chatty/blob/main/BACKEND/src/seeds/user.seeds.js"

    %% Styles
    classDef client fill:#bbdefb,stroke:#1e88e5;
    classDef server fill:#c8e6c9,stroke:#388e3c;
    classDef database fill:#ffe0b2,stroke:#f57c00;
    classDef external fill:#e1bee7,stroke:#8e24aa;
```
