ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

MQTT

This module is a native module for the MQTT protocol, which is compatible with the MQTT.js.

Example

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

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

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

Class: MqttClient

new MqttClient(endpoint, options)

Creates a new client of endpoint.

client.connect()

Connects the specified before url by MQTT.

Event 'connect'

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

Event 'reconnect'

Emitted when a reconnect starts.

Event 'offline'

Emitted when the client goes offline.

Event 'error'

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

Event 'message'

Emitted when the client receives a publish packet.

Event 'packetsend'

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'

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.

client.publish(topic, message[, options[, callback]])

Publish a message to a topic.

client.subscribe(topic[, options[, callback]])

Subscribe to a topic.

Not support on topics array or object.

client.unsubscribe(topic[, callback])

Unsubscribe from a topic.

client.end([force[, callback]])

Close the client.

client.disconnect([err, [, callback]])

This method is deprecated.

Disconnect the client right away The client can safety be unref after call this method.

client.reconnect()

Disconnect current connection(if connected) and connect again using the same options as connect().

For more detailed, please see MQTT.js Readme.