Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/beary/mqtt-parser

Split and parse MQTT packet from buffer.
https://github.com/beary/mqtt-parser

mqtt

Last synced: 17 days ago
JSON representation

Split and parse MQTT packet from buffer.

Awesome Lists containing this project

README

        

# MQTT-parser
Split and parse MQTT packet from buffer.
> MQTT protocol document
>
> EN: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
>
> ZH: https://mcxiaoke.gitbooks.io/mqtt-cn/content/

---
## Usage
```javascript
/* javascript */
const mqttParser = require('mqtt-parser')
const connectBuffer = Buffer.from([
16, 56, // Header
0, 4, // Protocol ID length
77, 81, 84, 84, // Protocol ID
3, // Protocol version
246, // Connect flags
0, 30, // Keepalive
0, 5, // Client ID length
104, 101, 108, 108, 111, // Client ID
0, 6, // Will topic length
228, 189, 160, 229, 165, 189, // Will topic
0, 8, // Will payload length
112, 97, 121, 108, 111, 97, 100, 126, // Will payload
0, 8, // Username length
117, 115, 101, 114, 110, 97, 109, 101, // Username
0, 9, // Password length
112, 52, 36, 36, 119, 48, 194, 163, 100 // Password
])

let packets = mqttParser.parse(connectBuffer)
console.log(packets[0])
/*
{ packetType: 1,
flags: 0,
remainLength: 56,
buffer: ,
protocolName: 'MQTT',
protocolLevel: 3,
connectFlags: {
username: 1,
password: 1,
willRetain: 1,
willQoS: 2,
willFlag: 1,
cleanSession: 1,
reserved: 0 },
keepAlive: 30,
clientId: 'hello',
willTopic: '你好',
willMessage: 'payload~',
username: 'username',
password: 'p4$$w0£d'
}
}
*/

packets = mqttParser.parse(Buffer.concat([connectBuffer, connectBuffer, ...otherTypesPacket]))
console.log(packets)
/*
[{ packetType: 1, ... },
{ packetType: 1, ... },
...]
*/
```
```typescript
/* typescript */
import { parse, PacketType, Packet } from 'mqtt-parser'
const packets = mqttParser.parse(Buffer.from(connectBuffer)) // packets: BasePacket[]
if (packets[0].packetType === PacketType.CONNECT) {
let packet = packet[0] as Packet.Connect
packet.willTopic // code complete
}
```
---
## API
### parse(buffer: Buffer): BasePacket[]