Go Client

The Paho Go Client provides an MQTT client library for connection to MQTT brokers via TCP, TLS or WebSockets

Source

http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.golang.git/

Download

Once you have installed Go and configured your environment you can install the Paho Go client by running;

go get git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git

Documentation

API documentation for the Paho Go client is available at Alternatively, once you have downloaded the src via go get you can run godoc -http=":6060" and navigate to http://localhost:6060 to browse the documentation locally

Getting Started

The client can connect to a broker using TCP, TLS or a WebSocket connection. Ensure the broker you're using supports the connection type you wish to use.

The type of connection required is specified by the scheme of the connection URL set in the ClientOptions struct, for example:

  • tcp://iot.eclipse.org:1883 - connect to iot.eclipse.org on port 1883 using plain TCP
  • ws://iot.eclipse.org:1883 - connect to iot.eclipse.org on port 1883 using WebSockets
  • tls://iot.eclipse.org:8883 - connect to iot.eclipse.org on port 8883 using TLS (ssl:// and tcps:// are synonyms for tls://)

Here is a very simple example that subscribes to a topic and publishes 5 messages:

package main

import (
  "fmt"
  //import the Paho Go MQTT library
  MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
  "os"
  "time"
)

//define a function for the default message handler
var f MQTT.MessageHandler = func(msg MQTT.Message) {
  fmt.Printf("TOPIC: %s\n", msg.Topic())
  fmt.Printf("MSG: %s\n", msg.Payload())
}

func main() {
  //create a ClientOptions struct setting the broker address, clientid, turn
  //off trace output and set the default message handler
  opts := MQTT.NewClientOptions().SetBroker("tcp://iot.eclipse.org:1883")
  opts.SetClientId("go-simple")
  opts.SetTraceLevel(MQTT.Off)
  opts.SetDefaultPublishHandler(f)

  //create and start a client using the above ClientOptions
  c := MQTT.NewClient(opts)
  _, err := c.Start()
  if err != nil {
    panic(err)
  }

  //subscribe to the topic /go-mqtt/sample and request messages to be delivered
  //at a maximum qos of zero, wait for the receipt to confirm the subscription
  if receipt, err := c.StartSubscription(nil, "/go-mqtt/sample", MQTT.QOS_ZERO); err != nil {
    fmt.Println(err)
    os.Exit(1)
  } else {
    <-receipt
  }

  //Publish 5 messages to /go-mqtt/sample at qos 1 and wait for the receipt
  //from the server after sending each message
  for i := 0; i < 5; i++ {
    text := fmt.Sprintf("this is msg #%d!", i)
    receipt := c.Publish(MQTT.QOS_ONE, "/go-mqtt/sample", text)
    <-receipt
  }

  time.Sleep(3 * time.Second)

  //unsubscribe from /go-mqtt/sample
  if receipt, err := c.EndSubscription("/go-mqtt/sample"); err != nil {
    fmt.Println(err)
    os.Exit(1)
  } else {
    <-receipt
  }

  c.Disconnect(250)
}