package
Parse and generate MQTT packets like a breeze
npm install mqtt-packetGitHub-flavored package documentation.
Across all versions, last 30 days.
14,071,563 in 30 days
Runtime metadata.
Tagged topics.
Encode and Decode MQTT 3.1.1, 5.0 packets the node way.
This library is tested with node v6, v8, v10, v12 and v14. The last version to support older versions of node was mqtt-packet@4.1.2.
npm install mqtt-packet --saveconst mqtt = require('mqtt-packet');
const object = {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 10,
topic: 'test',
payload: 'test' // Can also be a Buffer
};
const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet
console.log(mqtt.generate(object))
// Prints:
//
// <Buffer 30 0a 00 04 74 65 73 74 74 65 73 74>
//
// Which is the same as:
//
// Buffer.from([
// 48, 10, // Header (publish)
// 0, 4, // Topic length
// 116, 101, 115, 116, // Topic (test)
// 116, 101, 115, 116 // Payload (test)
// ])const mqtt = require('mqtt-packet');
const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet
const parser = mqtt.parser(opts);
// Synchronously emits all the parsed packets
parser.on('packet', 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(Buffer.from([
48, 10, // Header (publish)
0, 4, // Topic length
116, 101, 115, 116, // Topic (test)
116, 101, 115, 116 // Payload (test)
]))
// Returns the number of bytes left in the parserGenerates 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.
Writes the mqtt packet defined by object to the given stream.
The object must be one of the ones specified by the packets
section. Emits an Error on the stream if a packet cannot be generated.
On node >= 0.12, this function automatically calls cork() on your stream,
and then it calls uncork() on the next tick.
By default cache for number buffers is enabled.
It creates a list of buffers for faster write. To disable cache set mqtt.writeToStream.cacheNumbers = false.
Should be set before any writeToStream calls.
packet, when a new packet is parsed, according to
packetserror, if an error happensmqtt-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.
mqtt-packet is only possible due to the excellent work of the following contributors:
| Matteo Collina | GitHub/mcollina | Twitter/@matteocollina |
|---|---|---|
| Adam Rudd | GitHub/adamvr | Twitter/@adam_vr |
| Peter Sorowka | GitHub/psorowka | Twitter/@psorowka |
| Siarhei Buntsevich | GitHub/scarry1992 |
MIT
Estimated unpacked size across 5 packages.
330.3 kB
Includes dependencies.
3 direct · 1 outdated