Skip to main content
@capgo/capacitor-mqtt Communication Open source

MQTT Capacitor plugin

MQTT support for real-time messaging across iOS, Android, and Web.

Install

bun add @capgo/capacitor-mqtt bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-mqtt
  • pnpm add @capgo/capacitor-mqtt
  • yarn add @capgo/capacitor-mqtt

Guide

How to use MQTT in Capacitor

Test on device

Download the Capgo app, then scan the QR code.

MQTT plugin preview QR code

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.

FAQ

MQTT plugin FAQ

How do I install the MQTT plugin in a Capacitor app?

Run "bun add @capgo/capacitor-mqtt" (or "npm install @capgo/capacitor-mqtt"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-mqtt" in your app code.

Does @capgo/capacitor-mqtt work with React, Vue and Angular?

Yes. MQTT is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-mqtt support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-mqtt free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-mqtt/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses MQTT without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship MQTT changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo