# dun-sin/whisper architecture

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

## Overview

##### Main Components
- Client (Frontend):
– Located in the “client” directory.
– Built with React and styled with TailwindCSS.
– Contains various components (e.g., Chat, NavBar, EmojiPicker) and state management through Context and custom hooks.
– Uses libraries for notifications, axios for HTTP requests, and socket integration (e.g., chatSocket.js, socketConnection.js).

- Server (Backend):
– Found in the “server” directory.
– NodeJS application that includes controllers (userController.js, codeController.js), models (ChatModel.js, MessageModel.js, UserModel.js), and a collection of Socket.IO event handlers (e.g., sendMessage.js, join.js, typing.js).
– Handles business logic, REST endpoints, and real-time socket events.

- Database:
– Though not a separate folder, the README mentions using MongoDB.
– The server’s .env_sample hints at a localhost MongoDB URL (or a Docker port when using Docker).

- Docker:
– A Docker Compose file is present.
– The server supports Docker deployment (e.g., “npm run dev:docker”) with the appropriate MongoDB port defined (27018 for Docker usage).

##### Relationships and Data Flow
- The client (browser) interacts with the server via:
– Standard HTTP/REST API for regular CRUD operations.
– Socket.IO for real-time events such as joining a chat, sending and receiving messages, typing indicators, etc.
- The server processes these requests and commands:
– Uses controllers to handle API endpoints.
– Delegates real-time logic to socket modules, managing events like sendMessage, join, editMessage, etc.
- Data is stored and retrieved from the MongoDB, accessed through the models.
- Docker is used to orchestrate deployment, where the server container and MongoDB instance (if containerized) interact over specified network ports.

##### Architectural Patterns and Technologies
- Separation of Concerns:
– The clear split between client and server directories supports the standard full-stack separation.
- Real-Time Communication:
– Integration of Socket.IO in the server (divided into modular socket event files) and corresponding socket helper files in the client.
- State Management (in the client):
– Usage of React Contexts (e.g., AuthContext, ChatContext) to manage global states.
- Extensibility and Modularity:
– Multiple folders and files (e.g., context, hooks, controllers, models, sockets) allow adding or modifying parts of the application without major rework.
- Use of Docker for Deployment:
– Docker Compose orchestrates the containerized parts of the application, ensuring portability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Browser("User's Browser"):::external

    %% Client (React Frontend) Subgraph
    subgraph "Client (React Frontend)"
        C1("UI Components"):::frontend
        C2("Contexts"):::frontend
        C3("Hooks"):::frontend
        C4("Socket Integration"):::frontend
    end

    %% Server (NodeJS Backend) Subgraph
    subgraph "Server (NodeJS Backend)"
        S1("REST API Controllers"):::backend
        S2("Socket.IO Handlers"):::backend
        S3("Data Models"):::backend
        S4("Business Logic & Utilities"):::backend
    end

    %% Database Component
    D("MongoDB"):::database

    %% Docker Deployment Component
    Docker("Docker Compose"):::docker

    %% External Email Service (Optional)
    ET("Mailtrap"):::external

    %% Relationships - User to Client
    Browser -->|"UserActions"| C1
    Browser -->|"UserActions"| C2
    Browser -->|"UserActions"| C3
    Browser -->|"UserActions"| C4

    %% Relationships - Client to Server (API and WebSocket)
    C1 -->|"HTTP/REST"| S1
    C4 -->|"WebSocket"| S2

    %% Internal Server Relationships
    S1 -->|"Uses"| S3
    S2 -->|"Uses"| S3
    S4 -->|"Supports"| S1
    S4 -->|"SendsEmail"| ET

    %% Server to Database
    S3 -->|"CRUD"| D

    %% Docker Orchestration
    Docker --- S1
    Docker --- D

    %% Click Events for Client Components
    click C1 "https://github.com/dun-sin/whisper/tree/main/client/src/components"
    click C2 "https://github.com/dun-sin/whisper/tree/main/client/src/context"
    click C3 "https://github.com/dun-sin/whisper/tree/main/client/src/hooks"
    click C4 "https://github.com/dun-sin/whisper/tree/main/client/src/lib"

    %% Click Events for Server Components
    click S1 "https://github.com/dun-sin/whisper/tree/main/server/controllers"
    click S2 "https://github.com/dun-sin/whisper/tree/main/server/sockets"
    click S3 "https://github.com/dun-sin/whisper/tree/main/server/models"
    click S4 "https://github.com/dun-sin/whisper/tree/main/server/utils"
    click Docker "https://github.com/dun-sin/whisper/blob/main/docker-compose.yml"
    click Browser "https://github.com/dun-sin/whisper/tree/main/client"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px,color:#000;
    classDef backend fill:#98FB98,stroke:#000,stroke-width:2px,color:#000;
    classDef database fill:#FFA500,stroke:#000,stroke-width:2px,color:#000;
    classDef docker fill:#D3D3D3,stroke:#000,stroke-width:2px,color:#000;
    classDef external fill:#F2F2F2,stroke:#000,stroke-width:2px,color:#000;
```
