mqttsn-packet

0.0.3 • Public • Published

mqttsn-packet   Build Status

Encode and Decode MQTT-SN 1.2 packets. It is freely inspired from mqttjs/mqtt-packet.

This module supports streaming input (eg from a TCP socket for a serial port) where a single buffer pushed to the parser may contain a partial packet or multiple packets at once.

Install

npm install mqttsn-packet --save

Examples

Generating

var mqttsn  = require('mqttsn-packet'),
    object  = {
      cmd: 'connect',
      will: true,
      cleanSession: true,
      duration: 1800,
      clientId: 'test'
    };

console.log(mqttsn.generate(object));
// prints
// <Buffer 0a 04 0c 01 07 08 74 65 73 74>
// as :
// new Buffer([
//   10, 4, // header
//   12, 1, // flags & protocolId
//   7, 8,  // duration : 1800 seconds
//   116, 101, 115, 116 // client Id : test
// ])

Parsing

var mqttsn  = require('mqttsn-packet'),
    parser  = mqttsn.parser();

parser.on('packet', function (packet) {
  console.log(packet);
  // prints :
  // Packet {
  //   cmd: 'connect',
  //   will: true,
  //   cleanSession: true,
  //   duration: 1800,
  //   clientId: 'test' }
});

parser.parse(new Buffer([
  10, 4, // header
  12, 1, // flags & protocolId
  7, 8,  // duration : 1800 seconds
  116, 101, 115, 116 // client Id : test
]));
// returns the number of bytes left in the parser

License

MIT

Package Sidebar

Install

npm i mqttsn-packet

Weekly Downloads

27

Version

0.0.3

License

MIT

Unpacked Size

44.2 kB

Total Files

14

Last publish

Collaborators

  • ithinuel