MQTT Go Client library

Eclipse Paho MQTT Go Client is the Go language client library under the Eclipse Paho project, which can connect to the MQTT Broker to publish messages, subscribe to topics and receive the published message. It supports asynchronous operation mode completely.

The client depends on Google's software Package of proxy and websockets, which can be installed with the following command:

go get

MQTT Go usage example

This example contains the complete code for Paho MQTT in Go language connecting to EMQ X Broker, sending and receiving messages:

package main

import (


var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
    fmt.Printf("TOPIC: %s\n", msg.Topic())
    fmt.Printf("MSG: %s\n", msg.Payload())

func main() {
    mqtt.DEBUG = log.New(os.Stdout, "", 0)
    mqtt.ERROR = log.New(os.Stdout, "", 0)
    opts := mqtt.NewClientOptions().AddBroker("tcp://").SetClientID("emqx_test_client")

    opts.SetKeepAlive(60 * time.Second)
    // Set the message callback handler
    opts.SetPingTimeout(1 * time.Second)

    c := mqtt.NewClient(opts)
    if token := c.Connect(); token.Wait() && token.Error() != nil {

    // Subscribe to a topic
    if token := c.Subscribe("testtopic/#", 0, nil); token.Wait() && token.Error() != nil {

    // Publish a message
    token := c.Publish("testtopic/1", 0, false, "Hello World")

    time.Sleep(6 * time.Second)

    // Unscribe
    if token := c.Unsubscribe("testtopic/#"); token.Wait() && token.Error() != nil {

  // Disconnect
    time.Sleep(1 * time.Second)

Paho Golang MQTT 5.0 support

Currently, Paho Golang is still adapting to MQTT 5.0 and has not yet fully supported it.

results matching ""

    No results matching ""