Using @capgo/capacitor-mqtt
Capacitor plugin for MQTT connectivity on Android and iOS.
Install
bun add @capgo/capacitor-mqtt
bunx cap sync
bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.
Import
import { MqttBridge } from '@capgo/capacitor-mqtt';
API at a glance
| Method | Description |
|---|---|
connect |
See the source definitions for current behavior. |
disconnect |
See the source definitions for current behavior. |
subscribe |
See the source definitions for current behavior. |
publish |
See the source definitions for current behavior. |
Examples
connect()
See the API reference for the current contract.
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.connect({
serverURI: 'https://example.com',
port: 8080,
clientId: 'client-id-123',
username: 'username',
password: 'password',
setCleanSession: true,
connectionTimeout: 1000,
keepAliveInterval: 1000,
setAutomaticReconnect: true,
});
console.log(result);
disconnect()
See the API reference for the current contract.
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.disconnect();
console.log(result);
subscribe()
See the API reference for the current contract.
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.subscribe({
topic: 'topic',
qos: 1,
});
console.log(result);
publish()
See the API reference for the current contract.
import { MqttBridge } from '@capgo/capacitor-mqtt';
const result = await MqttBridge.publish({
topic: 'topic',
payload: 'payload',
qos: 1,
retained: true,
});
console.log(result);
Listen to events
addListener returns a handle. Call handle.remove() when the screen unmounts.
Full reference
Keep going from Using @capgo/capacitor-mqtt
If you are using Using @capgo/capacitor-mqtt to plan native plugin work, connect it with @capgo/capacitor-mqtt for the implementation detail in @capgo/capacitor-mqtt, Getting Started for the implementation detail in Getting Started, Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins.