Pages

Saturday, June 27, 2020

Create MQTT Server with NodeJs

Initialize nodejs project
npm init --yes
Install mosca mqtt module
npm install --save mosca
Create server.js and type this content
var mosca = require('mosca');

var ascoltatore = {
  //using ascoltatore
  type: 'mongo',
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

var settings = {
  port: 1883,
  backend: ascoltatore
};

var server = new mosca.Server(settings);

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', packet.payload);
});

server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running');
}

Create MQTT Server with NodeJs

Initialize nodejs project npm init --yes Install mosca mqtt module npm install --save mosca Create server.js and type this content va...