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-parser

v0.0.3

Published

Mqtt packet parser

Downloads

2

Readme

MQTT-parser

Split and parse MQTT packet from buffer.

MQTT protocol document: EN: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html ZH: https://mcxiaoke.gitbooks.io/mqtt-cn/content/


Usage

/* javascript */
const mqttParser = require('mqtt-parser')
const connectBuffer = Buffer.from([
  16, 56, // Header
  0, 4, // Protocol ID length
  77, 81, 84, 84, // Protocol ID
  3, // Protocol version
  246, // Connect flags
  0, 30, // Keepalive
  0, 5, // Client ID length
  104, 101, 108, 108, 111, // Client ID
  0, 6, // Will topic length
  228, 189, 160, 229, 165, 189, // Will topic
  0, 8, // Will payload length
  112, 97, 121, 108, 111, 97, 100, 126, // Will payload
  0, 8, // Username length
  117, 115, 101, 114, 110, 97, 109, 101, // Username
  0, 9, // Password length
  112, 52, 36, 36, 119, 48, 194, 163, 100 // Password
])

let packets = mqttParser.parse(connectBuffer)
console.log(packets[0])
/*
  { packetType: 1,
    flags: 0,
    remainLength: 56,
    buffer: <Buffer 10 38 00 04 4d 51 54 54 03 f6 00 1e 00 05 68 65 6c 6c 6f 00 06 e4 bd a0 e5 a5 bd 00 08 70 61 79 6c 6f 61 64 7e 00 08 75 73 65 72 6e 616d 65 00 09 70 ... >,
    protocolName: 'MQTT',
    protocolLevel: 3,
    connectFlags: { 
      username: 1,
      password: 1,
      willRetain: 1,
      willQoS: 2,
      willFlag: 1,
      cleanSession: 1,
      reserved: 0 },
      keepAlive: 30,
      clientId: 'hello',
      willTopic: '你好',
      willMessage: 'payload~',
      username: 'username',
      password: 'p4$$w0£d'
    }
  }
*/

packets = mqttParser.parse(Buffer.concat([connectBuffer, connectBuffer, ...otherTypesPacket]))
console.log(packets)
/*
  [{ packetType: 1, ... },
  { packetType: 1, ... },
  ...]
*/
/* typescript */
import { parse, PacketType, Packet } from 'mqtt-parser'
const packets = mqttParser.parse(Buffer.from(connectBuffer)) // packets: BasePacket[]
if (packets[0].packetType === PacketType.CONNECT) {
  let packet = packet[0] as Packet.Connect
  packet.willTopic // code complete
}

API

parse(buffer: Buffer): BasePacket[]