@capgo/capacitor-mqtt

中文文档
8.1.16发布于 2026/10/15MPL-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 4万

Capacitor plugin for MQTT connectivity on Android and iOS

原始使用文档
文档目录23 节
  1. ➡️ Get started for free
  2. Key features
  3. Why MQTT?
  4. Features
  5. Installation
  6. Android cleartext MQTT (tcp://)
  7. Usage
  8. Connect to MQTT Broker
  9. Subscribe to Topic
  10. Publish Message
  11. Listen for Messages
  12. Disconnect
  13. API
  14. connect(...)
  15. disconnect()
  16. subscribe(...)
  17. publish(...)
  18. addListener('onConnectionLost', ...)
  19. addListener('onConnectComplete', ...)
  20. addListener('onMessageArrived', ...)
  21. Interfaces
  22. Type Aliases
  23. License

@capgo/capacitor-mqtt

Connect your Capacitor app to an MQTT broker on iOS and Android for real-time IoT messaging: publish, subscribe and react to messages as they arrive.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-mqtt in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-mqtt to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-mqtt for Capacitor apps

Key features

  • Connect: connect() with server URI, port, client ID, credentials, timeouts and automatic reconnect.
  • Publish: publish() sends a payload to a topic with QoS and retain options.
  • Subscribe: subscribe() to topics with a QoS level.
  • Events: onMessageArrived, onConnectComplete and onConnectionLost listeners.
  • Native clients: CocoaMQTT on iOS and Eclipse Paho on Android.
  • Platforms: iOS and Android. Not available on web.

NPM Version NPM Downloads GitHub Repo stars GitHub License Maintenance

Capacitor plugin for MQTT connectivity on Android and iOS using the Eclipse Paho MQTT library.

Why MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe messaging protocol ideal for:

  • IoT devices - Low bandwidth, minimal battery usage.
  • Real-time messaging - Instant message delivery between clients
  • Remote monitoring - Send/receive data from distributed devices
  • Home automation - Connect smart devices seamlessly

This plugin provides a complete MQTT client implementation for Capacitor apps, supporting both Android and iOS.


This plugin is compatible with Capacitor 8 and above.

PR's are greatly appreciated.

Features

  • Connect to MQTT brokers via TCP
  • Subscribe to topics with QoS support
  • Publish messages with QoS and retained flag options
  • Listen for incoming messages
  • Automatic reconnection support
  • Connection loss detection
  • Clean session management
  • Keep-alive interval configuration

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-mqtt` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-mqtt
npx cap sync

Android cleartext MQTT (tcp://)

Android blocks non-TLS MQTT (tcp://) by default. If your broker does not use TLS, add a network security config in your app:

<!-- android/app/src/main/res/xml/network_security_config.xml -->
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
<!-- android/app/src/main/AndroidManifest.xml -->
<application
    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config">

Without this, connect() rejects with a broker connection error instead of silently failing.

Usage

Connect to MQTT Broker

import { MqttBridge } from '@capgo/capacitor-mqtt';

const connectionOptions = {
  serverURI: 'tcp://broker.hivemq.com',
  port: 1883,
  clientId: 'my-client-id',
  username: '',
  password: '',
  setCleanSession: true,
  connectionTimeout: 30,
  keepAliveInterval: 60,
  setAutomaticReconnect: true,
};

await MqttBridge.connect(connectionOptions);

Subscribe to Topic

const result = await MqttBridge.subscribe({
  topic: 'my/topic',
  qos: 0,
});

Publish Message

const result = await MqttBridge.publish({
  topic: 'my/topic',
  payload: 'Hello World',
  qos: 0,
  retained: false,
});

Listen for Messages

import { MqttBridge } from '@capgo/capacitor-mqtt';

MqttBridge.addListener('onMessageArrived', (result) => {
  console.log('Topic:', result.topic);
  console.log('Message:', result.message);
});

Disconnect

await MqttBridge.disconnect();

API

connect(...)

connect(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; }; }) => Promise<any>
ParamType
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; }; }

Returns: Promise<any>


disconnect()

disconnect() => Promise<any>

Returns: Promise<any>


subscribe(...)

subscribe(options: { topic: string; qos: number; }) => Promise<{ topic: string; qos: number; }>
ParamType
options{ topic: string; qos: number; }

Returns: Promise<{ topic: string; qos: number; }>


publish(...)

publish(options: { topic: string; payload: string; qos: number; retained: boolean; }) => Promise<{ topic: string; payload: string; qos: number; retained: boolean; messageId: any; }>
ParamType
options{ topic: string; payload: string; qos: number; retained: boolean; }

Returns: Promise<{ topic: string; payload: string; qos: number; retained: boolean; messageId: any; }>


addListener('onConnectionLost', ...)

addListener(eventName: 'onConnectionLost', listener: onConnectionLostListener) => Promise<PluginListenerHandle>
ParamType
eventName'onConnectionLost'
listeneronConnectionLostListener

Returns: Promise<PluginListenerHandle>


addListener('onConnectComplete', ...)

addListener(eventName: 'onConnectComplete', listener: onConnectCompleteListener) => Promise<PluginListenerHandle>
ParamType
eventName'onConnectComplete'
listeneronConnectCompleteListener

Returns: Promise<PluginListenerHandle>


addListener('onMessageArrived', ...)

addListener(eventName: 'onMessageArrived', listener: onMessageArrivedListener) => Promise<PluginListenerHandle>
ParamType
eventName'onMessageArrived'
listeneronMessageArrivedListener

Returns: Promise<PluginListenerHandle>


Interfaces

PluginListenerHandle

PropType
remove() => Promise<void>

Type Aliases

onConnectionLostListener

(x: { connectionStatus: string; reasonCode: number; message: string; }): void

onConnectCompleteListener

(x: { reconnected: boolean; serverURI: string; }): void

onMessageArrivedListener

(x: { topic: string; message: string; }): void

License

MPL-2.0

版本与发布记录

@capgo/capacitor-mqtt 共发布 20 个版本,首次发布于 2026/02/28;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.16latest2026/10/1525 天前
8.1.152026/10/1327 天前
8.1.142026/10/091 个月前
8.1.132026/10/081 个月前
8.1.122026/09/161 个月前
8.1.112026/08/202 个月前

关于 @capgo/capacitor-mqtt 的常见问题

@capgo/capacitor-mqtt 是什么?

Capacitor plugin for MQTT connectivity on Android and iOS

如何安装 @capgo/capacitor-mqtt?

在项目目录运行 npm install @capgo/capacitor-mqtt,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-mqtt 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-mqtt 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@capgo/capacitor-mqtt 可以商用吗?

@capgo/capacitor-mqtt 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-mqtt 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-mqtt 在 2026/10/09 – 2026/11/07 这 30 天内被下载 40,239 次,日均约 1,341 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-mqtt 还在维护吗?

最新版本 8.1.16 发布于 2026/10/15(25 天前);至今共发布 20 个版本,其中过去 12 个月发布了 20 个;首次发布于 2026/02/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。