How to use mqtt-packet - 10 common examples

To help you get started, we’ve selected a few mqtt-packet 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 linksmart / border-gateway / bgw-mqtt-proxy / index.js View on Github external
logger.log('debug', "Writing packet to dstClient", {
                                packet: packet,
                                validPacket: valid.packet
                            });
                            dstClient.write(generatedPacket);
                        }
                    } else {
                        // configurable disconnect in case of an unauthorized request
                        // always disconnect clients in case of a unauthorized connect request after forwarding the connack
                        if (
                            (packet.cmd === 'subscribe' && config.disconnect_on_unauthorized_subscribe) ||
                            (packet.cmd === 'publish' && config.disconnect_on_unauthorized_publish) ||
                            (packet.cmd === 'connect')
                        ) {
                            if (valid.packet && valid.packet.cmd === "connack") {
                                let generatedPacket = mqtt.generate(valid.packet, opts);
                                logger.log('debug', "Writing packet to srcClient", {
                                    packet: packet,
                                    validPacket: valid.packet
                                });
                                valid.packet && srcClient.write(generatedPacket);
                            }

                            logger
                                .log('info', "Destroying clients because of unauthorized request", {packet: packet});
                            srcClient.destroy();
                            dstClient.destroy();
                        }
                        // if a disconnection is not configured, just forward the response to srcClient
                        else {
                            let generatedPacket = mqtt.generate(valid.packet, opts);
                            logger.log('debug', "Writing packet to srcClient", {
github linksmart / border-gateway / bgw-mqtt-proxy / index.js View on Github external
dstClient.on("connect", () => {
        const srcParser = mqtt.parser(opts);
        const dstParser = mqtt.parser(opts);
        // const noopParser = mqtt.parser(opts);
        //
        // noopParser.on('packet', (packet) => {
        //     logger.log('debug', "packet event emitted on noopParser", {packet: packet});
        // });
        //
        // noopParser.on('error', (error) => {
        //     logger.log('debug', "error event emitted on noopParser", {errorMessage: error.message});
        // });

        srcClient.on('data', (data) => {
            logger.log('debug', "srcClient has data", {data: data});
            try {
                srcParser.parse(data);
            } catch (err) {
                logger.log('error', "Parse error in srcParser", {errorName: err.name, errorMessage: err.message});
github mcollina / aedes / lib / client.js View on Github external
function Client (broker, conn, req) {
  var that = this

  this.broker = broker
  this.conn = conn
  this.req = req
  this.parser = mqtt.parser()
  this.connected = false
  this.connackSent = false
  this.errored = false
  this.clean = true
  this._handling = 0
  this.subscriptions = {}
  this.id = null

  // we use two variables for the will
  // because we store in _will while
  // we are authenticating
  this.will = null
  this._will = null

  conn.client = this
github linksmart / border-gateway / bgw-mqtt-proxy / index.js View on Github external
dstClient.on("connect", () => {
        const srcParser = mqtt.parser(opts);
        const dstParser = mqtt.parser(opts);
        // const noopParser = mqtt.parser(opts);
        //
        // noopParser.on('packet', (packet) => {
        //     logger.log('debug', "packet event emitted on noopParser", {packet: packet});
        // });
        //
        // noopParser.on('error', (error) => {
        //     logger.log('debug', "error event emitted on noopParser", {errorMessage: error.message});
        // });

        srcClient.on('data', (data) => {
            logger.log('debug', "srcClient has data", {data: data});
            try {
                srcParser.parse(data);
            } catch (err) {
github mcollina / aedes / test / connect.js View on Github external
protocolId: 'MQTT',
    protocolVersion: 4,
    clean: true,
    clientId: 'my-client-proxyV2'
  }

  var protocol = new proxyProtocol.V2ProxyProtocol(
    proxyProtocol.Command.PROXY,
    proxyProtocol.TransportProtocol.STREAM,
    new proxyProtocol.IPv6ProxyAddress(
      proxyProtocol.IPv6Address.createFrom(clientIpArray),
      12345,
      proxyProtocol.IPv6Address.createWithEmptyAddress(),
      port
    ),
    mqttPacket.generate(packet)
  ).build()

  var broker = aedes({
    preConnect: function (client, done) {
      if (client.connDetails && client.connDetails.ipAddress) {
        client.ip = client.connDetails.ipAddress
        t.equal(clientIp, client.ip)
      } else {
        t.fail('no ip address present')
      }
      done(null, true)
      setImmediate(finish)
    },
    trustProxy: true
  })
github mqttjs / MQTT.js / test / client.js View on Github external
parser.on('packet', function (packet) {
        var packets = []

        if (packet.cmd === 'connect') {
          duplex.push(mqttPacket.generate({
            cmd: 'connack',
            sessionPresent: false,
            returnCode: 0
          }))

          for (var i = 0; i < max; i++) {
            packets.push(mqttPacket.generate({
              cmd: 'publish',
              topic: Buffer.from('hello'),
              payload: Buffer.from('world'),
              retain: false,
              dup: false,
              messageId: i + 1,
              qos: 1
            }))
          }
github Rodmg / aquila-mqtt-sn-gateway / node_modules / mqtt / lib / client.js View on Github external
function sendPacket (client, packet, cb) {
  try {
    var buf = mqttPacket.generate(packet);

    client.emit('packetsend', packet);

    if (client.stream.write(buf) && cb) {
      cb();
    } else if (cb) {
      client.stream.once('drain', cb);
    }
  } catch (err) {
    if (cb) {
      cb(err);
    } else {
      client.emit('error', err);
    }
  }
}
github noahziheng / freeiot / firmware / nodejs / node_modules / mqtt / lib / client.js View on Github external
function sendPacket (client, packet, cb) {
  client.emit('packetsend', packet)

  var result = mqttPacket.writeToStream(packet, client.stream)

  if (!result && cb) {
    client.stream.once('drain', cb)
  } else if (cb) {
    cb()
  }
}
github mqttjs / MQTT.js / lib / client.js View on Github external
function sendPacket (client, packet, cb) {
  client.emit('packetsend', packet)

  var result = mqttPacket.writeToStream(packet, client.stream, client.options)

  if (!result && cb) {
    client.stream.once('drain', cb)
  } else if (cb) {
    cb()
  }
}
github mqttjs / MQTT.js / lib / connection.js View on Github external
var mqttPacket = require('mqtt-packet')
  , Connection = mqttPacket.connection
  , inherits   = require('inherits');

function emitPacket(packet) {
  this.emit(packet.cmd, packet);
}

function MQTTConnection(duplex, opts) {
  if (!(this instanceof MQTTConnection)) {
    return new MQTTConnection(duplex, opts);
  }

  Connection.call(this, duplex, opts);

  this.stream = duplex;

  this.on('data', emitPacket);

mqtt-packet

Parse and generate MQTT packets like a breeze

MIT
Latest version published 5 days ago

Package Health Score

84 / 100
Full package analysis