# jacket-sikaha/websocket-server architecture

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

- Interactive diagram: https://gitdiagram.com/jacket-sikaha/websocket-server
- Repository: https://github.com/jacket-sikaha/websocket-server

## Overview

##### Project Type and Purpose
- The project is a back-end service built with NestJS for managing WebSocket communications and additional functionalities such as file handling, OCR processing, and scheduled operations (cron jobs).
- The main goal is to set up a reliable communication channel via WebSockets using NestJS, together with ancillary services that support file downloads, OCR, and periodic tasks.

##### File Structure for Architectural Insights
- Top-level Directories and Files: Notice key configuration files like Dockerfile, package.json, Nest CLI config (nest-cli.json), and testing suites. These indicate that the project follows best practices for module-based development and CI/CD.
- The source directory (src) is subdivided into several modules:
- app: The main application entry point (main.ts) and core components such as AppModule, AppController, and AppService.
- cron: Contains a dedicated module for scheduled tasks, complete with its controller, service, and test files.
- file: A module aimed at file-related functionalities including file downloading, logging (via an interceptor), role-based authorization, and validation measures.
- ocr: Focuses on OCR (Optical Character Recognition) functionality, with its own controller, service, and interface definitions.
- socket: Dedicated to WebSocket events, featuring an events gateway and its corresponding module. This is the critical component enabling real-time communication.
- util: Utility functions used across the application.
- Tests and configuration files indicate that each module is isolated with its own unit tests.

##### Key Components

a. Main Application Bootstrap
- NestJS application starting point in main.ts that bootstraps the entire system.
- AppModule as the central orchestrator that imports the various functional modules.

b. Functional Modules as Independent Components
- File Module: Emphasize its role in handling file operations with its own controller, service, interceptors (logging), guards (roles), and validation pipes.
- OCR Module: Present this module separately to illustrate how it handles OCR-related functionalities, driven by its own interface definitions and service layer.
- Cron Module: Display that this module is responsible for scheduling and executing tasks at defined intervals.
- Socket Module: Highlight this module as the core part handling WebSocket connections and event communications via its events gateway component.

c. Relationships and Interactions
- Within each module, show relationships such as:
- Controllers receiving incoming requests.
- Controllers delegating logic to their corresponding services.
- Services potentially interacting with other services/components (for example, the file module’s service might use utilities from the util folder).
- In the socket module, illustrate the bidirectional data flow with external WebSocket clients.
- That modules are loosely coupled but orchestrated centrally by the AppModule.

d. Architectural Patterns & Design Principles
- Modular architecture: Each feature (WebSocket, OCR, file management, cron jobs) follows the NestJS modular structure.
- RESTful and Real-time Communication: Separate controllers for handling HTTP requests and WebSocket events.
- Use of Interceptors, Guards, and Pipes for cross-cutting concerns in the file module.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Application Bootstrap
    Bootstrap["NestJS Bootstrap (main.ts)"]:::bootstrap
    AppModule["App Module (Central Orchestrator)"]:::module
    AppController["App Controller"]:::controller
    AppService["App Service"]:::service

    Bootstrap -->|"bootstraps"| AppModule
    AppModule -->|"includes"| AppController
    AppModule -->|"includes"| AppService

    %% Module Integrations from AppModule
    AppModule -->|"imports"| SocketModule[/"Socket Module"/]:::module
    AppModule -->|"imports"| FileModule[/"File Module"/]:::module
    AppModule -->|"imports"| OCRModule[/"OCR Module"/]:::module
    AppModule -->|"imports"| CronModule[/"Cron Module"/]:::module
    AppModule -->|"triggers"| CICD["CI/CD Workflow"]:::external

    %% Socket Module Subgraph
    subgraph "Socket Module"
        direction TB
        EventsModule["Events Module"]:::module
        EventsGateway["Events Gateway"]:::gateway
        EventsModule -->|"routes to"| EventsGateway
    end
    SocketModule --> EventsModule

    %% External WebSocket Client Interaction
    EventsGateway <-->|"bi-directional"| WebSocketClient["WebSocket Client"]:::external

    %% File Module Subgraph
    subgraph "File Module"
        direction TB
        FileModuleInner["File Module"]:::module
        FileController["File Controller"]:::controller
        FileService["File Service"]:::service
        LoggingInterceptor["Logging Interceptor"]:::annotation
        RolesGuard["Roles Guard"]:::annotation
        ValidationPipe["Validation Pipe"]:::annotation
        FileModuleInner -->|"handles"| FileController
        FileController -->|"delegates to"| FileService
        FileService -->|"uses"| LoggingInterceptor
        FileService -->|"uses"| RolesGuard
        FileService -->|"uses"| ValidationPipe
        FileService -->|"leverages"| UtilityFunctions["Utility Functions"]:::utility
    end
    FileModule --> FileModuleInner

    %% OCR Module Subgraph
    subgraph "OCR Module"
        direction TB
        OCRModuleInner["OCR Module"]:::module
        OCRController["OCR Controller"]:::controller
        OCRService["OCR Service"]:::service
        OCRInterface["OCR Interface"]:::annotation
        OCRModuleInner -->|"manages"| OCRController
        OCRController -->|"delegates to"| OCRService
        OCRService -->|"implements"| OCRInterface
    end
    OCRModule --> OCRModuleInner

    %% Cron Module Subgraph
    subgraph "Cron Module"
        direction TB
        CronModuleInner["Cron Module"]:::module
        CronController["Cron Controller"]:::controller
        CronService["Cron Service"]:::service
        CronModuleInner -->|"triggers"| CronController
        CronController -->|"executes"| CronService
    end
    CronModule --> CronModuleInner

    %% Styles
    classDef bootstrap fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef module fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef controller fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef service fill:#D1C4E9,stroke:#4A148C,stroke-width:2px;
    classDef annotation fill:#FFE0B2,stroke:#E65100,stroke-width:2px;
    classDef gateway fill:#F0F4C3,stroke:#827717,stroke-width:2px;
    classDef utility fill:#F8BBD0,stroke:#AD1457,stroke-width:2px;
    classDef external fill:#FFCCBC,stroke:#BF360C,stroke-dasharray: 5, 5, stroke-width:2px;

    %% Click Events
    click Bootstrap "https://github.com/jacket-sikaha/websocket-server/blob/main/src/main.ts"
    click AppModule "https://github.com/jacket-sikaha/websocket-server/blob/main/src/app.module.ts"
    click AppController "https://github.com/jacket-sikaha/websocket-server/blob/main/src/app.controller.ts"
    click AppService "https://github.com/jacket-sikaha/websocket-server/blob/main/src/app.service.ts"
    click EventsModule "https://github.com/jacket-sikaha/websocket-server/blob/main/src/socket/events.module.ts"
    click EventsGateway "https://github.com/jacket-sikaha/websocket-server/blob/main/src/socket/events.gateway.ts"
    click FileModuleInner "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/file.module.ts"
    click FileController "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/file.controller.ts"
    click FileService "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/file.service.ts"
    click LoggingInterceptor "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/logging.interceptor.ts"
    click RolesGuard "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/roles.guard.ts"
    click ValidationPipe "https://github.com/jacket-sikaha/websocket-server/blob/main/src/file/validation.pipe.ts"
    click OCRModuleInner "https://github.com/jacket-sikaha/websocket-server/blob/main/src/ocr/ocr.module.ts"
    click OCRController "https://github.com/jacket-sikaha/websocket-server/blob/main/src/ocr/ocr.controller.ts"
    click OCRService "https://github.com/jacket-sikaha/websocket-server/blob/main/src/ocr/ocr.service.ts"
    click OCRInterface "https://github.com/jacket-sikaha/websocket-server/blob/main/src/ocr/interfaces/type.ts"
    click CronModuleInner "https://github.com/jacket-sikaha/websocket-server/blob/main/src/cron/cron.module.ts"
    click CronController "https://github.com/jacket-sikaha/websocket-server/blob/main/src/cron/cron.controller.ts"
    click CronService "https://github.com/jacket-sikaha/websocket-server/blob/main/src/cron/cron.service.ts"
    click UtilityFunctions "https://github.com/jacket-sikaha/websocket-server/blob/main/src/util/index.ts"
    click CICD "https://github.com/jacket-sikaha/websocket-server/blob/main/.github/workflows/docker-image.yml"
```
