mqttjs /MQTT.js

The MQTT client for Node.js and the browser

★ 9,131⑂ 1,451👁 217⑃ 777● 1,226▤ 197
TypeScriptNOASSERTIONCreated 2011-04-05
Download: Release 5.16.1Release-5.16.1 ↓5.16.12026-10-14
+0
Daily
9,131
stars
1,451
forks
217
watchers
777
prs
1,226
issues
197
releases

README

!mqtt.js

 




![node !npm](https://www.npmjs.com/package/mqtt)


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("prese

Top issues

Discussions

Latest release

Release 5.16.1 v5.16.1 2026-10-14
5.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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON