+0
日增长
9,131
stars
1,451
forks
217
watchers
777
prs
1,226
issues
197
releases
README
!mqtt.js

MQTT.js is a client library for the MQTT protocol, written
in JavaScript for node.js and the browser.
Table of Contents
- Upgrade notes
- Installation
- Example
- React Native
- Import Styles
- Command Line Tools
- API
- Browser
- About QoS
- TypeScript
- Weapp and Ali support
- Contributing
- Sponsor
- License
MQTT.js is an OPEN Open Source Project, see the Contributing section to find out what this means.
Important notes for existing users
v5.0.0 (07/2023)
- Removes support for all end of life node versions (v12 and v14), and now supports node v18 and v20.
- Completely rewritten in Typescript 🚀.
- When creating MqttClient instance new is now required.
v4.0.0 (Released 04/2020) removes support for all end of life node versions, and now supports node v12 and v14. It also adds improvements to
debug logging, along with some feature additions.
As a breaking change, by default an error handler is built into the MQTT.js client, so if any
errors are emitted and the user has not created an event handler on the client for errors, the client will
not break as a result of unhandled errors. Additionally, typical TLS errors like ECONNREFUSED, ECONNRESET have been
added to a list of TLS errors that will be emitted from the MQTT.js client, and so can be handled as connection errors.
v3.0.0 adds support for MQTT 5, support for node v10.x, and many fixes to improve reliability.
v2.0.0 removes support for node v0.8, v0.10 and v0.12, and it is 3x faster in sending
packets. It also removes all the deprecated functionality in v1.0.0,
mainly mqtt.createConnection and mqtt.Server. From v2.0.0,
subscriptions are restored upon reconnection if clean: true.
v1.x.x is now in _LTS_, and it will keep being supported as long as
there are v0.8, v0.10 and v0.12 users.
As a breaking change, the encoding option in the old client is
removed, and now everything is UTF-8 with the exception of the
password in the CONNECT message and payload in the PUBLISH message,
which are Buffer.
Another breaking change is that MQTT.js now defaults to MQTT v3.1.1,
so to support old brokers, please read the client options doc.
v1.0.0 improves the overall architecture of the project, which is now
split into three components: MQTT.js keeps the Client,
mqtt-connection includes the barebone
Connection code for server-side usage, and mqtt-packet
includes the protocol parser and generator. The new Client improves
performance by a 30% factor, embeds Websocket support
(MOWS is now deprecated), and it has a better
support for QoS 1 and 2. The previous API is still supported but
deprecated, as such, it is not documented in this README.
Installation
sh
npm install mqtt --save
Example
For the sake of simplicity, let's put the subscriber and the publisher in the same file:
js
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://test.mosquitto.org");
client.on("connect", () = {
client.subscribe("preseTop issues
#828TransformUrl function returns a async promise instead of new url string22 comments · 2018-05-30#2002Architectural improvements18 comments · 2025-06-09#2038Roadmap: MQTT.js v6 — Zero/minimal dependencies, tree-shakeable transports, reduced bundle size15 comments · 2026-02-18#1569When connecting `ws://[::1]:8883`, an error is thrown - Uncaught TypeError TypeError [ERR_INVALID_URL]: Invalid URL13 comments · 2023-01-16#1939[Bug]: Errors do not result in an error event10 comments · 2024-09-20#1795[feat] Add support for MQTT 5 refresh auth9 comments · 2024-02-26#710end() callback not called if `force` is true and the stream is already closed (eg. after a failed keepalive ping)9 comments · 2017-11-04#1651Problem of sending outgoingStore data even though CONNACK sessionPresent is false8 comments · 2023-07-27
Discussions
#2032No failure of publish in case of broker down3 comments · 2025-11-28#2029How to prevent duplicate connect when reload browser2 comments · 2025-10-15#2027Can React Native connect to TCP MQTT?1 comments · 2025-09-28#2021Any one else getting massive errors after upgrading to 5.14.0 ?1 comments · 2025-08-11#2014Missing `url` dependency?1 comments · 2025-07-26#2000Architectural improvements12 comments · 2025-05-30#1998Guidance for Using MQTT.js in IoT-Based Web App (New to JavaScript)1 comments · 2025-05-15#1997MQTT frequent connection and disconnection1 comments · 2025-05-12
Latest release
Release 5.16.1 v5.16.1 2026-10-145.16.1 (2026-10-14) Bug Fixes reconnect instead of ending a client whose stream is already destroyed (2068) (569bb31) schedule browser process.nextTick with queueMicrotask (2056) (648b79c), closes 2074