# yashasxworkz/chatty architecture

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

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

## Overview

##### Project type and purpose
- This is a full-stack real-time chat application (“✨ Full Stack Realtime Chat App ✨”) built with the MERN stack (MongoDB, Express, React, Node.js), Socket.io for bidirectional messaging, TailwindCSS + Daisy UI for styling, and Zustand for client-side state. It provides user authentication (JWT), real-time messaging, online/offline status, image uploads (via Cloudinary), and global state management.

##### File structure
- Top‐level split:
- /backend – Express API + Socket.io server
- /frontend – React single-page application (Vite, TailwindCSS)
- Backend follows an MVC‐style layout:
- src/controllers – request handlers (auth, message)
- src/models – Mongoose schemas (User, Message)
- src/routes – REST endpoints (auth.route.js, message.route.js)
- src/middleware – JWT auth guard
- src/lib – reusable libs (db connection, Socket.io init, Cloudinary upload, utils)
- src/seeds – sample data loader
- Frontend is component-driven:
- src/pages – route targets (Home, Login, SignUp, Profile, Settings)
- src/components – UI parts (Sidebar, ChatContainer, MessageInput, skeletons)
- src/store – Zustand hooks for auth, chat, theme, emotion state
- src/lib – axios instance, utils
- Config/build files: package.json, vite.config.js, tailwind.config.js

a. Client (Browser/SPA)
b. Web Server (Express + Socket.io)
c. Authentication (JWT middleware)
d. Database (MongoDB Atlas)
e. Cloudinary (image storage service)
f. Real-time channel (Socket.io engine)

##### Relationships & data flows
- User → HTTP(S) REST calls → /api/auth & /api/messages on Express
- Express ↔ MongoDB via Mongoose (db.js lib)
- Express ↔ Cloudinary (cloudinary.js lib) for media uploads
- Client ↔ Socket.io:
- On login, SPA obtains JWT, stores in client store
- Socket client attaches JWT in handshake → server validates via auth.middleware
- Message send: client emits “send_message” → server broadcasts “receive_message” to rooms/users
- Presence: server emits “user_online” / “user_offline” events
- Frontend state: Zustand stores auth token, chat history, user list, theme, emotion states

##### Architectural patterns & design principles
- Backend: MVC separation, single Node.js process hosting HTTP and WebSocket on same port
- Auth: stateless JWT, middleware enforcement on both REST endpoints and Socket handshake
- Frontend: unidirectional data flow, global state via Zustand, component reusability
- Responsiveness: skeleton loaders for async data
- Scalability: cloud storage for media, external DB service

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser SPA
    subgraph "Browser (React SPA)"
        direction TB
        App["App.jsx"]:::frontend
        Main["main.jsx"]:::frontend

        subgraph "Pages"
            direction TB
            Home["HomePage.jsx"]:::frontend
            Login["LoginPage.jsx"]:::frontend
            SignUp["SignUpPage.jsx"]:::frontend
            Profile["ProfilePage.jsx"]:::frontend
            Settings["SettingsPage.jsx"]:::frontend
        end

        subgraph "Components"
            direction TB
            Sidebar["Sidebar.jsx"]:::frontend
            ChatContainer["ChatContainer.jsx"]:::frontend
            MessageInput["MessageInput.jsx"]:::frontend
            ChatHeader["ChatHeader.jsx"]:::frontend
            Navbar["Navbar.jsx"]:::frontend
            NoChatSelected["NoChatSelected.jsx"]:::frontend
            Skeletons["skeletons"]:::frontend
        end

        subgraph "State (Zustand)"
            direction TB
            AuthStore["useAuthStore.js"]:::state
            ChatStore["useChatStore.js"]:::state
            EmotionStore["useEmotionStore.js"]:::state
            ThemeStore["useThemeStore.js"]:::state
        end

        subgraph "Lib"
            direction TB
            Axios["axios.js"]:::frontend
            UtilsFE["utils.js"]:::frontend
        end
    end

    %% Server
    subgraph "Express + Socket.io Server"
        direction TB
        Index["index.js"]:::backend

        subgraph "REST API Layer"
            direction TB
            AuthRoute["auth.route.js"]:::backend
            MessageRoute["message.route.js"]:::backend
            AuthController["auth.controller.js"]:::backend
            MessageController["message.controller.js"]:::backend
        end

        subgraph "Socket.io Handler"
            direction TB
            SocketManager["socket.js"]:::backend
        end

        subgraph "Middleware"
            direction TB
            AuthMiddleware["auth.middleware.js"]:::backend
        end

        subgraph "Lib & Utils"
            direction TB
            DBConn["db.js"]:::backend
            CloudinarySvc["cloudinary.js"]:::backend
            UtilsBE["utils.js"]:::backend
        end

        Seeds["user.seed.js"]:::backend
    end

    %% Persistence and External
    DB["MongoDB Atlas"]:::persistence
    Cloudinary["Cloudinary"]:::external

    %% Connections: HTTP
    App -->|"import / render"| Home
    App -->|"import / render"| Login
    App -->|"import / render"| SignUp
    App -->|"import / render"| Profile
    App -->|"import / render"| Settings

    Home -->|"fetch chat history"| Axios
    Login -->|"POST /api/auth/login"| AuthRoute
    SignUp -->|"POST /api/auth/signup"| AuthRoute
    Profile -->|"GET /api/users/:id"| AuthRoute
    MessageInput -->|"POST /api/messages"| MessageRoute
    ChatContainer -->|"GET /api/messages"| MessageRoute

    %% WebSocket
    App -.->|"connect (JWT handshake)"| SocketManager
    MessageInput -.->|"emit send_message"| SocketManager
    SocketManager -.->|"broadcast receive_message"| ChatContainer
    AuthStore -.->|"token"| SocketManager

    %% Server to DB and Cloudinary
    AuthRoute -->|"read/write users"| DBConn
    MessageRoute -->|"read/write messages"| DBConn
    DBConn --> DB
    CloudinarySvc --> Cloudinary

    %% Click events
    click App "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/App.jsx"
    click Main "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/main.jsx"
    click Home "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/pages/HomePage.jsx"
    click Login "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/pages/LoginPage.jsx"
    click SignUp "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/pages/SignUpPage.jsx"
    click Profile "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/pages/ProfilePage.jsx"
    click Settings "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/pages/SettingsPage.jsx"
    click Sidebar "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/Sidebar.jsx"
    click ChatContainer "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/ChatContainer.jsx"
    click MessageInput "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/MessageInput.jsx"
    click ChatHeader "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/ChatHeader.jsx"
    click Navbar "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/Navbar.jsx"
    click NoChatSelected "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/components/NoChatSelected.jsx"
    click Skeletons "https://github.com/yashasxworkz/chatty/tree/main/frontend/src/components/skeletons"
    click AuthStore "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/store/useAuthStore.js"
    click ChatStore "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/store/useChatStore.js"
    click EmotionStore "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/store/useEmotionStore.js"
    click ThemeStore "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/store/useThemeStore.js"
    click Axios "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/lib/axios.js"
    click UtilsFE "https://github.com/yashasxworkz/chatty/blob/main/frontend/src/lib/utils.js"
    click Index "https://github.com/yashasxworkz/chatty/blob/main/backend/src/index.js"
    click AuthController "https://github.com/yashasxworkz/chatty/blob/main/backend/src/controllers/auth.controller.js"
    click MessageController "https://github.com/yashasxworkz/chatty/blob/main/backend/src/controllers/message.controller.js"
    click AuthRoute "https://github.com/yashasxworkz/chatty/blob/main/backend/src/routes/auth.route.js"
    click MessageRoute "https://github.com/yashasxworkz/chatty/blob/main/backend/src/routes/message.route.js"
    click AuthMiddleware "https://github.com/yashasxworkz/chatty/blob/main/backend/src/middleware/auth.middleware.js"
    click SocketManager "https://github.com/yashasxworkz/chatty/blob/main/backend/src/lib/socket.js"
    click DBConn "https://github.com/yashasxworkz/chatty/blob/main/backend/src/lib/db.js"
    click CloudinarySvc "https://github.com/yashasxworkz/chatty/blob/main/backend/src/lib/cloudinary.js"
    click UtilsBE "https://github.com/yashasxworkz/chatty/blob/main/backend/src/lib/utils.js"
    click Seeds "https://github.com/yashasxworkz/chatty/blob/main/backend/src/seeds/user.seed.js"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#21618C,color:#1B2631;
    classDef backend fill:#ABEBC6,stroke:#1D8348,color:#145A32;
    classDef persistence fill:#FAD7A0,stroke:#B9770E,color:#784212;
    classDef external fill:#D5DBDB,stroke:#7B7D7D,color:#424949;
    classDef state fill:#D2B4DE,stroke:#7D3C98,color:#4A235A;
```
