Code samples

Go MQTT example

Connect Go to an MQTT broker with the Eclipse Paho Go client. Start on the free public broker, then point the same code at a secure private broker on ConnectMQ.

Install
go get github.com/eclipse/paho.mqtt.golang
Source code on GitHubmqtt-code-samples/goClone the complete, runnable examples. Stars and pull requests welcome. View on GitHub

1. Try it on the free public broker

No account needed: connect to broker.connectmq.com on port 1883 and start publishing in seconds.

main.go
package main

import (
	"fmt"
	"time"

	mqtt "github.com/eclipse/paho.mqtt.golang"
)

func main() {
	topic := "your-project/sensors/temperature" // Use a unique topic prefix: everyone can read the public broker

	// Connect to the free public broker (no account needed)
	opts := mqtt.NewClientOptions().AddBroker("tcp://broker.connectmq.com:1883")
	// Every client needs a unique client ID
	opts.SetClientID(fmt.Sprintf("go-%d", time.Now().UnixNano()))
	// Reconnect automatically if the connection drops
	opts.SetAutoReconnect(true)
	// Runs when the connection is established
	opts.SetOnConnectHandler(func(c mqtt.Client) {
		// Subscribe to the temperature topic
		c.Subscribe(topic, 1, func(_ mqtt.Client, m mqtt.Message) {
			fmt.Printf("%s: %s\n", m.Topic(), m.Payload())
		})
	})

	client := mqtt.NewClient(opts)
	if t := client.Connect(); t.Wait() && t.Error() != nil {
		panic(t.Error())
	}
	// Publish a JSON reading
	client.Publish(topic, 1, false, `{"temperature":21.5}`).Wait()
	// Keep the client running
	select {}
}

Free public MQTT broker

The public broker is shared: anyone can subscribe to your topics. Never send passwords, personal data or anything confidential through it.

About the free public MQTT broker

2. Go to production with ConnectMQ

For real devices, use a private serverless MQTT broker on ConnectMQ instead of the public one: TLS encryption, a username and password for every device and per-topic access control, fully managed. Start on the Free plan — no card required.

Replace YOUR_BROKER_HOST, YOUR_USERNAME and YOUR_PASSWORD with the values from your ConnectMQ dashboard.

main.go
package main

import (
	"crypto/tls"
	"fmt"
	"time"

	mqtt "github.com/eclipse/paho.mqtt.golang"
)

func main() {
	topic := "your-project/sensors/temperature"

	// Connect to your private ConnectMQ broker on port 8883
	opts := mqtt.NewClientOptions().AddBroker("ssl://YOUR_BROKER_HOST:8883")
	// Username and password from your ConnectMQ dashboard
	opts.SetUsername("YOUR_USERNAME")
	opts.SetPassword("YOUR_PASSWORD")
	// TLS: the system CA store verifies the broker certificate
	opts.SetTLSConfig(&tls.Config{MinVersion: tls.VersionTLS12})
	// Every client needs a unique client ID
	opts.SetClientID(fmt.Sprintf("go-%d", time.Now().UnixNano()))
	// Reconnect automatically if the connection drops
	opts.SetAutoReconnect(true)
	// Runs when the connection is established
	opts.SetOnConnectHandler(func(c mqtt.Client) {
		// Subscribe to the temperature topic
		c.Subscribe(topic, 1, func(_ mqtt.Client, m mqtt.Message) {
			fmt.Printf("%s: %s\n", m.Topic(), m.Payload())
		})
	})

	client := mqtt.NewClient(opts)
	if t := client.Connect(); t.Wait() && t.Error() != nil {
		panic(t.Error())
	}
	// Publish a JSON reading
	client.Publish(topic, 1, false, `{"temperature":21.5}`).Wait()
	// Keep the client running
	select {}
}

Go MQTT FAQ

How do I connect Go to an MQTT broker?

Install Eclipse Paho Go, connect to the broker host on port 1883 (or 8883 with TLS), then subscribe to a topic and publish messages to it. The examples on this page connect to the free public broker broker.connectmq.com and to a private ConnectMQ broker.

Is there a free MQTT broker for testing Go?

Yes. broker.connectmq.com is a free public MQTT broker that needs no account. Anyone can read its topics, so use it only for tests and use a unique topic prefix.

How do I secure a Go MQTT connection?

Connect over TLS on port 8883 with a username and password from your ConnectMQ dashboard, and give every device its own credentials and topic permissions instead of using the public broker.

Free public MQTT broker

Test any example on broker.connectmq.com — no sign-up, MQTT 3.1.1 and 5, TCP, TLS and WebSocket.

About the free public MQTT broker

Secure serverless MQTT for production

When it is time for real devices, run the same code on a private, fully managed ConnectMQ broker with TLS and per-device credentials.

Start for free

Your private MQTT broker is a few clicks away

Create an account, get your broker credentials and connect your first device — no servers to install, patch or scale.