moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 509 forks source link

Use embedded mosca without a Broker #554

Closed helpme1 closed 7 years ago

helpme1 commented 7 years ago

From the documentation, it seems that embedded mosca has to use an external broker such as Mosquitto, AMQP, ZeroMQ, Redis. For stand-alone mosca, no external broker is required. Can an embedded mosca be run such that no external broker is used similar to stand-alone mosca?

If this can be done, how should the code that uses redis below be modified?

var mosca = require('mosca')

var ascoltatore = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  port: 6379,
  return_buffers: true, // to handle binary payloads
  host: "localhost"
};

var moscaSettings = {
  port: 1883,
  backend: ascoltatore,
  persistence: {
    factory: mosca.persistence.Redis
  }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

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);
});

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}
vladbabii commented 7 years ago

You mean something like

    moscaSettings = {
         host: "<ip>"
        ,port: "<port>"
        ........
        ,persistence : {
            factory : mosca.persistence.Memory
        }
    };
muety commented 7 years ago

Got the same question. Can I use pure Mosca without Mosquitto or Redis or the like? Just in-memory?

vladbabii commented 7 years ago

Yes, with the above configuration. I run it like that with no issues.

helpme1 commented 7 years ago

Here is working sample code that I wrote myself.

var mosca = require('mosca')

var settings = {
    port: 1883,
};

var server = new mosca.Server(settings);
server.on('ready', setup);

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);
});

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