Working with MQTT in Go

By Timothy J. Raymond

Elevator Pitch

MQTT is a better way for your Go applications to communicate with IoT devices. Using the paho.mqtt.golang library, we’ll query some example sensors through an MQTT broker, explaining concepts of the protocol along the way and explore the protocol’s advantages to HTTP for IoT devices.


IoT devices aren’t like your typical JSON-spewing microservice—they’re constrained in a variety of ways. One end of the spectrum has devices that run Linux, another has devices that are encumbered even by holding onto a string for too long. MQTT is a message queue that allows devices across this spectrum to communicate and provides services to place underpowered or intermittent devices on equal footing with an always-on cloud-hosted instance. The paho.mqtt.golang package allows our Go applications to perform queries across MQTT topics to select ranges of sensors, and send control messages to devices. We’ll explore how to do all of this, along with some best practices based on the environment your Go application is running in.


I added support for MQTT to Kapacitor, an open-source alerting component of the TICK stack. In the process, I had to learn about MQTT, and sift through best practices to figure out a good configuration for Kapacitor to use. I’m also working on deploying MQTT for a side-project to control air conditioners, fans, and other random things in my apartment with the growing mass of sensors, Arduinos, Raspberry Pis, etc. that I have in my closet.