-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.js
41 lines (32 loc) · 923 Bytes
/
broker.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
const mosca = require('mosca');
const _ = require('lodash');
let app = require('./server.js');
let http = require('http');
var ascoltatore = {
//using ascoltatore
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'browser-mqtt',
mongo: {}
};
var settings = {
port: 1883,
backend: ascoltatore
};
var server = new mosca.Server(settings);
let httpServer = app.listen(3000, () => {
console.log('Server running on port 3000!');
});
server.attachHttpServer(httpServer);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', _.isBuffer(packet.payload)? packet.payload.toString(): packet.payload);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}