Mosca

function

Server

Server()
  • @param: {Object} opts The option object
  • @param: {Function} callback The ready callback

The Mosca Server is a very simple MQTT server that supports
only QoS 0.
It is backed by Ascoltatori, and it descends from
EventEmitter.

Options

  • port, the port where to create the server.
  • backend, all the options for creating the Ascoltatore that will power this server.

Events

  • clientConnected, when a client is connected; the client is passed as a parameter.
  • clientDisconnected, when a client is disconnected; the client is passed as a parameter.
  • published, when a new message is published; the packet and the client are passed as parameters.
function Server(opts, callback) {
  EventEmitter.call(this);

  this.opts = opts || {};
  this.opts.port = this.opts.port || 1883;

  callback = callback || function () {};

  this.clients = {};

  var that = this;

  var serveWrap = function (client) {
    that.serve(client);
  };

  this.ascoltatore = ascoltatori.build(this.opts.backend);

  async.series([
    function (cb) {
      that.ascoltatore.on("ready", cb);
    },
    function (cb) {
      that.server = mqtt.createServer(serveWrap);

      that.once("ready", callback);

      that.server.listen(that.opts.port, cb);
    }, function(cb) {
      that.emit("ready");
      debug("started on port " + that.opts.port);
    }
  ]);
}

module.exports = Server;

Server.prototype = Object.create(EventEmitter.prototype);
method

close

Server.prototype.close()
  • @param: {Function} callback The closed callback function

Closes the server.

Server.prototype.close = function(callback) {
  var that = this;

  callback = callback || function() {};

  async.parallel(Object.keys(that.clients).map(function(id) { 
    return function(cb) {
      that.closeConn(that.clients[id], cb);
    };
  }), function() {
    that.once("closed", callback);
    try {
      that.server.close(function() {
        debug("closed");
        that.emit("closed");
      });
    } catch(exception) {
      callback(exception);
    }
  });
};