Encode and Decode MQTT 3.1.1 packets the node way.
This library is tested with node v4, v6 and v7. The last version to support
older versions of node was mqtt-packet@4.1.2.
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
{
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-device',
keepalive: 0,
username: 'matteo',
password: new Buffer('collina'),
will: {
topic: 'mydevice/status',
payload: new Buffer('dead')
}
}
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
{
cmd: 'connack',
returnCode: 0,
sessionPresent: false
}
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: new Buffer('test'),
retain: false
}
Only the topic
property is 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 property is messageId
, as generate
will throw if
missing.
Pubrec
{
cmd: 'pubcomp',
messageId: 42
}
The only mandatory property is messageId
, as generate
will throw if
missing.
Pubrel
{
cmd: 'pubrel',
messageId: 42
}
The only mandatory property is messageId
, as generate
will throw if
missing.
Pubcomp
{
cmd: 'pubcomp',
messageId: 42
}
The only mandatory property is messageId
, as generate
will throw if
missing.
Pingreq
{
cmd: 'pingreq'
}
Pingresp
{
cmd: 'pingresp'
}
Disconnect
{
cmd: 'disconnect'
}
mqtt-packet is only possible due to the excellent work of the following contributors: