forked from qnap-dev/qnap-qiot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-subscribe.js
34 lines (31 loc) · 911 Bytes
/
mqtt-subscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* This sample code demo receive value from QIoT Suite Lite by MQTT protocol
* requirement:
* -- npm install
* run command: node mqtt-subscribe.js
*/
var qiot = require('./lib/qiot');
/**
* Setup connection options
*/
var connection = new qiot(qiot.protocol.MQTT);
var connection_option = connection.readResource('./res/resourceinfo.json', './ssl/');
connection.connect(connection_option);
// subscribe when client ready
connection.on('connect', function(data) {
// TODO: you could replace "temp" by any resource id set form QIoT Suite Lite
connection.subscribeById("temp");
});
/**
* Receive data of QIoT Suite Lite.
*/
connection.on('message', function(data) {
switch (data.id) {
case "temp":
console.log("temp : " + data.message.value);
console.log("------------------------")
break;
default:
break;
}
});