Go MQTT with RabbitMQ and LavinMQ
We recommend paho.mqtt.golang, the Eclipse Paho MQTT client for Go.
go get github.com/eclipse/paho.mqtt.golang
Publish and subscribe
Set MQTT_HOST, MQTT_PORT, MQTT_USERNAME and MQTT_PASSWORD to the MQTT
details on your instance’s details page in the CloudAMQP console. Port 8883
uses TLS. Without them, the examples connect to a local broker.
Save the publisher as publisher/main.go:
package main
import (
"cmp"
"fmt"
"log"
"os"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
scheme, port := "tcp", cmp.Or(os.Getenv("MQTT_PORT"), "1883")
if port == "8883" {
scheme = "ssl"
}
opts := mqtt.NewClientOptions().
AddBroker(fmt.Sprintf("%s://%s:%s", scheme, cmp.Or(os.Getenv("MQTT_HOST"), "localhost"), port)).
SetUsername(cmp.Or(os.Getenv("MQTT_USERNAME"), "guest")).
SetPassword(cmp.Or(os.Getenv("MQTT_PASSWORD"), "guest"))
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
defer client.Disconnect(250)
// QoS 1 waits for the broker's PUBACK
if token := client.Publish("hello", 1, false, "Hello world!"); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
fmt.Println(" [x] Sent 'Hello world!'")
}
Save the subscriber as worker/main.go:
package main
import (
"cmp"
"fmt"
"log"
"os"
"os/signal"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
scheme, port := "tcp", cmp.Or(os.Getenv("MQTT_PORT"), "1883")
if port == "8883" {
scheme = "ssl"
}
opts := mqtt.NewClientOptions().
AddBroker(fmt.Sprintf("%s://%s:%s", scheme, cmp.Or(os.Getenv("MQTT_HOST"), "localhost"), port)).
SetUsername(cmp.Or(os.Getenv("MQTT_USERNAME"), "guest")).
SetPassword(cmp.Or(os.Getenv("MQTT_PASSWORD"), "guest"))
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
defer client.Disconnect(250)
// Each message is acked when the handler returns
token := client.Subscribe("hello", 1, func(_ mqtt.Client, msg mqtt.Message) {
fmt.Printf(" [x] Received '%s'\n", msg.Payload())
})
if token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
fmt.Println(" [*] Waiting for messages. To exit press CTRL+C")
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
}
Run go run ./worker in one terminal and go run ./publisher in another.
A runnable example is in go/paho-mqtt in cloudamqp/examples.
Good to know
- Put the vhost in the username, as
vhost:user. MQTT has no vhost of its own, and the console shows the username that way. - Use
ssl://for TLS, as the examples do on port 8883. The server certificate is verified. - Use QoS 1, so the publish token completes once the broker has sent its PUBACK, and each message is acked when the handler returns.
- The client reconnects automatically, but with a clean session the
subscriptions are gone after a reconnect. Subscribe in a handler set with
SetOnConnectHandlerto subscribe again on every connect.
Was this page helpful?
Thanks for your feedback!