Package Exports
- mqtt-packet
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mqtt-packet) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mqtt-packet 
Encode and Decode MQTT packets the node way.
Acknowledgements
This library has been extracted and refactored from MQTT.js. Thanks Adam Rudd for the great module.
Installation
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'
}
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: 'test'
// }
})
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
Streams
var mqtt = require('mqtt-packet')
, assert = require('assert')
, parser = mqtt.parseStream()
, generator = mqtt.generateStream()
, object = {
cmd: 'publish'
, retain: false
, qos: 0
, dup: false
, length: 10
, topic: 'test'
, payload: 'test'
}
generator.pipe(parser)
parser.on('data', function(packet) {
assert.deepEqual(packet, object, 'expected packet')
})
generator.end(object)
Duplex Wrapper
var mqtt = require('mqtt-packet')
, assert = require('assert')
, through = require('through2')
, connection = mqtt.connection(through())
, object = {
cmd: 'publish'
, retain: false
, qos: 0
, dup: false
, length: 10
, topic: 'test'
, payload: 'test'
}
connection.on('data', function(packet) {
assert.deepEqual(packet, object, 'expected packet')
})
connection.end(object)
API
### mqtt.generate(object)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 packetserror
, if an error happens
The only allowed option is { encoding: 'binary' }
which will block the
automatic parsing of all the strings in the package. Instead, the
strings will remain 'raw', i.e. a Buffer
.
Parse a given Buffer
and emits synchronously all the MQTT packets that
are included. Returns the number of bytes left to parse.
Returns a Transform
stream that calls generate()
.
The stream is configured into object mode.
Returns a Transform
stream that embeds a Parser
and calls Parser.parse()
for each new Buffer
. The stream is configured into object mode. It
accepts the same options of parser(opts)
.
Wraps a duplex using reduplexer
so that
the user can both write and read an object, as defined in packets.
It accepts the same options of parser(opts)
.
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: 'collina'
, will: {
topic: 'mydevice/status'
, payload: 'dead'
}
}
The only mandatory argument is clientId
, as generate
will throw if
missing.
Connack
{
cmd: 'connack'
, returnCode: 0 // or whatever else you see fit
}
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: '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
.
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'
}
License
MIT