-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrokerTest.js
37 lines (26 loc) · 934 Bytes
/
brokerTest.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
35
36
37
/* -----------------------------
AUTHOR: @SEYAM
seyam.bd.net@gmail.com
------------------------------ */
const mqtt = require('mqtt');
const broker = mqtt.connect('mqtt://iot.eclipse.org');
broker.on('connect', function () {
broker.subscribe('myLight'); //REMEMBER TO SUBSCRIBE FIRST TO THE TOPIC TO GET MESSAGE FROM THE BROKER
//broker.subscribe('light-status');
//broker.publish('light-status', state);
console.log('I get executed Once and I subscribed!!!');
});
//SUBSCRIBE TO BROKER TO GET MESSAGE
broker.on('message', function (topic, message) {
if(topic=="myLight"){
if(message == 'on'){
console.log('LED Turned ON');
//state = 'on';
}
if(message == 'off'){
console.log('LED Turned OFF');
//state = 'off';
}
//broker.publish('light-status', state);
}
});