# sotaaaaa/nest-quickfix architecture

> Architecture diagram of the GitHub repository sotaaaaa/nest-quickfix, 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/sotaaaaa/nest-quickfix
- Repository: https://github.com/sotaaaaa/nest-quickfix

## Overview

##### Main components
- FIX Module Core: At the heart of the application is the FIXModule (as seen in the root, along with configuration files and module registration in the README). This is the entry point where configuration is provided (including TCP settings, authentication, and store options).
- Role-based Components:
– Acceptor: Located in src/acceptor, this handles incoming FIX connections. It is responsible for accepting, validating, and processing incoming messages.
– Initiator: Found under src/initiator, this deals with outbound FIX messages and connection initiation.
- Session Management: Under src/session, you have components that manage session state, configuration, and automatic reconnection. This is complemented by Redis‐based session storage (as configured under the “store” property).
- Message Processing Infrastructure:
– Message Parser & Protocol Handler: The src/protocol directory (with subfolders like messages, parser, flow, and validator) contains all logic for parsing incoming FIX messages, validating checksums, managing the message queue, and handling protocol-specific flows.
– Fields and Constants: The large “fields” folder (and associated types) represents the various FIX fields (such as MsgType, ClOrdID, etc.) which are used to build, inspect, and validate messages.
- Service Layer: In src/services, components like FixService and TradingService abstract the business logic for handling messages (e.g. sending new orders, processing cancellations, and sending reject messages).
- Common Utilities: The common directory includes items such as logging and error handling that are critical for debugging and resilience.
- Decorators and Event Handlers: The project uses TypeScript decorators (e.g., @OnLogon, @OnFixMessage, etc.) that allow users to tie FIX events to custom logic in controllers. This is an integral part of its event-driven architecture.
- External Integrations: Redis is used for session storage, and support for SSL/TLS in the TCP configuration indicates secure connection handling.

##### Relationships and interactions
- The FIXModule registers the overall system with configuration data and ties together the acceptor/initiator components.
- Incoming TCP connections (secured via TLS if enabled) enter through the Acceptor component, where the raw FIX messages are received.
- The Message Parser (in the protocol layer) takes over to validate, parse, and route messages based on FIX field values (such as MsgType).
- Once parsed, messages are handed off to the appropriate event handlers (using decorators like @OnFixMessage) in controllers or services.
- Session Management maintains the state of each connection, supports automatic message recovery, and integrates with Redis for persistent session storage.
- The Service Layer (e.g., TradingService) handles business logic like sending new orders, cancellations, and reject messages. It communicates back through the FixService to route messages to specific sessions or broadcast to rooms.
- Logging, error handling, and configuration utilities operate across these layers to maintain overall system integrity.

##### Architectural patterns/design principles
- Modular design: Clear separation into modules such as acceptor, initiator, protocol handlers, and session managers.
- Event-driven architecture: The use of decorators to map FIX events (logon, message, disconnection, etc.) to specific controller functions.
- Layered architecture: Separation between transport (TCP/SSL), protocol parsing/validation, business logic (services), and configuration.
- Abstraction of FIX fields and messages: Dedicated directories for FIX fields and constants ensure that protocol-specific elements are decoupled from core logic.

##### Relevant technologies
- NestJS (framework)
- TypeScript (language)
- Redis (for session storage)
- TCP with TLS support (for secure messaging)
- Decorators for event-driven handling (custom annotations like @OnLogon, etc.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Module Layer
    A["FIX Module (Core)"]:::core

    %% Connection Layer
    subgraph "Connection Layer"
        B["Acceptor"]:::connection
        C["Initiator"]:::connection
        D["TCP/TLS Client"]:::external
    end

    %% Protocol Layer
    subgraph "Protocol Layer"
        E["Message Parser & Protocol Manager"]:::protocol
        E1["Parser"]:::protocol
        E2["Checksum Validator"]:::protocol
        E3["Message Flow Manager"]:::protocol
    end

    %% Session Management Layer
    subgraph "Session Management"
        F["Session Manager"]:::session
        G["Redis Storage"]:::external
    end

    %% Service Layer
    subgraph "Service Layer"
        H["FixService"]:::service
        I["TradingService"]:::service
    end

    %% Event Handling Layer
    subgraph "Event Handling"
        J["Decorator Event Handlers"]:::event
    end

    %% Utilities Layer
    subgraph "Utilities"
        K["Common Utilities"]:::utility
        L["Fields"]:::utility
        M["Constants"]:::utility
    end

    %% Relationships
    A -->|"registers"| B
    A -->|"registers"| C

    D -->|"establishes"| B

    B -->|"receives FIX message"| E
    E -->|"aggregates"| E1
    E -->|"aggregates"| E2
    E -->|"aggregates"| E3

    E -->|"sends parsed message"| J
    J -->|"triggers logic"| H
    J -->|"triggers logic"| I
    H -->|"routes outbound"| C
    I -->|"routes outbound"| C

    F -->|"manages session for"| B
    F -->|"manages session for"| C
    F -->|"persists state to"| G

    K --- A
    K --- B
    K --- C
    K --- E

    L --- E
    M --- E

    %% Click Events
    click A "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/fix.module.ts"
    click B "https://github.com/sotaaaaa/nest-quickfix/tree/main/src/acceptor/"
    click C "https://github.com/sotaaaaa/nest-quickfix/tree/main/src/initiator/"
    click F "https://github.com/sotaaaaa/nest-quickfix/tree/main/src/session/"
    click E1 "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/protocol/parser/message.parser.ts"
    click E2 "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/protocol/parser/checksum.validator.ts"
    click E3 "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/protocol/flow/message.queue.ts"
    click E "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/protocol/manager.ts"
    click L "https://github.com/sotaaaaa/nest-quickfix/tree/main/src/fields/"
    click M "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/constants/events.constant.ts"
    click H "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/services/fix.service.ts"
    click K "https://github.com/sotaaaaa/nest-quickfix/tree/main/src/common/"
    click J "https://github.com/sotaaaaa/nest-quickfix/blob/main/src/decorators/fix.decorators.ts"

    %% Styles
    classDef core fill:#f9c,stroke:#333,stroke-width:2px;
    classDef connection fill:#bbf,stroke:#333,stroke-width:2px;
    classDef protocol fill:#cfc,stroke:#333,stroke-width:2px;
    classDef session fill:#fcf,stroke:#333,stroke-width:2px;
    classDef service fill:#ffc,stroke:#333,stroke-width:2px;
    classDef event fill:#def,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffd,stroke:#333,stroke-width:2px;
    classDef external fill:#fdd,stroke:#333,stroke-width:2px;
```
