-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketio+mqtt_Server.js
78 lines (44 loc) · 1.71 KB
/
socketio+mqtt_Server.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* -----------------------------
AUTHOR: @SEYAM
seyam.bd.net@gmail.com
------------------------------ */
//var sys = require('sys');
//var net = require('net');
var sys = require('util');
var mqtt = require('mqtt');
var io = require('socket.io')();
//var broker = new mqtt.MQTTbroker(1883, '127.0.0.1', 'pusher');
const broker = mqtt.connect('mqtt://broker.hivemq.com')
io.on('connection', function (client) {
console.log('client connected himself');
client.on('publish', function (data) {
console.log('publishing to '+data.topic);
broker.publish(data.topic);
});
client.on('subscribe', function (data) {
console.log('Subscribing to '+data.topic);
//socket.join(data.topic);
broker.subscribe(data.topic);
});
client.on('disconnect',function(){ //No Parameter For Disconnect Event
console.log('client disconnected himself');
});
});
client.addListener('mqttData', function(topic, payload){
sys.puts(topic+'='+payload);
io.sockets.emit('mqtt',{'topic':String(topic),
'payload':String(payload)});
});
// // listen to messages coming from the mqtt broker
// broker.on('message', function (topic, payload, packet) {
// console.log(topic+'='+payload);
// io.sockets.emit('mqtt',{'topic':String(topic),
// 'payload':String(payload)});
// });
io.listen(5000);
console.log("SocketIO server is running at port 5000!");
/*broker.addListener('mqttData', function(topic, payload){
sys.puts(topic+'='+payload);
io.sockets.emit('mqtt',{'topic':String(topic),
'payload':String(payload)});
});*/