# sairajsawant/comment-system architecture

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

- Interactive diagram: https://gitdiagram.com/sairajsawant/comment-system
- Repository: https://github.com/sairajsawant/comment-system

## Overview

##### Main Components

Frontend Layer:
- React Single Page Application (in /src directory)
- WebSocket Client Connection
- Component Structure (Auth, Comments, Registration)

Backend Layer:
- Node.js/Express Server (backend/server.js)
- Authentication Service (JWT)
- WebSocket Server
- Route Controllers
- Models Layer

Data Layer:
- MongoDB (Primary Database)
- Redis (Caching Layer)

**[Client Tier]**
- ↕
**[Application Tier]**
- ↕
**[Data Tier]**

##### Specific Component Details

Frontend (React):
**[React SPA]**
- Auth Component
- Comment List Component
- Add Comment Component
- Register Component

Backend (Node.js/Express):
**[Express Server]**
- Auth Routes
- Comment Routes
- Cache Routes
- WebSocket Handler

Data Layer:
**[Redis Cache]**
- ↓
**[MongoDB]**
- User Model
- Comment Model

##### Key Flows to Highlight

a) Authentication Flow:
**Client → Auth Route → JWT Service → Token Verification Middleware**

b) Real-time Comment Flow:
**Client → WebSocket → Redis Cache → MongoDB**

##### Additional Elements

- System boundaries to separate different services
- Docker container boundaries
- Important ports (27017 for MongoDB, 6379 for Redis)

##### Technical Details

- JWT token flow
- WebSocket event propagation
- Cache invalidation paths
- Database read/write paths
- API endpoints grouping

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        SPA["React SPA"]:::frontend
        AC["Add Comment"]:::frontend
        LC["List Comments"]:::frontend
        Auth["Auth Component"]:::frontend
        Reg["Registration"]:::frontend
        WS_Client["WebSocket Client"]:::websocket
    end

    subgraph Server
        Express["Express Server"]:::backend
        WS_Server["WebSocket Server"]:::websocket
        JWT["JWT Service"]:::auth
        TokenVerify["Token Verification"]:::auth
        
        subgraph Routes
            AuthRoute["Auth Routes"]:::backend
            CommentRoute["Comment Routes"]:::backend
            CacheRoute["Cache Routes"]:::backend
        end
        
        subgraph Models
            UserModel["User Model"]:::model
            CommentModel["Comment Model"]:::model
        end
    end

    subgraph Data
        Redis["Redis Cache"]:::cache
        MongoDB["MongoDB"]:::database
    end

    %% Client Connections
    SPA --> AC
    SPA --> LC
    SPA --> Auth
    SPA --> Reg
    SPA <--> WS_Client

    %% WebSocket Connections
    WS_Client <-.->|"Real-time Updates"| WS_Server

    %% Server Routes
    Express --> AuthRoute
    Express --> CommentRoute
    Express --> CacheRoute
    Express --> WS_Server

    %% Authentication Flow
    AuthRoute --> JWT
    JWT --> TokenVerify
    TokenVerify --> CommentRoute

    %% Data Flow
    CommentRoute --> Redis
    Redis --> MongoDB
    AuthRoute --> MongoDB

    %% Model Relationships
    CommentRoute --> UserModel
    CommentRoute --> CommentModel
    AuthRoute --> UserModel

    %% Click Events
    click SPA "https://github.com/sairajsawant/comment-system/blob/master/src/App.js"
    click AC "https://github.com/sairajsawant/comment-system/blob/master/src/components/add-comment.component.js"
    click LC "https://github.com/sairajsawant/comment-system/blob/master/src/components/list-comments.component.js"
    click Auth "https://github.com/sairajsawant/comment-system/blob/master/src/components/auth-user-component.js"
    click Reg "https://github.com/sairajsawant/comment-system/blob/master/src/components/register-user.component.js"
    click Express "https://github.com/sairajsawant/comment-system/blob/master/backend/server.js"
    click AuthRoute "https://github.com/sairajsawant/comment-system/blob/master/backend/routes/auth.js"
    click CacheRoute "https://github.com/sairajsawant/comment-system/blob/master/backend/routes/cache.js"
    click CommentRoute "https://github.com/sairajsawant/comment-system/blob/master/backend/routes/comments.js"
    click TokenVerify "https://github.com/sairajsawant/comment-system/blob/master/backend/routes/verifyToken.js"
    click UserModel "https://github.com/sairajsawant/comment-system/blob/master/backend/models/user.model.js"
    click CommentModel "https://github.com/sairajsawant/comment-system/blob/master/backend/models/comment.model.js"

    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef auth fill:#f1c40f,stroke:#f39c12,color:black
    classDef database fill:#e74c3c,stroke:#c0392b,color:white
    classDef cache fill:#e67e22,stroke:#d35400,color:white
    classDef websocket fill:#9b59b6,stroke:#8e44ad,color:white
    classDef model fill:#1abc9c,stroke:#16a085,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        Backend["Backend Service"]:::backend
        Auth_L["Auth Component"]:::auth
        DB["Database"]:::database
        Cache["Cache"]:::cache
        WS["WebSocket"]:::websocket
        Model["Model"]:::model
    end
```
