/ never-odd-or-even / back / node_modules / mqtt /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[DIR]dist/2 years ago -  
[DIR]bin/2 years ago -  
[DIR]lib/2 years ago -  
[DIR]types/2 years ago -  
[DIR]doc/2 years ago -  
[TXT]CONTRIBUTING.md40 years ago1.2K 
[TXT]LICENSE.md40 years ago1.2K 
[TXT]README.md40 years ago 34K 
[   ]mqtt.js40 years ago700  
[   ]package.json2 years ago4.1K 
README.md

mqtt.js

Github Test Status codecov

MQTT.js is a client library for the MQTT protocol, written in JavaScript for node.js and the browser.

:boom: MQTT.js vNext
Want to contribute to the next generation of MQTT.js? Join the discussion here.

Table of Contents

MQTT.js is an OPEN Open Source Project, see the Contributing section to find out what this means.

JavaScript Style
Guide

Discussion on the next major version of MQTT.js

There are discussions happening on the future of MQTT.js and the next major version (vNext). We invite the community to provide their thoughts and feedback in this GitHub discussion

Important notes for existing users

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 a 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.

Note: MQTT v5 support is experimental as it has not been implemented by brokers yet.

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

npm install mqtt --save

Example

For the sake of simplicity, let's put the subscriber and the publisher in the same file:

const mqtt = require('mqtt')
const client  = mqtt.connect('mqtt://test.mosquitto.org')

client.on('connect', function () {
  client.subscribe('presence', function (err) {
    if (!err) {
      client.publish('presence', 'Hello mqtt')
    }
  })
})

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString())
  client.end()
})

output:

Hello mqtt

If you want to run your own MQTT broker, you can use Mosquitto or Aedes-cli, and launch it.

You can also use a test instance: test.mosquitto.org.

If you do not want to install a separate broker, you can try using the Aedes.

to use MQTT.js in the browser see the browserify section

Import styles

CommonJS (Require)

const mqtt = require('mqtt')  // require mqtt
const client = mqtt.connect('est.mosquitto.org')  // create a client

ES6 Modules (Import)

Aliased wildcard import

import * as mqtt from "mqtt"  // import everything inside the mqtt module and give it the namespace "mqtt"
let client = mqtt.connect('mqtt://test.mosquitto.org') // create a client

Importing individual components

import { connect } from "mqtt"  // import connect from mqtt
let client = connect('mqtt://test.mosquitto.org') // create a client

Promise support

If you want to use the new async-await functionality in JavaScript, or just prefer using Promises instead of callbacks, async-mqtt is a wrapper over MQTT.js which uses promises instead of callbacks when possible.

Command Line Tools

MQTT.js bundles a command to interact with a broker. In order to have it available on your path, you should install MQTT.js globally:

npm install mqtt -g

Then, on one terminal

mqtt sub -t 'hello' -h 'test.mosquitto.org' -v

On another

mqtt pub -t 'hello' -h 'test.mosquitto.org' -m 'from MQTT.js'

See mqtt help <command> for the command help.

Debug Logs

MQTT.js uses the debug package for debugging purposes. To enable debug logs, add the following environment variable on runtime :

# (example using PowerShell, the VS Code default)
$env:DEBUG='mqttjs*'

About Reconnection

An important part of any websocket connection is what to do when a connection drops off and the client needs to reconnect. MQTT has built-in reconnection support that can be configured to behave in ways that suit the application.

Refresh Authentication Options / Signed Urls with transformWsUrl (Websocket Only)

When an mqtt connection drops and needs to reconnect, it's common to require that any authentication associated with the connection is kept current with the underlying auth mechanism. For instance some applications may pass an auth token with connection options on the initial connection, while other cloud services may require a url be signed with each connection.

By the time the reconnect happens in the application lifecycle, the original auth data may have expired.

To address this we can use a hook called transformWsUrl to manipulate either of the connection url or the client options at the time of a reconnect.

Example (update clientId & username on each reconnect):

    const transformWsUrl = (url, options, client) => {
      client.options.username = `token=${this.get_current_auth_token()}`;
      client.options.clientId = `${this.get_updated_clientId()}`;

      return `${this.get_signed_cloud_url(url)`;
    }

    const connection = await mqtt.connectAsync(<wss url>, {
      ...,
      transformWsUrl: transformUrl,
    });

Now every time a new WebSocket connection is opened (hopefully not too often), we will get a fresh signed url or fresh auth token data.

Note: Currently this hook does not support promises, meaning that in order to use the latest auth token, you must have some outside mechanism running that handles application-level authentication refreshing so that the websocket connection can simply grab the latest valid token or signed url.

Enabling Reconnection with reconnectPeriod option

To ensure that the mqtt client automatically tries to reconnect when the connection is dropped, you must set the client option reconnectPeriod to a value greater than 0. A value of 0 will disable reconnection and then terminate the final connection when it drops.

The default value is 1000 ms which means it will try to reconnect 1 second after losing the connection.

About Topic Alias Management

Enabling automatic Topic Alias using

If the client sets the option autoUseTopicAlias:true then MQTT.js uses existing topic alias automatically.

example scenario:

1. PUBLISH topic:'t1', ta:1                   (register)
2. PUBLISH topic:'t1'       -> topic:'', ta:1 (auto use existing map entry)
3. PUBLISH topic:'t2', ta:1                   (register overwrite)
4. PUBLISH topic:'t2'       -> topic:'', ta:1 (auto use existing map entry based on the receent map)
5. PUBLISH topic:'t1'                         (t1 is no longer mapped to ta:1)

User doesn't need to manage which topic is mapped to which topic alias. If the user want to register topic alias, then publish topic with topic alias. If the user want to use topic alias, then publish topic without topic alias. If there is a mapped topic alias then added it as a property and update the topic to empty string.

Enabling automatic Topic Alias assign

If the client sets the option autoAssignTopicAlias:true then MQTT.js uses existing topic alias automatically. If no topic alias exists, then assign a new vacant topic alias automatically. If topic alias is fully used, then LRU(Least Recently Used) topic-alias entry is overwritten.

example scenario:

The broker returns CONNACK (TopicAliasMaximum:3)
1. PUBLISH topic:'t1' -> 't1', ta:1 (auto assign t1:1 and register)
2. PUBLISH topic:'t1' -> ''  , ta:1 (auto use existing map entry)
3. PUBLISH topic:'t2' -> 't2', ta:2 (auto assign t1:2 and register. 2 was vacant)
4. PUBLISH topic:'t3' -> 't3', ta:3 (auto assign t1:3 and register. 3 was vacant)
5. PUBLISH topic:'t4' -> 't4', ta:1 (LRU entry is overwritten)

Also user can manually register topic-alias pair using PUBLISH topic:'some', ta:X. It works well with automatic topic alias assign.

API


mqtt.connect([url], options)

Connects to the broker specified by the given url and options and returns a Client.

The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp', 'tls', 'ws', 'wss', 'wxs', 'alis'. The URL can also be an object as returned by URL.parse(), in that case the two objects are merged, i.e. you can pass a single object with both the URL and the connect options.

You can also specify a servers options with content: [{ host: 'localhost', port: 1883 }, ... ], in that case that array is iterated at every connect.

For all MQTT-related options, see the Client constructor.


mqtt.Client(streamBuilder, options)

The Client class wraps a client connection to an MQTT broker over an arbitrary transport method (TCP, TLS, WebSocket, ecc).

Client automatically handles the following:

The arguments are:

In case mqtts (mqtt over tls) is required, the options object is passed through to tls.connect(). If you are using a self-signed certificate, pass the rejectUnauthorized: false option. Beware that you are exposing yourself to man in the middle attacks, so it is a configuration that is not recommended for production environments.

If you are connecting to a broker that supports only MQTT 3.1 (not 3.1.1 compliant), you should pass these additional options:

{
  protocolId: 'MQIsdp',
  protocolVersion: 3
}

This is confirmed on RabbitMQ 3.2.4, and on Mosquitto < 1.3. Mosquitto version 1.3 and 1.4 works fine without those.

Event 'connect'

function (connack) {}

Emitted on successful (re)connection (i.e. connack rc=0).

Event 'reconnect'

function () {}

Emitted when a reconnect starts.

Event 'close'

function () {}

Emitted after a disconnection.

Event 'disconnect'

function (packet) {}

Emitted after receiving disconnect packet from broker. MQTT 5.0 feature.

Event 'offline'

function () {}

Emitted when the client goes offline.

Event 'error'

function (error) {}

Emitted when the client cannot connect (i.e. connack rc != 0) or when a parsing error occurs.

The following TLS errors will be emitted as an error event:

Event 'end'

function () {}

Emitted when mqtt.Client#end() is called. If a callback was passed to mqtt.Client#end(), this event is emitted once the callback returns.

Event 'message'

function (topic, message, packet) {}

Emitted when the client receives a publish packet

Event 'packetsend'

function (packet) {}

Emitted when the client sends any packet. This includes .published() packets as well as packets used by MQTT for managing subscriptions and connections

Event 'packetreceive'

function (packet) {}

Emitted when the client receives any packet. This includes packets from subscribed topics as well as packets used by MQTT for managing subscriptions and connections


mqtt.Client#publish(topic, message, [options], [callback])

Publish a message to a topic


mqtt.Client#subscribe(topic/topic array/topic object, [options], [callback])

Subscribe to a topic or topics


mqtt.Client#unsubscribe(topic/topic array, [options], [callback])

Unsubscribe from a topic or topics


mqtt.Client#end([force], [options], [callback])

Close the client, accepts the following options:


mqtt.Client#removeOutgoingMessage(mId)

Remove a message from the outgoingStore. The outgoing callback will be called with Error('Message removed') if the message is removed.

After this function is called, the messageId is released and becomes reusable.


mqtt.Client#reconnect()

Connect again using the same options as connect()


mqtt.Client#handleMessage(packet, callback)

Handle messages with backpressure support, one at a time. Override at will, but always call callback, or the client will hang.


mqtt.Client#connected

Boolean : set to true if the client is connected. false otherwise.


mqtt.Client#getLastMessageId()

Number : get last message id. This is for sent messages only.


mqtt.Client#reconnecting

Boolean : set to true if the client is trying to reconnect to the server. false otherwise.


mqtt.Store(options)

In-memory implementation of the message store.

Other implementations of mqtt.Store:


mqtt.Store#put(packet, callback)

Adds a packet to the store, a packet is anything that has a messageId property. The callback is called when the packet has been stored.


mqtt.Store#createStream()

Creates a stream with all the packets in the store.


mqtt.Store#del(packet, cb)

Removes a packet from the store, a packet is anything that has a messageId property. The callback is called when the packet has been removed.


mqtt.Store#close(cb)

Closes the Store.

Browser

Via CDN

The MQTT.js bundle is available through http://unpkg.com, specifically at https://unpkg.com/mqtt/dist/mqtt.min.js. See http://unpkg.com for the full documentation on version ranges.

Browserify

In order to use MQTT.js as a browserify module you can either require it in your browserify bundles or build it as a stand alone module. The exported module is AMD/CommonJs compatible and it will add an object in the global space.

mkdir tmpdir
cd tmpdir
npm install mqtt
npm install browserify
npm install tinyify
cd node_modules/mqtt/
npm install .
npx browserify mqtt.js -s mqtt >browserMqtt.js // use script tag
# show size for compressed browser transfer
gzip <browserMqtt.js | wc -c

Be sure to only use this bundle with ws or wss URLs in the browser. Others URL types will likey fail

Webpack

Just like browserify, export MQTT.js as library. The exported module would be const mqtt = xxx and it will add an object in the global space. You could also export module in other formats (AMD/CommonJS/others) by setting output.libraryTarget in webpack configuration.

npm install -g webpack // install webpack

cd node_modules/mqtt
npm install . // install dev dependencies
webpack mqtt.js ./browserMqtt.js --output-library mqtt

you can then use mqtt.js in the browser with the same api than node's one.

<html>
<head>
  <title>test Ws mqtt.js</title>
</head>
<body>
<script src="./browserMqtt.js"></script>
<script>
  const client = mqtt.connect() // you add a ws:// url here
  client.subscribe("mqtt/demo")

  client.on("message", function (topic, payload) {
    alert([topic, payload].join(": "))
    client.end()
  })

  client.publish("mqtt/demo", "hello world!")
</script>
</body>
</html>

React

npm install -g webpack                    // Install webpack globally
npm install mqtt                          // Install MQTT library
cd node_modules/mqtt
npm install .                             // Install dev deps at current dir
webpack mqtt.js --output-library mqtt     // Build

// now you can import the library with ES6 import, commonJS not tested
import React from 'react';
import mqtt from 'mqtt';

export default () => {
  const [connectionStatus, setConnectionStatus] = React.useState(false);
  const [messages, setMessages] = React.useState([]);

  useEffect(() => {
    const client = mqtt.connect(SOME_URL);
    client.on('connect', () => setConnectionStatus(true));
    client.on('message', (topic, payload, packet) => {
      setMessages(messages.concat(payload.toString()));
    });
  }, []);

  return (
    <>
     {messages.map((message) => (
        <h2>{message}</h2>
     )
    </>
  )
}

Your broker should accept websocket connection (see MQTT over Websockets to setup Aedes).

About QoS

Here is how QoS works:

About data consumption, obviously, QoS 2 > QoS 1 > QoS 0, if that's a concern to you.

Usage with TypeScript

This repo bundles TypeScript definition files for use in TypeScript projects and to support tools that can read .d.ts files.

Pre-requisites

Before you can begin using these TypeScript definitions with your project, you need to make sure your project meets a few of these requirements:

Typescript example

import * as mqtt from "mqtt"
let client : mqtt.MqttClient = mqtt.connect('mqtt://test.mosquitto.org')

WeChat and Ali Mini Program support

WeChat Mini Program

Supports WeChat Mini Program. Use the wxs protocol. See the WeChat docs.

const mqtt = require('mqtt')
const client = mqtt.connect('wxs://test.mosquitto.org')

Ali Mini Program

Supports Ali Mini Program. Use the alis protocol. See the Alipay docs.

const mqtt = require('mqtt')
const client = mqtt.connect('alis://test.mosquitto.org')

Contributing

MQTT.js is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the CONTRIBUTING.md file for more details.

Contributors

MQTT.js is only possible due to the excellent work of the following contributors:

Adam RuddGitHub/adamvrTwitter/@adam_vr
Matteo CollinaGitHub/mcollinaTwitter/@matteocollina
Maxime AgorGitHub/4rzaelTwitter/@4rzael
Siarhei BuntsevichGitHub/scarry1992

License

MIT

Apache/2.4.38 (Debian) Server at www.karls.computer Port 80