MQTT-Wrapper package provides easy-to-use MQTTv3 and MQTTv5 connection for Go projects.
It supports Request/Response pattern for MQTTv5 connection.
import(
mqtt "github.com/alihanyalcin/mqtt-wrapper"
)
config := mqtt.Config{
Brokers: []string{"127.0.0.1:1883"},
ClientID: "",
Username: "",
Password: "",
Topics: []string{"topic"},
QoS: 0,
Retained: false,
AutoReconnect: false,
MaxReconnectInterval: 5,
PersistentSession: false,
KeepAlive: 15,
TLSCA: "",
TLSCert: "",
TLSKey: "",
Version: mqtt.V3, // use mqtt.V5 for MQTTv5 client.
}
client, err := config.CreateConnection()
if err != nil {
log.Fatal(err)
}
client.Disconnect()
err = client.Publish("topic", "message")
if err != nil {
log.Fatal(err)
}
client.Handle(func(topic string, payload []byte) {
log.Printf("Received on [%s]: '%s'", topic, string(payload))
})
Check examples for more information about MQTTv5 client.