This go package is an encoder/decoder library for MQTT 3.1.1 messages.
Get it using go's standard toolset:
$ go get github.com/gomqtt/message
- Extremely simple to use.
- Zero allocation encoding/decoding.
- Builtin message detection.
- No overflows thanks to input fuzzing.
- Non-intrusive design.
Create a new message and encode it:
// Create new message.
msg1 := NewConnectMessage()
msg1.Username = []byte("gomqtt")
msg1.Password = []byte("amazing!")
// Allocate buffer.
buf := make([]byte, msg1.Len())
// Encode the message.
if _, err := msg1.Encode(buf); err != nil {
// there was an error while encoding
panic(err)
}
Decode bytes to a message:
// Detect message.
l, mt := DetectMessage(buf)
// Check length
if l == 0 {
// buffer not complete yet
return
}
// Create message.
msg2, err := mt.New()
if err != nil {
// message type is invalid
panic(err)
}
// Decode message.
_, err = msg2.Decode(buf)
if err != nil {
// there was an error while decoding
panic(err)
}
More details can be found in the documentation.
This package has been originally extracted and contributed by @zhenjl from the surgemq project.