parse-mqttv5-packet
is a JavaScript library designed for parsing MQTTv5 packets, offering a simple and efficient solution for working with the MQTT protocol in JavaScript applications.
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol, ideal for communication between devices on low-bandwidth networks and with limited resources. It's commonly used in IoT (Internet of Things) applications to facilitate device-to-device communication.
- Support for parsing various MQTTv5 packet types.
- Lightweight and easy to integrate.
- Detailed extraction of information from each packet type.
Install parse-mqttv5-packet
via npm:
npm install parse-mqttv5-packet
Here's a basic example of how to use parse-mqttv5-packet to parse a Connect packet:
const { parse } = require('parse-mqttv5-packet');
const packet = Buffer.from([/* MQTT packet */]);
const parsedData = parse(packet);
console.log(parsedData);
The parse-mqttv5-packet library provides specific parsers for different MQTTv5 packet types and is capable of autonomously determining the type of MQTT message received, allowing it to be processed by the correct specific parser:
Byte 1: Packet type and flags (fixed byte for ConnAck).
Byte 2: Remaining length.
Byte 3: Acknowledge flags.
Byte 4: Connection return code.
The parseConnAck parser extracts the connection return code and acknowledge flags to determine the connection status and communicate it to the client.
Bytes 1-2: Packet type and remaining length.
Bytes 3-x: Protocol Name.
Byte x+1: Protocol Version.
Byte x+2: Connect flags.
Bytes x+3-x+4: Keep Alive timer.
Bytes x+5-end: Payload (Client Identifier, Will Topic, Will Message, Username, Password).
The parseConnect parser extracts and interprets various components such as the client identifier, Will message topic, Will message, username, and password.
Byte 1: Packet type and DUP, QoS, and RETAIN flags.
Bytes 2-x: Remaining length.
Bytes x+1-x+n: Topic name.
Bytes x+n+1-x+n+2 (if QoS > 0): Packet Identifier.
Bytes x+n+3-end: Message payload.
The parsePublish parser extracts the topic, packet identifier (if QoS > 0), and message payload, handling the DUP, QoS, and RETAIN flags to determine how the message should be treated.
Byte 1: Packet type. Byte 2: Remaining length. Bytes 3-4: Packet Identifier.
The parsePubAck parser extracts the packet identifier, which is crucial for correlating the acknowledgment with its original publish message.
Packet Structure:
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
The parsePubRec parser is used in QoS 2 message flow, extracting the packet identifier to acknowledge that a publish message was received.
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
The parsePubRel parser is part of the QoS 2 protocol flow, indicating that the receiver can release the relevant message, identified by the packet identifier.
Packet Structure:
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
The parsePubComp parser concludes the QoS 2 message flow by acknowledging that the message was published completely, identified by the packet identifier.
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
Byte 5-end: Payload with return codes for each topic.
The parseSubAck parser extracts the packet identifier and the return codes for each topic subscription, indicating the subscription's success or failure.
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
Byte 5-end: Payload with topic filters and QoS levels.
The parseSubscribe parser extracts the packet identifier and a list of topic filters with their corresponding QoS levels, indicating the topics the client wants to subscribe to.
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
The parseUnSubAck parser extracts the packet identifier, acknowledging the receipt of an unsubscribe request.
Packet Structure:
Byte 1: Packet type.
Byte 2: Remaining length.
Bytes 3-4: Packet Identifier.
Byte 5-end: Payload with topic filters.
The parseUnSubscribe parser extracts the packet identifier and a list of topic filters, indicating the topics the client wants to unsubscribe from.
Contributions are welcome! Feel free to submit a pull request or create an issue if you have suggestions or find any bugs.
This project is licensed under the MIT License - see the LICENSE file for details.