{"id":16017152,"url":"https://github.com/kufayeka/capacitorjs-mqtt-native-plugin","last_synced_at":"2025-07-29T16:14:21.261Z","repository":{"id":71284401,"uuid":"604501944","full_name":"kufayeka/capacitorjs-mqtt-native-plugin","owner":"kufayeka","description":"⚡️Capacitor plugin library that enables your CapacitorJS-powered Android mobile app to connect to an MQTT broker and send/receive messages. With this plugin, you can easily implement MQTT-based communication in your Android CapacitorJS app natively using TCP protocol.","archived":false,"fork":false,"pushed_at":"2024-05-15T13:47:41.000Z","size":459,"stargazers_count":6,"open_issues_count":1,"forks_count":6,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-07-09T14:43:38.327Z","etag":null,"topics":["android","capacitor","capacitor-plugin","java","javascript","mqtt","mqtt-client","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/capacitor-mqtt-native-plugin","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kufayeka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-21T07:35:57.000Z","updated_at":"2025-05-21T02:04:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"95455819-e6ad-4f51-885e-3142481bbe08","html_url":"https://github.com/kufayeka/capacitorjs-mqtt-native-plugin","commit_stats":{"total_commits":31,"total_committers":2,"mean_commits":15.5,"dds":"0.16129032258064513","last_synced_commit":"c518115d87aac004fac63f5edb5c79b0966498de"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kufayeka/capacitorjs-mqtt-native-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kufayeka%2Fcapacitorjs-mqtt-native-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kufayeka%2Fcapacitorjs-mqtt-native-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kufayeka%2Fcapacitorjs-mqtt-native-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kufayeka%2Fcapacitorjs-mqtt-native-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kufayeka","download_url":"https://codeload.github.com/kufayeka/capacitorjs-mqtt-native-plugin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kufayeka%2Fcapacitorjs-mqtt-native-plugin/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267717969,"owners_count":24133443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","capacitor","capacitor-plugin","java","javascript","mqtt","mqtt-client","typescript"],"created_at":"2024-10-08T16:04:10.964Z","updated_at":"2025-07-29T16:14:21.210Z","avatar_url":"https://github.com/kufayeka.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CapacitorJS MQTT Native Plugin\n\n⚡️ This plugin enabzles CapacitorJS-powered Android mobile apps to connect to an MQTT broker and send/receive messages natively using TCP protocol.\n\n#### ⚠️ Note: Supports only for android for now.\n\n## Installation\n\nTo install the plugin, run:\n\n```bash\nnpm install capacitor-mqtt-native-plugin\nnpx cap sync\n```\n\n## Examples\n\nHere are some examples of how to use the plugin in your capacitorJS project using Typescript:\n\n- [Connect to an MQTT Broker](#connect-to-broker)\n- [Disconnecting from the MQTT Broker](#disconnect-from-broker)\n- [Subscribing to an MQTT Topic](#subscribe-to-topic)\n- [Publishing a Message to an MQTT Topic](#publish-to-topic)\n- [Listen to Incoming Messages](#message-arrived-event)\n- [Listen to ConnectComplete Event](#connect-complete-event)\n- [Listen to ConnectionLost Event](#connection-lost-event)\n\n### Connect to an MQTT Broker : \u003ca name=\"connect-to-broker\"\u003e\u003c/a\u003e\n\nTo connect to an MQTT broker, you can use the `connect()` method provided by the plugin. The following code demonstrates how to connect to an MQTT broker:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Set the MQTT server connection options\nconst connectionOptions = {\n  serverURI: 'tcp://', // MQTT broker URI\n  port: 1883, // MQTT broker default port\n  clientId: 'your_mqtt_clientId', // client ID for connection\n  username: 'your_mqtt_broker_username', // MQTT broker username\n  password: 'your_mqtt_broker_password', // MQTT broker password\n  setCleanSession: true, // clean session option\n  connectionTimeout: 30, // connection timeout in seconds\n  keepAliveInterval: 60, // keep alive interval in seconds\n  setAutomaticReconnect: true, // automatic reconnect option\n};\n\n// connect to MQTT broker with options\nMqttBridge.connect(connectionOptions)\n  .then(() =\u003e {\n    // connection successful\n    console.log('Connect Success');\n  })\n  .catch((error: any) =\u003e {\n    // connection failed with error message\n    console.log('Connect Failed:', error);\n  });\n```\n\nyou can also add optional connect options parameter: `lastWill` to the `connectOptions`:\n\n```typescript\n  setLastWill: {\n    willTopic: \"your_last_will_topic\",\n    willPayload: \"your_last_will_message\",\n    willQoS: \"your_last_will_QoS\",\n    setRetained: true,\n  }\n```\n\n### Disconnecting from the MQTT Broker : \u003ca name=\"disconnect-from-broker\"\u003e\u003c/a\u003e\n\nTo disconnect from the MQTT broker, you can use the `disconnect()` method provided by the plugin. The following code demonstrates how to disconnect from an MQTT broker:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Disconnect from the MQTT broker\nMqttBridge.disconnect()\n  .then(() =\u003e {\n    // The disconnection is successful\n    console.log('Successfully disconnected from the MQTT broker');\n  })\n  .catch((errorMessage: any) =\u003e {\n    // The disconnection fails\n    console.log(\n      'Failed to disconnect from the MQTT broker. Error:',\n      errorMessage,\n    );\n  });\n```\n\n### Subscribing to an MQTT Topic : \u003ca name=\"#subscribe-to-topic\"\u003e\u003c/a\u003e\n\nTo subscribe to an MQTT topic, you can use the `subscribe()` method provided by the plugin. The following code demonstrates how to subscribe to an MQTT topic:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Define the topic, qos\nconst topic = 'your_mqtt_topic';\nconst qos = 0;\n\n// Subscribe to an MQTT topic\nMqttBridge.subscribe({ topic, qos })\n  // The subscription is successful\n  .then((result: any) =\u003e {\n    console.log('Successfully subscribed to topic:');\n    console.log('Topic:', result.topic);\n    console.log('QoS:', result.qos);\n  })\n  // The subscription fails\n  .catch((errorMessage: any) =\u003e {\n    console.log('Failed to subscribe to topic. Error:', errorMessage);\n  });\n```\n\n### Publishing a Message to an MQTT Topic : \u003ca name=\"publish-to-topic\"\u003e\u003c/a\u003e\n\nTo publish a message to an MQTT topic, you can use the `publish()` method provided by the plugin. The following code demonstrates how to publish a message to an MQTT topic:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Define the topic, payload, qos, and retained properties for the message\nconst topic = 'your_mqtt_topic';\nconst payload = 'your_mqtt_message';\nconst qos = 0;\nconst retained = false;\n\n// Publish the message\nMqttBridge.publish({ topic, payload, qos, retained })\n  .then((result: any) =\u003e {\n    // The message is published successfully\n    console.log('Successfully published message:');\n    console.log('Topic:', result.topic);\n    console.log('QoS:', result.qos);\n    console.log('Payload:', result.payload);\n    console.log('Retained:', result.retained);\n    console.log('Message ID:', result.messageId);\n  })\n  .catch((errorMessage: any) =\u003e {\n    // The message fails to publish\n    console.log('Failed to publish message. Error:', errorMessage);\n  });\n```\n\n### Listen to Incoming Messages : \u003ca name=\"message-arrived-event\"\u003e\u003c/a\u003e\n\nTo listen to incoming messages, you can add a CapacitorJS listener with this event name : `onMessageArrived`. The following code demonstrates how to publish a message to an MQTT topic:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Listen to incoming MQTT messages\nMqttBridge.addListener('onMessageArrived', (result: any) =\u003e {\n  console.log('Received a new message:');\n  console.log('Topic:', result.topic);\n  console.log('Message:', result.message);\n});\n```\n\nWhen a message arrives, the listener will be triggered and you can access the message topic and payload in the result parameter. You can modify the code to suit your use case and do something more interesting with the incoming messages.\n\n### Listen to ConnectComplete Event : \u003ca name=\"connect-complete-event\"\u003e\u003c/a\u003e\n\nThis event is triggered only when the connection to the MQTT broker is successfully completed. It also triggers when the client was reconnected after a connection loss. To implement this, you can add a CapacitorJS listener with the event name : `onConnectComplete`. The following code demonstrates how to listen to the ConnectComplete event:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Listen for the 'onConnectComplete' event\nMqttBridge.addListener('onConnectComplete', (result: any) =\u003e {\n  console.log('Successfully connected to MQTT broker:');\n  console.log('Reconnected:', result.reconnected);\n  console.log('Server URI:', result.serverURI);\n});\n```\n\n### Listen to ConnectionLost Event : \u003ca name=\"connection-lost-event\"\u003e\u003c/a\u003e\n\nThis event is triggered only when the client loses the connection to the MQTT broker. To handle this event, you can add a CapacitorJS listener with the event name : `onConnectionLost`. The following code demonstrates how to listen to ConnectionLost event:\n\n```typescript\nimport { MqttBridge } from 'capacitor-mqtt-native-plugin';\n\n// Add a listener for when the connection is lost\nMqttBridge.addListener('onConnectionLost', (result: any) =\u003e {\n  console.log('Connection lost:');\n  console.log('Connection status:', result.connectionStatus);\n  console.log('Reason code:', result.reasonCode);\n  console.log('Message:', result.message);\n});\n```\n\nThe event listener function receives an object result as an argument with the following properties:\n\n- **connectionStatus:** The status of the connection at the time the event was triggered.\n- **reasonCode:** The MQTT reason code for the connection loss.\n- **message:** Additional information about the connection loss.\n\n#\n\n## API\n\n\u003cdocgen-index\u003e\n\n- [`connect(...)`](#connect)\n- [`disconnect()`](#disconnect)\n- [`subscribe(...)`](#subscribe)\n- [`publish(...)`](#publish)\n- [`addListener('onConnectionLost', ...)`](#addlisteneronconnectionlost)\n- [`addListener('onConnectComplete', ...)`](#addlisteneronconnectcomplete)\n- [`addListener('onMessageArrived', ...)`](#addlisteneronmessagearrived)\n- [Interfaces](#interfaces)\n- [Type Aliases](#type-aliases)\n\n\u003c/docgen-index\u003e\n\n\u003cdocgen-api\u003e\n\u003c!--Update the source file JSDoc comments and rerun docgen to update the docs below--\u003e\n\n### connect(...)\n\n```typescript\nconnect(options: { serverURI: string; port: number; clientId: string; username: string; password: string; setCleanSession: boolean; connectionTimeout: number; keepAliveInterval: number; setAutomaticReconnect: boolean; setLastWill?: { willTopic: string; willPayload: string; willQoS: number; setRetained: boolean; }; }) =\u003e Promise\u003cany\u003e\n```\n\n| Param         | Type                                                                                                                                                                                                                                                                                                                      |\n| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| **`options`** | \u003ccode\u003e{ serverURI: string; port: number; clientId: string; username: string; password: string; setCleanSession: boolean; connectionTimeout: number; keepAliveInterval: number; setAutomaticReconnect: boolean; setLastWill?: { willTopic: string; willPayload: string; willQoS: number; setRetained: boolean; }; }\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;any\u0026gt;\u003c/code\u003e\n\n---\n\n### disconnect()\n\n```typescript\ndisconnect() =\u003e Promise\u003cany\u003e\n```\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;any\u0026gt;\u003c/code\u003e\n\n---\n\n### subscribe(...)\n\n```typescript\nsubscribe(options: { topic: string; qos: number; }) =\u003e Promise\u003c{ topic: string; qos: number; }\u003e\n```\n\n| Param         | Type                                         |\n| ------------- | -------------------------------------------- |\n| **`options`** | \u003ccode\u003e{ topic: string; qos: number; }\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;{ topic: string; qos: number; }\u0026gt;\u003c/code\u003e\n\n---\n\n### publish(...)\n\n```typescript\npublish(options: { topic: string; payload: string; qos: number; retained: boolean; }) =\u003e Promise\u003c{ topic: string; payload: string; qos: number; retained: boolean; messageId: any; }\u003e\n```\n\n| Param         | Type                                                                             |\n| ------------- | -------------------------------------------------------------------------------- |\n| **`options`** | \u003ccode\u003e{ topic: string; payload: string; qos: number; retained: boolean; }\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;{ topic: string; payload: string; qos: number; retained: boolean; messageId: any; }\u0026gt;\u003c/code\u003e\n\n---\n\n### addListener('onConnectionLost', ...)\n\n```typescript\naddListener(eventName: 'onConnectionLost', listener: onConnectionLostListener) =\u003e Promise\u003cPluginListenerHandle\u003e \u0026 PluginListenerHandle\n```\n\n| Param           | Type                                                                          |\n| --------------- | ----------------------------------------------------------------------------- |\n| **`eventName`** | \u003ccode\u003e'onConnectionLost'\u003c/code\u003e                                               |\n| **`listener`**  | \u003ccode\u003e\u003ca href=\"#onconnectionlostlistener\"\u003eonConnectionLostListener\u003c/a\u003e\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;\u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u0026gt; \u0026 \u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u003c/code\u003e\n\n---\n\n### addListener('onConnectComplete', ...)\n\n```typescript\naddListener(eventName: 'onConnectComplete', listener: onConnectCompleteListener) =\u003e Promise\u003cPluginListenerHandle\u003e \u0026 PluginListenerHandle\n```\n\n| Param           | Type                                                                            |\n| --------------- | ------------------------------------------------------------------------------- |\n| **`eventName`** | \u003ccode\u003e'onConnectComplete'\u003c/code\u003e                                                |\n| **`listener`**  | \u003ccode\u003e\u003ca href=\"#onconnectcompletelistener\"\u003eonConnectCompleteListener\u003c/a\u003e\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;\u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u0026gt; \u0026 \u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u003c/code\u003e\n\n---\n\n### addListener('onMessageArrived', ...)\n\n```typescript\naddListener(eventName: 'onMessageArrived', listener: onMessageArrivedListener) =\u003e Promise\u003cPluginListenerHandle\u003e \u0026 PluginListenerHandle\n```\n\n| Param           | Type                                                                          |\n| --------------- | ----------------------------------------------------------------------------- |\n| **`eventName`** | \u003ccode\u003e'onMessageArrived'\u003c/code\u003e                                               |\n| **`listener`**  | \u003ccode\u003e\u003ca href=\"#onmessagearrivedlistener\"\u003eonMessageArrivedListener\u003c/a\u003e\u003c/code\u003e |\n\n**Returns:** \u003ccode\u003ePromise\u0026lt;\u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u0026gt; \u0026 \u003ca href=\"#pluginlistenerhandle\"\u003ePluginListenerHandle\u003c/a\u003e\u003c/code\u003e\n\n---\n\n### Interfaces\n\n#### PluginListenerHandle\n\n| Prop         | Type                                      |\n| ------------ | ----------------------------------------- |\n| **`remove`** | \u003ccode\u003e() =\u0026gt; Promise\u0026lt;void\u0026gt;\u003c/code\u003e |\n\n### Type Aliases\n\n#### onConnectionLostListener\n\n\u003ccode\u003e(x: { connectionStatus: string; reasonCode: number; message: string; }): void\u003c/code\u003e\n\n#### onConnectCompleteListener\n\n\u003ccode\u003e(x: { reconnected: boolean; serverURI: string; }): void\u003c/code\u003e\n\n#### onMessageArrivedListener\n\n\u003ccode\u003e(x: { topic: string; message: string; }): void\u003c/code\u003e\n\n\u003c/docgen-api\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkufayeka%2Fcapacitorjs-mqtt-native-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkufayeka%2Fcapacitorjs-mqtt-native-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkufayeka%2Fcapacitorjs-mqtt-native-plugin/lists"}