# nguyendatgh/chat_app-serverside architecture

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

- Interactive diagram: https://gitdiagram.com/nguyendatgh/chat_app-serverside
- Repository: https://github.com/nguyendatgh/chat_app-serverside

## Overview

##### Main Components
- Express Server: The backbone of the application handling HTTP requests, middleware execution, and routing.
- Real-Time Socket Server: A dedicated module (under src/controllers/socket.ts) that manages web socket connections for real-time chat functionality.
- API Controllers & Routes: Present in the src/controllers and src/routes directories, these manage endpoints for authentication, contact management, conversations, and user operations.
- Middlewares: Modules like verifyToken and validate (in src/middlewares) that enforce security, token validation, and input validation.
- Prisma & PostgreSQL: The data layer that persists user, contact, and chat data. This component shows the use of Prisma for schema management, migrations, and generating the client.
- Utility and Validation Layers: Additional helper functions (in src/utils, src/validations, and src/validators) that ensure data consistency and might perform business logic checks.

##### Relationships and Interactions
- The client (front-end, although not part of this codebase, is implied) sends HTTP requests to the Express server (API endpoints).
- HTTP requests pass through middlewares that perform validation and authentication checks (e.g., verifyToken) before reaching the controllers.
- Controllers use services (which may internally use utilities, validators, and Prisma Client) to interact with the PostgreSQL database, executing CRUD operations.
- For real-time communication, the socket controller manages persistent socket connections where data messages flow via socket.io channels.

##### Architectural Patterns and Design Principles
- MVC (or similar layered pattern): Routes delegate requests to controllers, which then use middlewares and validations; separate concerns are maintained between handling requests, business logic, and data persistence.
- Real-time Communication: Use of socket.io indicates an event-driven model to support dynamic message exchange.
- Security and Data Integrity: JWT-based authentication and token verification middleware ensure that only authenticated requests proceed into the system.

##### Technologies, Frameworks, and Libraries
- Node.js with Express for HTTP server operations.
- TypeScript for type safety and structured code.
- Prisma as the ORM interfacing with a PostgreSQL database.
- Socket.io for managing real-time web socket connections.
- Libraries such as bcrypt, cors, dotenv, jsonwebtoken, and yup to handle security, environment configuration, and validation.

##### Specific Elements
- Labels for each major module/component (Express Server, Socket Server, Controllers, Middlewares, Prisma Client, PostgreSQL Database, etc.).
- From client requests to Express routes.
- From routes through middlewares to controllers.
- From controllers to the database via Prisma.
- Between the socket module and connected clients for real-time event handling.
- Notes indicating where security (JWT authentication) and validation (yup) are enforced.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Client
    Client("Client"):::client

    %% HTTP Request Flow Subgraph
    subgraph "HTTP Request Flow"
        ES("Express Server"):::server
        AR("API Routes"):::server
        MW("Middlewares"):::middleware
        AC("API Controllers"):::controller
        UL("Utility Layer"):::util
        VL("Validation Layer"):::util
        VT("Validators"):::util
        PC("Prisma Client"):::data
        DB("PostgreSQL Database"):::data
    end

    %% Real-Time Communication Subgraph
    subgraph "Real-Time Communication"
        SS("Socket Server"):::realtime
    end

    %% HTTP Flow Connections
    Client -->|"HTTP_Request"| ES
    ES -->|"routes"| AR
    AR -->|"passes_through"| MW
    MW -->|"calls"| AC
    AC -->|"uses"| UL
    AC -->|"uses"| VL
    AC -->|"calls"| VT
    AC -->|"CRUD_operations"| PC
    PC -->|"stores_data_in"| DB

    %% Real-Time Flow Connections
    Client -->|"WebSocket_Connection"| SS
    SS -->|"broadcasts_events"| Client

    %% Click Events
    click ES "https://github.com/nguyendatgh/chat_app-serverside/blob/master/src/index.ts"
    click AR "https://github.com/nguyendatgh/chat_app-serverside/tree/master/src/routes"
    click MW "https://github.com/nguyendatgh/chat_app-serverside/tree/master/src/middlewares"
    click AC "https://github.com/nguyendatgh/chat_app-serverside/tree/master/src/controllers"
    click SS "https://github.com/nguyendatgh/chat_app-serverside/blob/master/src/controllers/socket.ts"
    click PC "https://github.com/nguyendatgh/chat_app-serverside/blob/master/prisma/schema.prisma"
    click UL "https://github.com/nguyendatgh/chat_app-serverside/blob/master/src/utils/constants.ts"
    click VL "https://github.com/nguyendatgh/chat_app-serverside/tree/master/src/validations"
    click VT "https://github.com/nguyendatgh/chat_app-serverside/blob/master/src/validators/index.ts"

    %% Styles
    classDef server fill:#f9e79f,stroke:#d35400,stroke-width:2px;
    classDef controller fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef middleware fill:#fadbd8,stroke:#c0392b,stroke-width:2px;
    classDef data fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef realtime fill:#d2b4de,stroke:#8e44ad,stroke-width:2px;
    classDef util fill:#fcf3cf,stroke:#f39c12,stroke-width:2px;
    classDef client fill:#f0f3f4,stroke:#7f8c8d,stroke-width:2px;
```
