# emqx/mqttx architecture

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

- Interactive diagram: https://gitdiagram.com/emqx/mqttx
- Repository: https://github.com/emqx/mqttx

## Overview

##### Main Components
- Desktop Application (Electron):
- Located in the “src” folder (which includes main.ts, main process scripts, and UI components).
- Uses Electron to encapsulate a NodeJS/Chromium environment.
- Contains business logic, IPC modules, database interactions, language support, and plugin management.
- Web Application:
- Found under the “web” folder with its own Vue project structure.
- Contains components, styles, routers, state management, and utility modules to offer a browser-based MQTT client.
- CLI Tool:
- Found within the “cli” folder.
- Uses Node.js, TypeScript, and similar MQTT control libraries (relying on MQTT.js) to offer command-line interactions.
- Shared or Supporting Libraries:
- Common utilities and modules (in “src/utils”, “src/lib”, “src/database”, etc.) that deal with MQTT connections, data conversion, schema handling, logging, and so on.
- Database & Persistence Layers:
- Both the desktop and (to a lesser degree) the CLI tools interact with a SQLite database through TypeORM.
- This is evidenced by folders like “src/database” with migration scripts, models, and service layers.
- MQTT Communication Module:
- MQTT.js integration is central for making broker connections and handling different protocols (MQTT/TCP, MQTT/SSL, MQTT/WebSocket).

##### Interactions & Data Flow
- User Interface Layer:
- For the desktop and web apps, users operate via a GUI built using Vue and Element UI.
- In the CLI, the interaction is terminal-based.
- Application Logic:
- For the desktop app, the Electron main process (scripts in “src/main” and “src/plugins”) coordinates the overall application flow and mediates communication between the renderer process (UI) and the backend services.
- For the web app, Vue components (in “web/src/components” and “web/src/views”) interact with Vuex store and Vue Router to control UI and navigation.
- MQTT Communication:
- A dedicated module (found throughout the “cli/src/lib”, “src/utils/mqttUtils.ts”, etc.) handles connecting to MQTT brokers, publishing, subscribing, and handling protocol details.
- Database/Persistence:
- Database migrations, models, and service layers (present in “src/database”) manage configuration, connection histories, messaging logs, and other persistent storage.
- External Interfaces & Build/Deployment Tools:
- The GitHub workflows (.github/workflows) illustrate CI/CD pipelines for building the CLI, desktop, and web packages.
- Docker and deployment configurations also indicate containerized builds (e.g., in “cli/Dockerfile” and “web/Dockerfile”).

##### Key Architectural Patterns & Technologies
- Separation of Concerns: Distinct directories for CLI, desktop (Electron), and web interfaces show clear isolation of UI versus underlying service logic.
- Layered Architecture: The project is organized into UI (Vue components), business logic (services, plugins, MQTT handlers), and data persistence (TypeORM with SQLite).
- Modular Design: Common utilities and shared modules aid reusability across different targets (desktop, web, CLI).
- Technology Stack:
- Electron and Vue form the desktop application.
- Web client is powered by Vue.
- CLI uses Node.js with TypeScript.
- Database handled with TypeORM and SQLite.
- MQTT.js is central for protocol handling.
- Testing is set up with Jest and Cypress; CI/CD is configured through GitHub Actions.

- Split into three main sections: Desktop (Electron), Web, and CLI.
- Within the Desktop section, show:
- Electron Main Process (with modules such as window management, IPC, plugin integration).
- Renderer Process (Vue components, state management, and UI assets).
- Shared Utilities (common MQTT communication and database access layers).
- In the Web section, represent:
- Vue-based Frontend (views, components, routing, state).
- Associated API utilities (handling MQTT calls, if applicable).
- Command interface.
- Core libraries (MQTT connectivity, configuration).
- At the bottom or side, include the persistent layer:
- SQLite database accessed via TypeORM and multiple “Service” modules.
- External connections to MQTT brokers (as provided by EMQX Cloud or local EMQX instance).
- Also, show a separate build/CI/CD block referencing workflows (from the .github/workflows directory).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Desktop Application
    subgraph "Desktop Application"
        D1["Electron Main Process"]:::logic
        D2["Renderer Process (UI)"]:::ui
        D3["Plugins & IPC Modules"]:::plugin
        D1 -->|"renders"| D2
        D1 -->|"integrates"| D3
    end

    %% Web Application
    subgraph "Web Application"
        W1["Vue Frontend (Components,Views,Router,Store)"]:::ui
    end

    %% CLI Tool
    subgraph "CLI Tool"
        L1["CLI Command Interface"]:::logic
        L2["Core Libraries & Configurations"]:::shared
        L1 -->|"uses"| L2
    end

    %% Shared & Persistence
    subgraph "Shared & Persistence"
        S1["MQTT Communication Module"]:::shared
        S2["Database Service (SQLite,TypeORM)"]:::database
        S1 -->|"logs/to"| S2
    end

    %% External Interfaces
    subgraph "External Interfaces"
        E1["MQTT Broker (EMQX)"]:::external
    end

    %% Build & Deployment
    subgraph "Build & Deployment"
        B1["CI/CD Pipelines"]:::build
        B2["CLI Dockerfile"]:::build
        B3["Web Dockerfile"]:::build
    end

    %% Connections from Subsystems to Shared & External Layers
    D1 -->|"MQTT_calls"| S1
    W1 -->|"MQTT_calls"| S1
    L2 -->|"MQTT_calls"| S1

    D1 -->|"DB_ops"| S2
    L2 -->|"DB_ops"| S2

    S1 -->|"broker_connection"| E1

    %% Build & Deployment integration
    B1 -->|"builds"| D1
    B1 -->|"builds"| W1
    B1 -->|"builds"| L1
    B2 -->|"buildConfig"| L2
    B3 -->|"buildConfig"| W1

    %% Click Events
    click D1 "https://github.com/emqx/mqttx/blob/main/src/main.ts"
    click D3 "https://github.com/emqx/mqttx/tree/main/src/plugins/"
    click D2 "https://github.com/emqx/mqttx/tree/main/src/views/"
    click W1 "https://github.com/emqx/mqttx/tree/main/web/"
    click L1 "https://github.com/emqx/mqttx/tree/main/cli/src/configs/"
    click L2 "https://github.com/emqx/mqttx/tree/main/cli/src/lib/"
    click S1 "https://github.com/emqx/mqttx/blob/main/src/utils/mqttUtils.ts"
    click S2 "https://github.com/emqx/mqttx/tree/main/src/database/"
    click B1 "https://github.com/emqx/mqttx/tree/main/.github/workflows/"
    click B2 "https://github.com/emqx/mqttx/tree/main/cli/Dockerfile"
    click B3 "https://github.com/emqx/mqttx/tree/main/web/Dockerfile"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef logic fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef plugin fill:#ffeeba,stroke:#856404,stroke-width:2px;
    classDef shared fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef database fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef build fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
