npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mqtt-packet-for-milkcocoa

v3.2.0

Published

Parse and generate MQTT packets like a breeze for Milkcocoa

Downloads

10

Readme

mqtt-packet   Build Status

Encode and Decode MQTT 3.1.1 packets the node way.

  • Install
  • Examples
  • Packets
  • API
  • Contributing
  • Licence & copyright

This library works with node v0.10 and node v0.8, but it requires at least NPM 1.4. To upgrade on node v0.8, run npm install [email protected] -g.

Install

npm install mqtt-packet --save

Examples

Generating

var mqtt    = require('mqtt-packet')
  , object  = {
        cmd: 'publish'
      , retain: false
      , qos: 0
      , dup: false
      , length: 10
      , topic: 'test'
      , payload: 'test' // can also be a Buffer
    }

console.log(mqtt.generate(object))
// prints
// <Buffer 30 0a 00 04 74 65 73 74 74 65 73 74>
//
// the same as
//
// new Buffer([
//   48, 10, // Header
//   0, 4, // Topic length
//   116, 101, 115, 116, // Topic (test)
//   116, 101, 115, 116 // Payload (test)
// ])

Parsing

var mqtt      = require('mqtt-packet')
  , parser    = mqtt.parser()

// synchronously emits all the parsed packets
parser.on('packet', function(packet) {
  console.log(packet)
  // prints:
  //
  // {
  //     cmd: 'publish'
  //   , retain: false
  //   , qos: 0
  //   , dup: false
  //   , length: 10
  //   , topic: 'test'
  //   , payload: <Buffer 74 65 73 74>
  // }
})

parser.parse(new Buffer([
  48, 10, // Header
  0, 4, // Topic length
  116, 101, 115, 116, // Topic (test)
  116, 101, 115, 116 // Payload (test)
])
// returns the number of bytes left in the parser

API

  • mqtt#generate()
  • mqtt#parser()

Generates a Buffer containing an MQTT packet. The object must be one of the ones specified by the packets section. Throws an Error if a packet cannot be generated.

Returns a new Parser object. Parser inherits from EventEmitter and will emit:

  • packet, when a new packet is parsed, according to packets
  • error, if an error happens

Parse a given Buffer and emits synchronously all the MQTT packets that are included. Returns the number of bytes left to parse.

Packets

This section describes the format of all packets emitted by the Parser and that you can input to generate.

Connect

{
    cmd: 'connect'
  , protocolId: 'MQTT' // or 'MQIsdp' in MQTT 3.1.1
  , protocolVersion: 4 // or 3 in MQTT 3.1
  , clean: true // or false
  , clientId: 'my-device'
  , keepalive: 0 // seconds, 0 is the default, can be any positive number
  , username: 'matteo'
  , password: new Buffer('collina') // passwords are buffers
  , will: {
        topic: 'mydevice/status'
      , payload: new Buffer('dead') // payloads are buffers
    }
}

The only mandatory argument is clientId, as generate will throw if missing.

If password or will.payload are passed as strings, they will automatically be converted into a Buffer.

Connack

{
    cmd: 'connack'
  , returnCode: 0 // or whatever else you see fit
  , sessionPresent: false // or true.
}

The only mandatory argument is returnCode, as generate will throw if missing.

Subscribe

{
    cmd: 'subscribe'
  , messageId: 42
  , subscriptions: [{
        topic: 'test'
      , qos: 0
    }]
}

All properties are mandatory.

Suback

{
    cmd: 'suback'
  , messageId: 42
  , granted: [0, 1, 2, 128]
}

All the granted qos must be < 256, as they are encoded as UInt8. All properties are mandatory.

Unsubscribe

{
    cmd: 'unsubscribe'
  , messageId: 42
  , unsubscriptions: [
        'test'
      , 'a/topic'
    ]
}

All properties are mandatory.

Unsuback

{
    cmd: 'unsuback'
  , messageId: 42
}

All properties are mandatory.

Publish

{
    cmd: 'publish'
  , messageId: 42
  , qos: 2
  , dup: false
  , topic: 'test'
  , payload: new Buffer('test')
  , retain: false
}

Only the topic and properties are mandatory Both topic and payload can be Buffer objects instead of strings. messageId is mandatory for qos > 0.

If payload is passed to generate(packet) as a string, it will be automatically converted into a Buffer.

Puback

{
    cmd: 'puback'
  , messageId: 42
}

The only mandatory argument is messageId, as generate will throw if missing.

Pubrec

{
    cmd: 'pubcomp'
  , messageId: 42
}

The only mandatory argument is messageId, as generate will throw if missing.

Pubrel

{
    cmd: 'pubrel'
  , messageId: 42
}

The only mandatory argument is messageId, as generate will throw if missing.

Pubcomp

{
    cmd: 'pubcomp'
  , messageId: 42
}

The only mandatory argument is messageId, as generate will throw if missing.

Pingreq

{
  cmd: 'pingreq'
}

Pingresp

{
  cmd: 'pingresp'
}

Disconnect

{
  cmd: 'pingresp'
}

Contributing

mqtt-packet is an OPEN Open Source Project. This means that:

Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.

See the CONTRIBUTING.md file for more details.

Contributors

mqtt-packet is only possible due to the excellent work of the following contributors:

License

MIT