# jaeyonglee3/easychat architecture

> Architecture diagram of the GitHub repository jaeyonglee3/easychat, 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/jaeyonglee3/easychat
- Repository: https://github.com/jaeyonglee3/easychat

## Overview

##### Main Components
- Frontend Client: This is the React app found under the “frontend” directory. It includes pages (e.g., Login, Signup, Home), reusable components (ChatArea, Sidebar, AddFriendModal), application-level context (AuthContext, FriendContext), and hooks for managing authentication, socket connections, and chat functionalities.
- Backend Server: Under “backend”, this Node/Express server handles the business logic. It is structured with controllers (conversationController, userController), models (conversationModel, userModel), and routes (conversation.ts, user.ts). The “server.ts” file likely sets up middleware, API endpoints, and Socket.IO integration.
- Database: Although not explicitly shown as a directory, the use of MongoDB is specified in the README. This is the persistence layer for storing user data, conversation history, and other information.
- Real-time Communication Layer: Using Socket.IO for live chat features, this forms an essential part of the backend that manages persistent socket connections for real-time messaging.

##### Out Relationships and Interactions
- The Frontend communicates with the Backend via HTTP/REST API calls (for login, signup, data fetch, etc.) and through socket connections (for real-time chat updates).
- The Backend processes incoming API requests via designated routes, invokes controllers to handle business logic, and interacts with the MongoDB database to store or retrieve data.
- Socket.IO connections allow bi-directional communication between the client and server for chat functionalities, typically coordinated alongside the Express server.
- Authentication uses JWT tokens, so the server also handles secure token management while the client uses custom hooks for login/logout.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The clear division between frontend and backend, with each having its own dedicated directory, reflects this.
- MVC-like Structure in the Backend: Controllers, models, and routes are separated to achieve clarity between business logic, data representation, and API endpoints.
- Real-time Event Handling: The integration of Socket.IO introduces an event-driven architecture for handling live chat updates.
- Modularization in Frontend: Use of context, hooks, and components signals a component-based design for managing application state and UI interactions.

##### Incorporate Key Technologies
- Frontend: React.js with TypeScript, Chakra UI for styling, and several custom hooks and contexts.
- Backend: Node.js, Express, and TypeScript with Socket.IO for real-time messaging.
- Database: MongoDB (even though its connection details are not part of the file tree, it is vital to the architecture).
- Additional: JWT for authentication and secure API interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Client"
        A["Frontend Client (React + Chakra UI)"]:::frontend
        A1["Pages: Login, Signup, Home"]:::frontend
        A2["Components: ChatArea, Sidebar, AddFriendModal"]:::frontend
        A3["Context: AuthContext, FriendContext"]:::frontend
        subgraph "Custom Hooks"
            A41["useSocket"]:::frontend
            A42["useLogin"]:::frontend
            A43["useLogout"]:::frontend
        end
    end

    subgraph "Backend Server"
        B["Backend Server (Express + Socket.IO)"]:::backend
        B1["Server (server.ts)"]:::backend
        subgraph "Controllers"
            B21["User Controller"]:::backend
            B22["Conversation Controller"]:::backend
        end
        subgraph "Models"
            B31["User Model"]:::backend
            B32["Conversation Model"]:::backend
        end
        subgraph "Routes"
            B41["User Routes"]:::backend
            B42["Conversation Routes"]:::backend
        end
    end

    C["Database (MongoDB)"]:::database

    %% Communication flows
    A -->|"HTTP/REST"| B
    A41 -->|"Socket.IO"| B1
    A3 -->|"JWT_Auth"| B21
    B -->|"Queries"| C

    %% Backend internal interactions
    B1 -->|"invokes"| B21
    B1 -->|"invokes"| B22
    B21 -->|"data_op"| B31
    B22 -->|"data_op"| B32
    B41 -->|"routes_to"| B21
    B42 -->|"routes_to"| B22

    %% Click Events
    click A "https://github.com/jaeyonglee3/easychat/tree/master/frontend/src"
    click A3 "https://github.com/jaeyonglee3/easychat/blob/master/frontend/src/context/AuthContext.tsx"
    click A41 "https://github.com/jaeyonglee3/easychat/blob/master/frontend/src/hooks/useSocket.ts"
    click A42 "https://github.com/jaeyonglee3/easychat/blob/master/frontend/src/hooks/useLogin.ts"
    click A43 "https://github.com/jaeyonglee3/easychat/blob/master/frontend/src/hooks/useLogout.ts"
    click B "https://github.com/jaeyonglee3/easychat/tree/master/backend/"
    click B1 "https://github.com/jaeyonglee3/easychat/blob/master/backend/server.ts"
    click B21 "https://github.com/jaeyonglee3/easychat/blob/master/backend/controllers/userController.ts"
    click B22 "https://github.com/jaeyonglee3/easychat/blob/master/backend/controllers/conversationController.ts"
    click B31 "https://github.com/jaeyonglee3/easychat/blob/master/backend/models/userModel.ts"
    click B32 "https://github.com/jaeyonglee3/easychat/blob/master/backend/models/conversationModel.ts"
    click B41 "https://github.com/jaeyonglee3/easychat/blob/master/backend/routes/user.ts"
    click B42 "https://github.com/jaeyonglee3/easychat/blob/master/backend/routes/conversation.ts"

    %% Styles
    classDef frontend fill:#ccf,stroke:#333,stroke-width:2px;
    classDef backend fill:#fcf,stroke:#333,stroke-width:2px;
    classDef database fill:#dfd,stroke:#333,stroke-width:2px;
```
