Skip to content

taoyonggang/message

Repository files navigation

gomqtt/message

Circle CI GoDoc Release

This go package is an encoder/decoder library for MQTT 3.1.1 messages.

Installation

Get it using go's standard toolset:

$ go get github.com/gomqtt/message

Features

  • Extremely simple to use.
  • Zero allocation encoding/decoding.
  • Builtin message detection.
  • No overflows thanks to input fuzzing.
  • Non-intrusive design.

Usage

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.

Credits

This package has been originally extracted and contributed by @zhenjl from the surgemq project.

About

go encoder/decoder library for MQTT 3.1.1 messages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages