# dream-sports-labs/d11-react-native-mqtt architecture

> Architecture diagram of the GitHub repository dream-sports-labs/d11-react-native-mqtt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dream-sports-labs/d11-react-native-mqtt
- Repository: https://github.com/dream-sports-labs/d11-react-native-mqtt

## Overview

##### Main Components
- React Native (JS/TS) Layer:
– Located in the top-level "src" directory and further illustrated by the example project under "example".
– Contains JavaScript modules such as the MQTT client (in src/Mqtt/* and src/Modules/mqttModule.ts) and helper utilities for managing MQTT events.

- Native Android Module:
– Found in the "android" directory.
– Written primarily in Kotlin/Java (e.g., MqttConnectOptions.kt, MqttHelper.kt, MqttManager.kt, etc.).
– Integrates with HiveMQTT through native code exposing MQTT functionality to the React Native layer.

- Native iOS Module:
– Located in the "ios" directory.
– Written in Swift or Objective-C (files like MqttModule.mm, MqttDelegate.swift, MqttHelper.swift).
– Wraps CocoaMQTT, bridging the MQTT capabilities to the React Native context.

- Example Application:
– Under "example", shows how to configure and use the MQTT client from the React Native perspective.
– Provides real-world code demonstrating client creation, connection, subscription, and disconnection.

##### Data Flow & Interactions
- The React Native app (front-end) calls the exposed API functions (e.g., createMqttClient, connect, subscribe) from the JavaScript layer.
- These functions dispatch calls via the bridge to the native modules on both Android and iOS.
- The native modules internally interact with their respective MQTT libraries (HiveMQTT for Android and CocoaMQTT for iOS), handling tasks such as connection management, reconnection logic, QoS handling, and event callbacks.
- Results and events (like connection status changes or message events) are sent back through the bridge to the JavaScript layer where they are handled via callbacks.

##### Important Architectural Patterns & Design Principles
- Bridge Pattern: Connects the JavaScript/React Native layer with native modules on Android and iOS.
- Event-Driven Architecture: Implementation of various event callbacks such as onConnect, onError, onDisconnect, and subscription events, which are clearly visible in the API support section.
- Asynchronous & Reactive Design: Emphasis on the asynchronous API, backpressure support, and handling retries with interceptors.
- Modular Separation: Clean separation between platform-agnostic logic (JS/TS) and platform-specific implementations (native Android and iOS modules).

##### Relevant Technologies and Frameworks
- React Native and JavaScript/TypeScript for the core module.
- Native Android development with Java/Kotlin, using Gradle for builds.
- Native iOS development with Swift/Objective-C, using Xcode and CocoaPods (noted by the Podfile).
- External MQTT libraries: HiveMQTT and CocoaMQTT.
- Build and CI/CD Tools: Yarn, GitHub Actions workflows (ci.yml, publish.yml, release.yml), Jest for testing, and Turbo for monorepo/script management.

##### Detailing the Architecture
- React Native Client:
– Internal modules: src/Mqtt (handling client logic and event emission) and src/Modules/mqttModule.ts.
– API endpoints (createMqttClient, connect, disconnect, subscribe).

- Native Integration:
– Android: Showcase components like MqttManager and helper classes within android/src/main/java/com/dream11/mqtt that wrap HiveMQTT capabilities.
– iOS: Illustrate components like MqttModule and MqttDelegate in the ios directory that wrap CocoaMQTT.

- External Communications:
– Include an external entity for the MQTT Broker, noting protocols like TCP and SSL/TLS.

- Supporting Infrastructure:
– Optionally, add a block for the example app which demonstrates integration, and another for testing (Jest setup files) and CI (GitHub workflow files).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% React Native Layer
    subgraph "React Native Layer (JS/TS)"
        RN1["Mqtt Client (src/Mqtt)"]:::react
        RN2["MQTT Module API (src/Modules/mqttModule.ts)"]:::react
    end

    %% Native Android Module
    subgraph "Native Android Module"
        Android1["Mqtt Manager (MqttManager.kt)"]:::native
        Android2["MQTT Helper (MqttHelper.kt)"]:::native
        Android3["MQTT Options (MqttConnectOptions.kt)"]:::native
    end

    %% Native iOS Module
    subgraph "Native iOS Module"
        iOS1["Mqtt Module (MqttModule.mm)"]:::native
        iOS2["Mqtt Delegate (MqttDelegate.swift)"]:::native
        iOS3["MQTT Helper (MqttHelper.swift)"]:::native
    end

    %% Supporting Components
    subgraph "Supporting Components"
        EX["Example App (example)"]:::example
        Broker["MQTT Broker (TCP/SSL)"]:::external
    end

    %% Data Flow & Interactions
    EX -->|"integrates"| RN2
    RN2 -->|"bridge_call"| Android1
    RN2 -->|"bridge_call"| iOS1
    RN1 -->|"emits_events"| RN2
    Android1 -->|"network_request"| Broker
    iOS1 -->|"network_request"| Broker
    Android1 -->|"event_callback"| RN2
    iOS1 -->|"event_callback"| RN2

    %% Click Events
    click RN1 "https://github.com/dream-sports-labs/d11-react-native-mqtt/tree/main/src/Mqtt"
    click RN2 "https://github.com/dream-sports-labs/d11-react-native-mqtt/blob/main/src/Modules/mqttModule.ts"
    click Android1 "https://github.com/dream-sports-labs/d11-react-native-mqtt/tree/main/android/src/main/java/com/dream11/mqtt"
    click iOS1 "https://github.com/dream-sports-labs/d11-react-native-mqtt/tree/main/ios"
    click EX "https://github.com/dream-sports-labs/d11-react-native-mqtt/tree/main/example"

    %% Styles
    classDef react fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef native fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef external fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef example fill:#FFA07A,stroke:#333,stroke-width:2px;
```
