Skip to content

Go module for encoding and decoding v3 and v5 MQTT packets

License

Notifications You must be signed in to change notification settings

matevzmihalic/mqttpackets

Repository files navigation

MQTT packets Go Reference

is a Go module for decoding and encoding v3 and v5 MQTT packets. It's based on https://github.com/eclipse/paho.golang which supports only v5 version and also includes MQTT client.

Installation

Install with the standard go tools:

go get github.com/matevzmihalic/mqttpackets

Basic usage

import "github.com/matevzmihalic/mqttpackets"

// read packet from incoming connection
packet, err := mqttpackets.ReadPacket(inConn, 0)
if err != err {
	return err
}

// first packet in MQTT connection should always be CONNECT packet
connectPacket, ok := packet.Content.(*mqttpackets.Connect)
if !ok {
    return fmt.Errorf("first packet should be CONNECT")
}

// get version from CONNECT packet and save it for later
version := connectPacket.ProtocolVersion

// write the first packet to outgoing connection
_, err := packet.WriteTo(outConn)
if err != err {
    return err
}

// read next packet with the correct MQTT version for the connection
packet, err = mqttpackets.ReadPacket(inConn, version)
if err != err {
    return err
}

// create a custom PUBLISH packet
newPacket := mqttpackets.NewControlPacket(mqttpackets.PUBLISH, version)
publishPacket := newPacket.Content.(*mqttpackets.Publish)
publishPacket.Topic = "sensor/temp"
publishPacket.QoS = 1
publishPacket.Payload = []byte("23.56")

_, err = newPacket.WriteTo(outConn)
if err != err {
    return err
}

About

Go module for encoding and decoding v3 and v5 MQTT packets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages