Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
mqtt-packet
Advanced tools
The mqtt-packet npm package is a utility for encoding and decoding packets used in MQTT (Message Queuing Telemetry Transport), which is a lightweight messaging protocol for small sensors and mobile devices. It is designed for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
Parsing MQTT Packets
This feature allows you to parse MQTT packets from a buffer. The code sample demonstrates how to create a parser instance, listen for 'packet' events, and parse a buffer containing an MQTT packet.
const mqttPacket = require('mqtt-packet');
const parser = mqttPacket.parser();
parser.on('packet', function(packet) {
console.log(packet);
});
parser.parse(Buffer.from([0x30, 0x02, 0x00, 0x00]));
Generating MQTT Packets
This feature allows you to generate MQTT packets. The code sample shows how to create an MQTT packet object and then generate a buffer that can be sent over a network.
const mqttPacket = require('mqtt-packet');
const packet = {
cmd: 'publish',
messageId: 42,
topic: 'test/topic',
payload: 'test payload',
qos: 0,
retain: false
};
const buffer = mqttPacket.generate(packet);
console.log(buffer);
The 'mqtt' package is a client library for the MQTT protocol. It provides an API for connecting to an MQTT broker, publishing messages, and subscribing to topics. It is more feature-rich than mqtt-packet as it includes the full client functionality, not just packet encoding and decoding.
The 'mqtt-connection' package is a barebone connection package for MQTT that can be used when implementing an MQTT client or server. It provides a simple stream interface for sending and receiving MQTT packets over a network. It is similar to mqtt-packet but includes stream handling capabilities.
The 'aedes-packet' package is a fork of mqtt-packet, which is used by the Aedes MQTT broker. It has similar functionalities to mqtt-packet but is tailored to work seamlessly with the Aedes broker ecosystem.
Encode and Decode MQTT packets the node way.
This library has been extracted and refactored from MQTT.js. Thanks Adam Rudd for the great module.
npm install mqtt-packet --save
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)
// ])
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
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 happensThe 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.
This section describes the format of all packets emitted by the Parser
and that you can input to generate
.
{
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.
{
cmd: 'connack'
, returnCode: 0 // or whatever else you see fit
}
The only mandatory argument is returnCode
, as generate
will throw if
missing.
{
cmd: 'subscribe'
, messageId: 42
, subscriptions: [{
topic: 'test'
, qos: 0
}]
}
All properties are mandatory.
{
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.
{
cmd: 'unsubscribe'
, messageId: 42
, unsubscriptions: [
'test'
, 'a/topic'
]
}
All properties are mandatory.
{
cmd: 'unsuback'
, messageId: 42
}
All properties are mandatory.
{
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
.
{
cmd: 'puback'
, messageId: 42
}
The only mandatory argument is messageId
, as generate
will throw if
missing.
{
cmd: 'pubcomp'
, messageId: 42
}
The only mandatory argument is messageId
, as generate
will throw if
missing.
{
cmd: 'pubrel'
, messageId: 42
}
The only mandatory argument is messageId
, as generate
will throw if
missing.
{
cmd: 'pubcomp'
, messageId: 42
}
The only mandatory argument is messageId
, as generate
will throw if
missing.
{
cmd: 'pingreq'
}
{
cmd: 'pingresp'
}
{
cmd: 'pingresp'
}
MIT
FAQs
Parse and generate MQTT packets like a breeze
The npm package mqtt-packet receives a total of 611,673 weekly downloads. As such, mqtt-packet popularity was classified as popular.
We found that mqtt-packet demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.