2025-07-08 17:21:25 +08:00
mqtt-packet [](https://travis-ci.org/mqttjs/mqtt-packet)
2025-05-22 16:37:43 +08:00
===========
2025-07-08 17:21:25 +08:00
Encode and Decode MQTT 3.1.1 packets the node way.
2025-05-22 16:37:43 +08:00
[](https://github.com/feross/standard)
* < a href = " #installation " > Installation</ a >
* < a href = " #examples " > Examples</ a >
* < a href = " #packets " > Packets</ a >
* < a href = " #api " > API</ a >
* < a href = " #contributing " > Contributing</ a >
* < a href = " #license " > License & copyright</ a >
2025-07-08 17:21:25 +08:00
This library is tested with node v4, v6 and v7. The last version to support
2025-05-22 16:37:43 +08:00
older versions of node was mqtt-packet@4 .1.2.
Installation
------------
```bash
npm install mqtt-packet --save
```
Examples
--------
### Generating
```js
2025-07-08 17:21:25 +08:00
var mqtt = require('mqtt-packet')
var object = {
2025-05-22 16:37:43 +08:00
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 10,
topic: 'test',
payload: 'test' // Can also be a Buffer
2025-07-08 17:21:25 +08:00
}
2025-05-22 16:37:43 +08:00
console.log(mqtt.generate(object))
// Prints:
//
// < Buffer 30 0a 00 04 74 65 73 74 74 65 73 74 >
//
// Which is the same as:
//
2025-07-08 17:21:25 +08:00
// new Buffer([
2025-05-22 16:37:43 +08:00
// 48, 10, // Header (publish)
// 0, 4, // Topic length
// 116, 101, 115, 116, // Topic (test)
// 116, 101, 115, 116 // Payload (test)
// ])
```
### Parsing
```js
2025-07-08 17:21:25 +08:00
var mqtt = require('mqtt-packet')
var parser = mqtt.parser()
2025-05-22 16:37:43 +08:00
// Synchronously emits all the parsed packets
2025-07-08 17:21:25 +08:00
parser.on('packet', function(packet) {
2025-05-22 16:37:43 +08:00
console.log(packet)
// Prints:
//
// {
// cmd: 'publish',
// retain: false,
// qos: 0,
// dup: false,
// length: 10,
// topic: 'test',
// payload: < Buffer 74 65 73 74 >
// }
})
2025-07-08 17:21:25 +08:00
parser.parse(new Buffer([
2025-05-22 16:37:43 +08:00
48, 10, // Header (publish)
0, 4, // Topic length
116, 101, 115, 116, // Topic (test)
116, 101, 115, 116 // Payload (test)
2025-07-08 17:21:25 +08:00
])
2025-05-22 16:37:43 +08:00
// Returns the number of bytes left in the parser
```
API
---
* < a href = " #generate " >< code > mqtt#< b > generate()</ b ></ code ></ a >
* < a href = " #writeToStream " >< code > mqtt#< b > writeToStream()</ b ></ code ></ a >
* < a href = " #parser " >< code > mqtt#< b > parser()</ b ></ code ></ a >
< a name = "generate" >
2025-07-08 17:21:25 +08:00
### mqtt.generate(object)
2025-05-22 16:37:43 +08:00
Generates a `Buffer` containing an MQTT packet.
The object must be one of the ones specified by the [packets ](#packets )
section. Throws an `Error` if a packet cannot be generated.
< a name = "writeToStream" >
2025-07-08 17:21:25 +08:00
### mqtt.writeToStream(object, stream)
2025-05-22 16:37:43 +08:00
Writes the mqtt packet defined by `object` to the given stream.
The object must be one of the ones specified by the [packets ](#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.
< a name = "parser" >
2025-07-08 17:21:25 +08:00
### mqtt.parser()
2025-05-22 16:37:43 +08:00
Returns a new `Parser` object. `Parser` inherits from `EventEmitter` and
will emit:
* `packet` , when a new packet is parsed, according to
[packets ](#packets )
* `error` , if an error happens
< a name = "parse" >
#### Parser.parse(buffer)
Parses 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` .
Packets
-------
This section describes the format of all packets emitted by the `Parser`
and that you can input to `generate` .
### Connect
```js
{
cmd: 'connect',
2025-07-08 17:21:25 +08:00
protocolId: 'MQTT', // Or 'MQIsdp' in MQTT 3.1
protocolVersion: 4, // Or 3 in MQTT 3.1
2025-05-22 16:37:43 +08:00
clean: true, // Can also be false
clientId: 'my-device',
keepalive: 0, // Seconds which can be any positive number, with 0 as the default setting
username: 'matteo',
2025-07-08 17:21:25 +08:00
password: new Buffer('collina'), // Passwords are buffers
2025-05-22 16:37:43 +08:00
will: {
topic: 'mydevice/status',
2025-07-08 17:21:25 +08:00
payload: new Buffer('dead') // Payloads are buffers
2025-05-22 16:37:43 +08:00
}
}
```
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` .
### Connack
```js
{
cmd: 'connack',
2025-07-08 17:21:25 +08:00
returnCode: 0, // Or whatever else you see fit
sessionPresent: false // Can also be true.
2025-05-22 16:37:43 +08:00
}
```
The only mandatory argument is `returnCode` , as `generate` will throw if
missing.
### Subscribe
```js
{
cmd: 'subscribe',
messageId: 42,
subscriptions: [{
topic: 'test',
2025-07-08 17:21:25 +08:00
qos: 0
2025-05-22 16:37:43 +08:00
}]
}
```
All properties are mandatory.
### Suback
```js
{
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
```js
{
cmd: 'unsubscribe',
messageId: 42,
unsubscriptions: [
'test',
'a/topic'
]
}
```
All properties are mandatory.
### Unsuback
```js
{
cmd: 'unsuback',
2025-07-08 17:21:25 +08:00
messageId: 42
2025-05-22 16:37:43 +08:00
}
```
All properties are mandatory.
### Publish
```js
{
cmd: 'publish',
messageId: 42,
qos: 2,
dup: false,
topic: 'test',
2025-07-08 17:21:25 +08:00
payload: new Buffer('test'),
retain: false
2025-05-22 16:37:43 +08:00
}
```
Only the `topic` property is mandatory.
Both `topic` and `payload` can be `Buffer` objects instead of strings.
`messageId` is mandatory for `qos > 0` .
### Puback
```js
{
cmd: 'puback',
2025-07-08 17:21:25 +08:00
messageId: 42
2025-05-22 16:37:43 +08:00
}
```
The only mandatory property is `messageId` , as `generate` will throw if
missing.
### Pubrec
```js
{
2025-07-08 17:21:25 +08:00
cmd: 'pubcomp',
messageId: 42
2025-05-22 16:37:43 +08:00
}
```
The only mandatory property is `messageId` , as `generate` will throw if
missing.
### Pubrel
```js
{
cmd: 'pubrel',
2025-07-08 17:21:25 +08:00
messageId: 42
2025-05-22 16:37:43 +08:00
}
```
The only mandatory property is `messageId` , as `generate` will throw if
missing.
### Pubcomp
```js
{
cmd: 'pubcomp',
2025-07-08 17:21:25 +08:00
messageId: 42
2025-05-22 16:37:43 +08:00
}
```
The only mandatory property is `messageId` , as `generate` will throw if
missing.
### Pingreq
```js
{
cmd: 'pingreq'
}
```
### Pingresp
```js
{
cmd: 'pingresp'
}
```
### Disconnect
```js
{
2025-07-08 17:21:25 +08:00
cmd: 'disconnect'
2025-05-22 16:37:43 +08:00
}
```
< a name = "contributing" > < / a >
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 ](https://github.com/mqttjs/mqtt-packet/blob/master/CONTRIBUTING.md ) file for more details.
### Contributors
mqtt-packet is only possible due to the excellent work of the following contributors:
< table > < tbody >
< tr >< th align = "left" > Matteo Collina</ th >< td >< a href = "https://github.com/mcollina" > GitHub/mcollina</ a ></ td >< td >< a href = "http://twitter.com/matteocollina" > Twitter/@matteocollina </ a ></ td ></ tr >
< tr >< th align = "left" > Adam Rudd</ th >< td >< a href = "https://github.com/adamvr" > GitHub/adamvr</ a ></ td >< td >< a href = "http://twitter.com/adam_vr" > Twitter/@adam_vr </ a ></ td ></ tr >
< tr >< th align = "left" > Peter Sorowka</ th >< td >< a href = "https://github.com/psorowka" > GitHub/psorowka</ a ></ td >< td >< a href = "http://twitter.com/psorowka" > Twitter/@psorowka </ a ></ td ></ tr >
< / tbody > < / table >
License
-------
MIT