mqtt-js-client
v0.0.5
Published
MQTT Client Wrapper for NodeJS
Downloads
93
Maintainers
Readme
MQTT Wrapper
Easy MQTT Client
Installation
npm install mqtt-js-client
How to use
Creates your MQTT client
MqttClient receives same arguments that MQTT Official Package for JS.
const MqttClient = require('mqtt-js-client')
let mc = new MqttClient('mqtt://mybroker', {
username: 'myMqttUser',
password: 'myMqttPassword',
});
#subscribe(topic, options, callback)
mc.subscribe('mqtt/topic', options, function (topic, messageBuffer, packet) {
console.log(messageBuffer.toString()); //Prints message as utf8
})
.then(granted => {
console.log('Subscrition granted:', granted)
})
MqttClient supports the MQTT Wildcards
mc.subscribe('mqtt/topic/#', options, function (topic, messageBuffer, packet) {
console.log(messageBuffer.toString()); //Prints message as utf8
})
mc.subscribe('mqtt/+/topic', options, function (topic, messageBuffer, packet) {
console.log(messageBuffer.toString()); //Prints message as utf8
})
#unsubscribe(topic, options)
Unsubscribe from a topic
mc.unsubscribe('mqtt/topic', options)
.then(success => {
console.log('Unsubcribe success!')
})
#publish(topic, message, options)
Publish a message
mc.publish('mqtt/topic', 'Hello world!', options)
.then(_ => {
console.log('Publish works!')
})
#end(force, options, callback)
Close connection
mc.end(false, options)
.then(_ => {
console.log('Connection closed!')
})
#removeOutgoingMessage(mId)
Remove a message from the outgoingStore. The outgoing callback will be called with Error('Message removed') if the message is removed.
mc.removeOutgoingMessage(messageId)
#reconnect()
Reconnects the client
mc.reconnect()
#handleMessage(packet, callback)
Handle messages with backpressure support, one at a time. Override at will, but always call callback, or the client will hang.
mc.handleMessage(packet, function () {
})
#isConnected()
Returns if client is connected
mc.isConnected()
#isReconnecting()
Returns if client is reconnecting
mc.isReconnecting()
#getLastMessageId()
Returns the last message id
mc.getLastMessageId()