Communicate using MQTT topics
更新时间:
复制 MD 格式
The SDK provides basic interfaces for long-lived connections to the cloud. You can use these interfaces to implement features related to custom topics, such as publishing, subscribing, and unsubscribing.
Publish
- API prototype: device#publish(topic, message, [options], [callback])
For more information about the API prototype, see API Description in the Authentication and Connection document.
- Function: Sends a message to a specified topic.
- Parameters
Parameter Description topic The specified topic. The data type is String. message The message to send. The data type is Buffer or String. options Optional. For more information, see mqtt qos The Quality of Service (QoS) level. Valid values:- 0 (default)
- 1
- 2
callback The callback function that is run after the message is published. The parameter is error. This parameter exists only if the message fails to be published.
Example code: (Ensure that the product contains the topic and that the topic has the Publish permission.)
const iot = require('alibabacloud-iot-device-sdk');
// Creating an iot.device object initiates a connection to Alibaba Cloud IoT.
const device = iot.device({
productKey: `${productKey}`, // Replace ${productKey} with the actual ProductKey of your product.
deviceName: `${deviceName}`,// Replace ${deviceName} with the actual DeviceName of your device.
deviceSecret: `${deviceSecret}`,// Replace ${deviceSecret} with the actual DeviceSecret of your device.
});
//To publish a message with QoS 0
device.publish('/<productKey>/<deviceName>/user/update', 'hello world!');
//To publish a message with QoS 1
device.publish('/<productKey>/<deviceName>/user/update', 'hello world!',{qos:1});
//To publish a Buffer
device.publish('/<productKey>/<deviceName>/user/update', new Buffer([0,1,2,3,4])); Subscribe
- API prototype: device#subscribe(topic/topic array/topic object, [options], [callback])
- Function: Subscribes to messages from a specified topic.
- Parameters
Parameter Description topic The specified topic. The data format is String. options Optional. qos The QoS level. The default value is 0. callback The callback function that is run when a SUBACK packet is received. error The error message. granted {topic, qos}.
Example code: (Ensure that the product contains the topic and that the topic has the Subscribe permission.)
const iot = require('alibabacloud-iot-device-sdk');
// Creating an iot.device object initiates a connection to Alibaba Cloud IoT.
const device = iot.device({
productKey: `${productKey}`, // Replace ${productKey} with the actual ProductKey of your product.
deviceName: `${deviceName}`,// Replace ${deviceName} with the actual DeviceName of your device.
deviceSecret: `${deviceSecret}`,// Replace ${deviceSecret} with the actual DeviceSecret of your device.
});
// Subscribe to a specified topic.
device.subscribe('/<productKey>/<deviceName>/user/get');
//device.subscribe('/<productKey>/<deviceName>/user/get',{qos:1});
// Print the topic and message when data is received.
device.on('message', (topic, payload) => {
console.log(topic, payload.toString());
}); Reference code: https://github.com/aliyun/alibabacloud-iot-device-sdk/blob/master/examples/origin.js
Unsubscribe
- API prototype: device#unsubscribe(topic/topic array, [callback])
- Function: Unsubscribes from a specified topic.
- Parameters
Parameter Description topic The specified topic. The data type is String. callback The callback function that is run when an UNSUBACK packet is received.
Example code:
const iot = require('alibabacloud-iot-device-sdk');
// Creating an iot.device object initiates a connection to Alibaba Cloud IoT.
const device = iot.device({
productKey: `${productKey}`, // Replace ${productKey} with the actual ProductKey of your product.
deviceName: `${deviceName}`,// Replace ${deviceName} with the actual DeviceName of your device.
deviceSecret: `${deviceSecret}`,// Replace ${deviceSecret} with the actual DeviceSecret of your device.
});
device.unsubscribe('/<productKey>/<deviceName>/user/get') 该文章对您有帮助吗?