Gathering detailed insights and metrics for mqtt-packet
Gathering detailed insights and metrics for mqtt-packet
Gathering detailed insights and metrics for mqtt-packet
Gathering detailed insights and metrics for mqtt-packet
aedes-packet
Basic data structure for packets in Aedes
u8-mqtt-packet
MQTT packet codec using Uint8Array -- suitable for use in the Browser, NodeJS, and Deno.land.
iopa-mqtt-packet
API-first OASIS Message Queuing Telemetry Transport (MQTT) packet transport for the Internet of Things (IoT), based on the Internet of Protocols Alliance (IOPA) specification
mqtt-packet-shgbit
Parse and generate MQTT packets like a breeze
Parse and generate MQTT packets like a breeze in JS
npm install mqtt-packet
Typescript
Module System
Node Version
NPM Version
98.8
Supply Chain
99.6
Quality
79.2
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
198,639,653
Last Day
75,898
Last Week
1,261,880
Last Month
5,404,674
Last Year
55,297,960
NOASSERTION License
211 Stars
296 Commits
96 Forks
18 Watchers
11 Branches
40 Contributors
Updated on Jun 01, 2025
Latest Version
9.0.2
Package Id
mqtt-packet@9.0.2
Unpacked Size
170.15 kB
Size
30.56 kB
File Count
19
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 04, 2025
Cumulative downloads
Total Downloads
Last Day
1.1%
75,898
Compared to previous day
Last Week
-0.4%
1,261,880
Compared to previous week
Last Month
-0.8%
5,404,674
Compared to previous month
Last Year
35.3%
55,297,960
Compared to previous year
3
5
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.
1npm install mqtt-packet --save
1const mqtt = require('mqtt-packet'); 2const object = { 3 cmd: 'publish', 4 retain: false, 5 qos: 0, 6 dup: false, 7 length: 10, 8 topic: 'test', 9 payload: 'test' // Can also be a Buffer 10}; 11const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet 12 13console.log(mqtt.generate(object)) 14// Prints: 15// 16// <Buffer 30 0a 00 04 74 65 73 74 74 65 73 74> 17// 18// Which is the same as: 19// 20// Buffer.from([ 21// 48, 10, // Header (publish) 22// 0, 4, // Topic length 23// 116, 101, 115, 116, // Topic (test) 24// 116, 101, 115, 116 // Payload (test) 25// ])
1const mqtt = require('mqtt-packet'); 2const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet 3const parser = mqtt.parser(opts); 4 5// Synchronously emits all the parsed packets 6parser.on('packet', packet => { 7 console.log(packet) 8 // Prints: 9 // 10 // { 11 // cmd: 'publish', 12 // retain: false, 13 // qos: 0, 14 // dup: false, 15 // length: 10, 16 // topic: 'test', 17 // payload: <Buffer 74 65 73 74> 18 // } 19}) 20 21parser.parse(Buffer.from([ 22 48, 10, // Header (publish) 23 0, 4, // Topic length 24 116, 101, 115, 116, // Topic (test) 25 116, 101, 115, 116 // Payload (test) 26])) 27// Returns the number of bytes left in the 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.
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.
Returns a new Parser
object. Parser
inherits from EventEmitter
and
will emit:
packet
, when a new packet is parsed, according to
packetserror
, if an error happensParses a given Buffer
and emits synchronously all the MQTT packets that
are included. Returns the number of bytes left to parse.
If an error happens, an error
event will be emitted, but no packet
events
will be emitted after that. Calling parse()
again clears the error and
previous buffer, as if you created a new Parser
.
This section describes the format of all packets emitted by the Parser
and that you can input to generate
.
1{ 2 cmd: 'connect', 3 protocolId: 'MQTT', // Or 'MQIsdp' in MQTT 3.1 and 5.0 4 protocolVersion: 4, // Or 3 in MQTT 3.1, or 5 in MQTT 5.0 5 clean: true, // Can also be false 6 clientId: 'my-device', 7 keepalive: 0, // Seconds which can be any positive number, with 0 as the default setting 8 username: 'matteo', 9 password: Buffer.from('collina'), // Passwords are buffers 10 will: { 11 topic: 'mydevice/status', 12 payload: Buffer.from('dead'), // Payloads are buffers 13 properties: { // MQTT 5.0 14 willDelayInterval: 1234, 15 payloadFormatIndicator: false, 16 messageExpiryInterval: 4321, 17 contentType: 'test', 18 responseTopic: 'topic', 19 correlationData: Buffer.from([1, 2, 3, 4]), 20 userProperties: { 21 'test': 'test' 22 } 23 } 24 }, 25 properties: { // MQTT 5.0 properties 26 sessionExpiryInterval: 1234, 27 receiveMaximum: 432, 28 maximumPacketSize: 100, 29 topicAliasMaximum: 456, 30 requestResponseInformation: true, 31 requestProblemInformation: true, 32 userProperties: { 33 'test': 'test' 34 }, 35 authenticationMethod: 'test', 36 authenticationData: Buffer.from([1, 2, 3, 4]) 37 } 38}
If protocolVersion
is 3, clientId
is mandatory and generate
will throw if
missing.
If password
or will.payload
are passed as strings, they will
automatically be converted into a Buffer
.
1{ 2 cmd: 'connack', 3 returnCode: 0, // Or whatever else you see fit MQTT < 5.0 4 sessionPresent: false, // Can also be true. 5 reasonCode: 0, // reason code MQTT 5.0 6 properties: { // MQTT 5.0 properties 7 sessionExpiryInterval: 1234, 8 receiveMaximum: 432, 9 maximumQoS: 1, 10 retainAvailable: true, 11 maximumPacketSize: 100, 12 assignedClientIdentifier: 'test', 13 topicAliasMaximum: 456, 14 reasonString: 'test', 15 userProperties: { 16 'test': 'test' 17 }, 18 wildcardSubscriptionAvailable: true, 19 subscriptionIdentifiersAvailable: true, 20 sharedSubscriptionAvailable: false, 21 serverKeepAlive: 1234, 22 responseInformation: 'test', 23 serverReference: 'test', 24 authenticationMethod: 'test', 25 authenticationData: Buffer.from([1, 2, 3, 4]) 26 } 27}
The only mandatory argument is returnCode
, as generate
will throw if
missing.
1{ 2 cmd: 'subscribe', 3 messageId: 42, 4 properties: { // MQTT 5.0 properties 5 subscriptionIdentifier: 145, 6 userProperties: { 7 test: 'test' 8 } 9 } 10 subscriptions: [{ 11 topic: 'test', 12 qos: 0, 13 nl: false, // no Local MQTT 5.0 flag 14 rap: true, // Retain as Published MQTT 5.0 flag 15 rh: 1 // Retain Handling MQTT 5.0 16 }] 17}
All properties are mandatory.
1{ 2 cmd: 'suback', 3 messageId: 42, 4 properties: { // MQTT 5.0 properties 5 reasonString: 'test', 6 userProperties: { 7 'test': 'test' 8 } 9 } 10 granted: [0, 1, 2, 128] 11}
All the granted qos must be < 256, as they are encoded as UInt8. All properties are mandatory.
1{ 2 cmd: 'unsubscribe', 3 messageId: 42, 4 properties: { // MQTT 5.0 properties 5 userProperties: { 6 'test': 'test' 7 } 8 } 9 unsubscriptions: [ 10 'test', 11 'a/topic' 12 ] 13}
All properties are mandatory.
1{ 2 cmd: 'unsuback', 3 messageId: 42, 4 properties: { // MQTT 5.0 properties 5 reasonString: 'test', 6 userProperties: { 7 'test': 'test' 8 } 9 } 10}
All properties are mandatory.
1{ 2 cmd: 'publish', 3 messageId: 42, 4 qos: 2, 5 dup: false, 6 topic: 'test', 7 payload: Buffer.from('test'), 8 retain: false, 9 properties: { // optional properties MQTT 5.0 10 payloadFormatIndicator: true, 11 messageExpiryInterval: 4321, 12 topicAlias: 100, 13 responseTopic: 'topic', 14 correlationData: Buffer.from([1, 2, 3, 4]), 15 userProperties: { 16 'test': 'test' 17 }, 18 subscriptionIdentifier: 120, // can be an Array in message from broker, if message included in few another subscriptions 19 contentType: 'test' 20 } 21}
Only the topic
property is mandatory.
Both topic
and payload
can be Buffer
objects instead of strings.
messageId
is mandatory for qos > 0
.
1{ 2 cmd: 'puback', 3 messageId: 42, 4 reasonCode: 16, // only for MQTT 5.0 5 properties: { // MQTT 5.0 properties 6 reasonString: 'test', 7 userProperties: { 8 'test': 'test' 9 } 10 } 11}
The only mandatory property is messageId
, as generate
will throw if
missing.
1{ 2 cmd: 'pubrec', 3 messageId: 42, 4 reasonCode: 16, // only for MQTT 5.0 5 properties: { // properties MQTT 5.0 6 reasonString: 'test', 7 userProperties: { 8 'test': 'test' 9 } 10 } 11}
The only mandatory property is messageId
, as generate
will throw if
missing.
1{ 2 cmd: 'pubrel', 3 messageId: 42, 4 reasonCode: 16, // only for MQTT 5.0 5 properties: { // properties MQTT 5.0 6 reasonString: 'test', 7 userProperties: { 8 'test': 'test' 9 } 10 } 11}
The only mandatory property is messageId
, as generate
will throw if
missing.
1{ 2 cmd: 'pubcomp', 3 messageId: 42, 4 reasonCode: 16, // only for MQTT 5.0 5 properties: { // properties MQTT 5.0 6 reasonString: 'test', 7 userProperties: { 8 'test': 'test' 9 } 10 } 11}
The only mandatory property is messageId
, as generate
will throw if
missing.
1{ 2 cmd: 'pingreq' 3}
1{ 2 cmd: 'pingresp' 3}
1{ 2 cmd: 'disconnect', 3 reasonCode: 0, // MQTT 5.0 code 4 properties: { // properties MQTT 5.0 5 sessionExpiryInterval: 145, 6 reasonString: 'test', 7 userProperties: { 8 'test': 'test' 9 }, 10 serverReference: 'test' 11 } 12}
1{ 2 cmd: 'auth', 3 reasonCode: 0, // MQTT 5.0 code 4 properties: { // properties MQTT 5.0 5 authenticationMethod: 'test', 6 authenticationData: Buffer.from([0, 1, 2, 3]), 7 reasonString: 'test', 8 userProperties: { 9 'test': 'test' 10 } 11 } 12}
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.
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
No vulnerabilities found.