A Node.js MQTT client for managing secure MQTT connections with certificate-based authentication.
- Connect to MQTT brokers securely using TLS/SSL.
- Manage certificates with a built-in certificate manager.
- Easily configure and handle MQTT connections, subscriptions, and messages.
Install the package via npm:
npm install node-mqtt-client
This project makes use of the following libraries:
- fs: File system module to handle reading and writing of files.
- mqtt: MQTT.js library to handle MQTT connections and communication.
- node-forge: Library for creating and managing X.509 certificates.
Here's an example of how to use the MQTT client:
const { MQTTClient } = require('node-mqtt-client');
const mqttClient = new MQTTClient();
mqttClient.host = 'broker.fabris.io';
mqttClient.port = 8883;
mqttClient.hostProtocol = MQTTClient.Protocol.MQTTS;
mqttClient.certificateManager.loadCertificates(
'authority.pem',
'certificate.pem',
'key.pem'
);
mqttClient.connect();
Parameter | Type | Description | Default |
---|---|---|---|
host |
string |
The MQTT broker's host address. | 'your-mqtt-broker-host' |
port |
number |
The port to connect to on the MQTT broker. | 8883 |
hostProtocol |
string |
The protocol to use (mqtt , mqtts , ws , wss ). |
mqtts |
Connects to the MQTT broker using the loaded certificates.
Returns:
MQTTClient
- The instance of MQTTClient.
Subscribes to a topic or topics on the MQTT broker.
-
topic
(string|string[]|Object
): The topic(s) to subscribe to. Can be a single topic, an array of topics, or an object with topics as keys and QoS levels as values. -
options
(Object
): Optional parameters for the subscription.-
qos
(number
): QoS level for the subscription (0, 1, or 2). Default is 0. -
nl
(boolean
): No Local flag for MQTT 5.0. Default is false. -
rap
(boolean
): Retain as Published flag for MQTT 5.0. Default is false. -
rh
(number
): Retain Handling option for MQTT 5.0. Default is 0. -
properties
(Object
): MQTT 5.0 properties object.
-
-
callback
(Function
): The callback function to handle the subscription response.
Returns:
MQTTClient
- The instance of MQTTClient.
Unsubscribes from a topic or topics on the MQTT broker.
-
topic
(string|string[]
): The topic(s) to unsubscribe from. Can be a single topic (string) or an array of topics (string[]). -
options
(Object
): Optional parameters for the unsubscription.-
nosuffix
(boolean
): If set totrue
, does not prepend the common name to the topic.
-
-
callback
(Function
): The callback function to handle the unsubscription response. Called with(err, topic)
parameters.
Returns:
MQTTClient
- The instance of MQTTClient.
Publishes a message to a topic on the MQTT broker.
-
topic
(string
): The topic to publish to. -
message
(string|Buffer
): The message to publish. -
options
(Object
): Optional parameters for publishing.-
qos
(number
): QoS level (0, 1, or 2). Default is 0. -
retain
(boolean
): Retain flag. Default is false. -
dup
(boolean
): Mark as duplicate flag. Default is false. -
properties
(Object
): MQTT 5.0 properties object.
-
-
callback
(Function
): Callback called when QoS handling completes or an error occurs.
Returns:
MQTTClient
- The instance of MQTTClient.
Registers a callback for incoming messages.
-
callback
(Function
): The callback function to handle incoming messages.
Returns:
MQTTClient
- The instance of MQTTClient.
Loads certificates from the specified paths.
-
ca
(string
): Path to the CA certificate. -
certificate
(string
): Path to the client certificate. -
key
(string
): Path to the private key.
Returns:
CertificateManager
- The instance of CertificateManager.
Returns the loaded certificates.
Returns:
{ cert: Buffer, key: Buffer, ca: Buffer }
- The loaded certificates.
Subscribe to a topic to receive messages:
mqttClient.subscribe('your/topic', { qos: 1 }, (err, granted) => {
if (err) console.error('Subscription error:', err);
// Anything you want
});
Publish a message to a specific topic:
mqttClient.publish('your/topic', 'Hello MQTT!', { qos: 1, retain: true }, (err) => {
if (err) console.error('Error publishing message:', err);
// Anything you want
});
Register a callback to handle incoming messages:
mqttClient.onMessage((topic, message) => {
console.log(`Received message on topic ${topic}: ${message.toString()}`);
// Anything you want
});
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Paolo Fabris - Initial work - ubyte.it
See also the list of contributors who participated in this project.
This project is licensed under the MIT License. See the LICENSE file for details.