How jacket-sikaha/websocket-server works
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.