Node.js SDK Access Example
This topic describes how to use a Node.js AMQP SDK to connect to the HUAWEI CLOUD IoT platform and receive subscribed messages from the platform.
Development Environment
Node.js 8.0.0 or later is used.
Downloading the SDK
For the AMQP SDK using Node.js, rhea is recommended. Visit rhea to download the repository and view the user guide.
Adding Dependencies
Add the following dependencies to the package.json file:
"dependencies": {
"rhea": "^1.0.12"
}
Sample Code
You can click here to obtain the SDK access example. For details on the parameters involved in the demo, see AMQP Client Access.
const container = require('rhea');
// Obtain the timestamp.
var timestamp = Math.round(new Date() / 1000);
// Set up a connection.
var connection = container.connect({
// Access domain name. For details, see AMQP Client Access.
'host': '${UUCID}.iot-amqps.cn-north-4.myhuaweicloud.com',
'port': 5671,
'transport': 'tls',
'reconnect': true,
'idle_time_out': 8000,
// Method to assemble username. For details, see AMQP Client Access.
'username': 'accessKey=${yourAccessKey}|timestamp=' + timestamp + '|',
// accessCode. For details, see AMQP Client Access.
'password': '${yourAccessCode}',
'saslMechannisms': 'PLAIN',
'rejectUnauthorized': false,
'hostname': 'default',
});
// Create a Receiver connection. You can use DefaultQueue.
var receiver = connection.open_receiver('${yourQueue}');
// Callback function for receiving messages pushed from the cloud
container.on('message', function (context) {
var msg = context.message;
var content = msg.body;
console.log(content);
// Send an ACK message. Note that the callback function should not contain time-consuming logic.
context.delivery.accept();
});
Last Article: Java SDK Access Example
Next Article: Java Demo
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.