# ishi-ta-lal/beam-chatting-app architecture

> Architecture diagram of the GitHub repository ishi-ta-lal/beam-chatting-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ishi-ta-lal/beam-chatting-app
- Repository: https://github.com/ishi-ta-lal/beam-chatting-app

## Overview

##### Main Components
- Frontend: This is the React-based client found under the “frontend” directory. It includes:
- UI components (e.g., messages, sidebar, login, signup pages)
- Context providers (e.g., AuthContext, SocketContext)
- Custom hooks (e.g., useLogin, useSendMessage) to interact with backend services
- Static assets and configuration files (e.g., tailwind.config.js, vite.config.js)
- Backend: Located in the “backend” directory, this component provides a Node.js/Express API server including:
- Controllers (for authentication, messaging, and user operations)
- Routes that map HTTP requests to these controllers
- Models corresponding to MongoDB collections (users, conversations, messages)
- Middleware for route protection (e.g., protectRoute)
- Real-time communication using Socket.io (in the “socket” subdirectory)
- Utility functions (like token generation)
- Database connectivity via MongoDB, as set up in “connectToMongoDB.js”
- Database: MongoDB is used, where the backend connects and performs CRUD operations.
- External Services/Authentication: JWT is used for authentication. WebSocket (Socket.io) handles real-time messaging.

##### Component Relationships and Data Flow
- Frontend sends HTTP requests to the Backend API endpoints (e.g., for login, registration, messaging).
- Upon authentication, the frontend receives a JWT which will be used for subsequent requests.
- Frontend also establishes a WebSocket connection to the server for real-time message delivery and typing indicators.
- The Backend handles incoming HTTP routes via controllers and middleware, processes business logic, and interacts with MongoDB for data persistence.
- WebSocket events are managed by the “socket” module, ensuring real-time communication.
- Data flows using APIs: the frontend’s custom hooks and contexts handle changes and dispatch updates across the app.

##### Important Architectural Patterns and Design Principles
- Separation of concerns: Clear separation between presentation (frontend) and business logic/data access (backend).
- MVC Pattern in the Backend: Controllers, models, and routes illustrate the MVC separation.
- Real-time Architecture: Use of Socket.io for bidirectional communication.
- Modular Design: Organized directories indicate modular components for robust maintenance and extensibility.
- Security: JWT-based authentication alongside middleware to protect routes.

##### Relevant Technologies/Frameworks
- Frontend: React.js (components, context, hooks), Vite, Tailwind CSS.
- Backend: Node.js, Express.js, Mongoose (for MongoDB interaction), Socket.io.
- Database: MongoDB.
- Other: JWT for security tokens.

##### Suggested Elements
- Use clear labels for each component:
- Frontend (detailing UI components, state management, hooks, pages)
- Backend (broken down into controllers, routes, models, middleware, socket handling)
- Database (MongoDB)
- External/authentication services (JWT, token generation)
- API calls (from frontend to backend)
- Data persistence flows (from controllers to the database)

- Under “Frontend,” show sub-components like the Main UI, context providers, and hooks.
- Annotations for WebSocket and REST API communication between the Frontend and Backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend"
        FE1["UI: Messages"] 
        FE2["UI: Sidebar"]
        FE3["UI: Pages"]
        FE4["Context Providers (Auth,Socket)"]
        FE5["Custom Hooks (useLogin,useSendMessage)"]
        FE6["Tailwind Config"]
        FE7["Vite Config"]
    end

    %% Backend Subgraph
    subgraph "Backend"
        BE1["API Server (server.js)"]
        BE3["Routes (HTTP_Mapping)"]
        BE2["Controllers (Auth,Message,User)"]
        BE4["Middleware (protectRoute)"]
        BE5["Models (MongoDB_Collections)"]
        BE7["Utilities (JWT_Token)"]
        BE8["DB Connectivity (connectToMongoDB)"]
        BE6["Socket Module (Socket.io)"]
    end

    %% Database Node
    DB["MongoDB"]

    %% Connections within Backend
    BE1 -->|"routes"| BE3
    BE3 -->|"executes"| BE2
    BE2 -->|"CRUD"| BE5
    BE2 -->|"TokenGen"| BE7
    BE5 -->|"db_ops"| BE8
    BE8 -->|"connects"| DB
    BE4 --- BE2

    %% Frontend to Backend Connections
    FE5 -->|"HTTP_Request"| BE3
    FE4 <==>|"WebSocket"| BE6

    %% Assign Classes for Styling
    class FE1,FE2,FE3,FE4,FE5,FE6,FE7 frontend;
    class BE1,BE2,BE3,BE4,BE5,BE6,BE7,BE8 backend;
    class DB database;

    %% Click Events for Frontend Components
    click FE1 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/frontend/src/components/messages"
    click FE2 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/frontend/src/components/sidebar"
    click FE3 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/frontend/src/pages"
    click FE4 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/frontend/src/context"
    click FE5 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/frontend/src/hooks"
    click FE6 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/frontend/tailwind.config.js"
    click FE7 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/frontend/vite.config.js"

    %% Click Events for Backend Components
    click BE1 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/backend/server.js"
    click BE2 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/backend/controllers"
    click BE3 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/backend/routes"
    click BE4 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/backend/middleware/protectRoute.js"
    click BE5 "https://github.com/ishi-ta-lal/beam-chatting-app/tree/main/backend/models"
    click BE6 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/backend/socket/socket.js"
    click BE7 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/backend/utils/generateToken.js"
    click BE8 "https://github.com/ishi-ta-lal/beam-chatting-app/blob/main/backend/db/connectToMongoDB.js"

    %% Style Definitions
    classDef frontend fill:#a3c4f3,stroke:#1f6fb2,stroke-width:2px;
    classDef backend fill:#f9e79f,stroke:#f39c12,stroke-width:2px;
    classDef database fill:#d5f5e3,stroke:#28a745,stroke-width:2px;
```
