How to use the mqtt.createServer function in mqtt

To help you get started, we’ve selected a few mqtt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github enableiot / iotkit-agent / server.js View on Github external
} 
  
});

server.on("listening", function () {
  var addr = server.address();
  logger.info('Starting UDP Server on %s:%d', addr.address, addr.port);
});

server.bind(SERVER_UDP_PORT);

// ************************************************************
// MQTT Server
// ************************************************************
logger.info('Starting MQTT broker on %s ...', SERVER_MQTT_PORT);
mqtt.createServer(function(client) {

  logger.info('Server created...');

  client.on('connect', function(packet) {
    client.connack({returnCode: 0});
    client.id = packet.clientId;
    logger.info('MQTT Client connected: %s', packet.clientId);
  });

  client.on('publish', function(packet) {
    logger.info('MQTT Topic: %s Payload: %s', packet.topic, packet.payload);
    makeMetrics(packet.topic, JSON.parse(packet.payload));
  });

  client.on('pingreq', function(packet) {
    client.pingresp();
github enableiot / iotkit-agent / listeners / mqtt.js View on Github external
if (fs.existsSync(verifyCertKeyPath)) {
        tlsArgs = {
            keyPath: conf.connector.mqtt.key || './certs/client.key',
            certPath: conf.connector.mqtt.crt || './certs/client.crt',
            keepalive: 59000
        };
    } else {
        // load from /usr/share
        tlsArgs = {
            keyPath: '/usr/share/iotkit-agent/certs/enableiot_agent.key',
            certPath: '/usr/share/iotkit-agent/certs/enableiot_agent.crt',
            keepalive: 59000
        };
    }

  var mqttServer = mqtt.createServer(function(client) {

    client.on('connect', function(packet) {
      client.connack({returnCode: 0});
      client.id = packet.clientId;
      logger.debug('MQTT Client connected: ', packet.clientId);
    });

    client.on('publish', function(packet) {
      logger.debug('MQTT Topic: %s Payload: %s', packet.topic, packet.payload);
      try {
        onMessage(JSON.parse(packet.payload));
      } catch (ex) {
        logger.error('MQTT Error on message: %s', ex);
      }
  });
github phodal / lan / test / modules / mqtt_spec.js View on Github external
it("spec name", function () {
		var mqttServer = mqtt.createServer(mqttModule).listen(1884, function () {
			console.log("mqtt server listening on port %d", 1884);
		});
		mqttServer.close();
	});
});
github mcollina / ascoltatori / test / mqtt_server.js View on Github external
module.exports = function (port) {
  return mqtt.createServer(function (client) {
    var self = this;

    if (!self.clients) self.clients = {};

    client.on('connect', function (packet) {
      self.clients[packet.client] = client;
      client.id = packet.client;
      client.subscriptions = [];
      client.connack({returnCode: 0});
    });

    client.on('subscribe', function (packet) {
      var granted = [];

      for (var i = 0; i < packet.subscriptions.length; i++) {
        var qos = packet.subscriptions[i].qos
github dx-ted-emea / iot-labs / lab2-energy monitor / src / nitrogen-io / server.js View on Github external
var config = require('./config')
  , mqtt = require('mqtt')
  , nitrogen = require('nitrogen');

//TODO: need to move config to file
var AzureEventHubManager = require("./AzureEventHubManager.js")
var aehm = new AzureEventHubManager(config.eventhub_namespace, config.eventhub_hubname ,config.eventhub_keyname, config.eventhub_keyvalue)

var service = new nitrogen.Service(config);

var FAILURE = 1;
var SUCCESS = 0;

var mqttServer = mqtt.createServer(function(client) {

    client.on('connect', function(packet) {
        console.log("CONNECT: " + JSON.stringify(packet));

        if (!packet.username || !packet.password) {
            console.log("Error: No username or password.");
            return client.connack({ returnCode: 1 });
        }

        var principal = new nitrogen.Device({
            accessToken: {
                token: packet.password
            },
            id: packet.username,
            nickname: packet.username
        });
github NetEase / pomelo / lib / connectors / mqttconnector.js View on Github external
Connector.prototype.start = function(cb) {
  var self = this;
  this.mqttServer = mqtt.createServer();
  this.mqttServer.on('client', function(client) {
		client.on('error', function(err) {
			client.stream.destroy();
		});
		
    client.on('close', function() {
			client.stream.destroy();
		});
		
    client.on('disconnect', function(packet) {
			client.stream.destroy();
		});
    
    if(self.opts.disconnectOnTimeout) {
      var timeout = self.opts.timeout * 1000 || constants.TIME.DEFAULT_MQTT_HEARTBEAT_TIMEOUT;
      client.stream.setTimeout(timeout,function() {
github dpjanes / iotdb-homestar / app / mqtt.js View on Github external
var create_server = function () {
    mqtt
        .createServer(server_client)
        .listen(settings.d.mqttd.port, settings.d.mqttd.host);

    logger.info({
        method: "create_server",
        port: settings.d.mqttd.port,
        host: settings.d.mqttd.host,
    }, "listening for MQTT");

};