How dream-sports-labs/d11-react-native-mqtt works
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).