-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (35 loc) · 1.12 KB
/
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
var http = require('http');
var mosca = require('mosca');
var port = (process.env.PORT || 3000);
// var ascoltatore = {
// //using ascoltatore
// type: 'mongo',
// url: 'mongodb://localhost:27017/mqtt',
// pubsubCollection: 'ascoltatori',
// mongo: {}
// };
var settings = {
port: 1883
// backend: ascoltatore
};
var Mqttsv = new mosca.Server(settings);
Mqttsv.on('clientConnected', function (client) {
console.log('client connected', client.id);
});
// // fires when a message is published, incase if you wanted to do something when each client publish something
// Mqttsv.on('published', function (packet, client) {
// console.log('\nTopic', packet.topic);
// console.log('Message', packet.payload);
// });
Mqttsv.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running', port);
}
// // to host along with http server
// var server = http.createServer();
// Mqttsv.attachHttpServer(server)
// server.listen(port, function (err) {
// if (err) throw err;
// console.log("Server is running on port: ", port);
// });