Skip to content

Rodmg/mqttsn-packet

 
 

Repository files navigation

mqttsn-packet   Build Status

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

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

About

Parse and generate MQTT-SN packets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%