-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.js
34 lines (27 loc) · 992 Bytes
/
mqtt.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
var mqtt = require('mqtt');
var options = {
port: 17***,
host: 'mqtt://m11.cloudmqtt.com',
clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
username: 'User_Name',
password: 'User_Password',
keepalive: 60,
reconnectPeriod: 1000,
protocolId: 'MQIsdp',
protocolVersion: 3,
clean: true,
encoding: 'utf8'
};
var client = mqtt.connect('mqtt://m11.cloudmqtt.com', options);
client.on('connect', function() {
console.log("Connected");//The first connection will have a latency, so don't worry
client.subscribe('test', function() {
client.on('message', function(topic, message, packet) {
console.log("Received '" + message + "' on '" + topic + "'");
});
});
/*client.publish('test', 'my message', function() { Uncomment if you want to test the publish function
console.log("Published");
client.end(); // Close the connection when published
});*/
});