Skip to content

alihanyalcin/mqtt-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Go Report Card

MQTT-Wrapper

MQTT-Wrapper package provides easy-to-use MQTTv3 and MQTTv5 connection for Go projects.

It supports Request/Response pattern for MQTTv5 connection.

Usage

  • Import the package
import( 
    mqtt "github.com/alihanyalcin/mqtt-wrapper"
)
  • Create a local variable using MQTTConfig struct and fill the necessary fields for the connection.
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.
}
  • Then, create a connection to MQTT broker(s).
client, err := config.CreateConnection()
if err != nil {
    log.Fatal(err)
}
  • To disconnect from broker(s)
client.Disconnect()
  • To publish a message to broker(s) with a specific topic
err = client.Publish("topic", "message")
if err != nil {
    log.Fatal(err)
}
  • To handle new messages
client.Handle(func(topic string, payload []byte) {
    log.Printf("Received on [%s]: '%s'", topic, string(payload))
})

Check examples for more information about MQTTv5 client.