This page looks best with JavaScript enabled

MQTT basics and how to get started on Ubuntu

 ·   ·  ☕ 8 min read

What is MQTT

MQTT (Message Queue Telemetry Transport) is a lightweight, publish-subscribe messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. It is particularly well-suited for IoT (Internet of Things) devices and applications that require the transmission of small amounts of data over long periods of time.

I always see MQTT as a sibling of AMQP. They are both messaging protocol. We’ll do a comparision later in the post.

MQTT works by using a publish-subscribe model, where a client (e.g. an IoT device) publishes data to a broker (a server that receives and stores data) and another client subscribes to receive updates from the broker. The broker acts as a central hub, routing messages between clients and ensuring that only subscribed clients receive the data they are interested in.

Why MQTT

  • Low overhead. One of the key benefits of MQTT is its low overhead. It uses a compact binary message format and requires minimal network resources, making it ideal for use on low-bandwidth or constrained networks. It is also designed to be efficient in terms of battery usage, making it well-suited for use on battery-powered devices.

  • Scalable and Flexible. MQTT is also highly scalable and flexible, with support for multiple topics and the ability to handle millions of connections. This makes it well-suited for use in large-scale IoT deployments, where there may be a need to handle a large number of connected devices.

  • Secure In addition, MQTT has built-in support for security and authentication, with the ability to use SSL/TLS to encrypt data in transit and authenticate clients. This is important for ensuring the privacy and security of IoT devices and data.

MQTT vs AMQP

One of the main differences between MQTT and AMQP is the scope of their capabilities. MQTT is a lightweight protocol that is designed specifically for IoT applications and other scenarios where low-bandwidth, high-latency, or unreliable networks are a concern. It has a compact binary message format and requires minimal network resources, making it well-suited for use on constrained networks. In contrast, AMQP is a more feature-rich protocol that is designed for use in enterprise environments and has a more complex message format. It is generally better suited for use in high-bandwidth, low-latency networks.

Another difference between MQTT and AMQP is the way in which they handle messaging. MQTT uses a publish-subscribe model, where a client (e.g. an IoT device) publishes data to a broker (a server that receives and stores data) and another client subscribes to receive updates from the broker. In contrast, AMQP uses a more traditional message queuing model, where clients send messages to a queue and another client consumes those messages.

In terms of security, both MQTT and AMQP support the use of SSL/TLS to encrypt data in transit and authenticate clients. However, MQTT has more limited support for security features such as access control and authentication compared to AMQP.

Overall, while both MQTT and AMQP are useful messaging protocols, they are designed for different use cases and have different capabilities. MQTT is a lightweight, efficient protocol that is well-suited for use in IoT applications and other scenarios where low-bandwidth, high-latency, or unreliable networks are a concern. AMQP is a more feature-rich protocol that is better suited for use in enterprise environments and high-bandwidth, low-latency networks.

Digging Deeper into MQTT

To get a hang of the protocol, you need to establish these keywords in your mind.

  • Topic: This is data structure where messages go through. One message can be part of many topics.
  • Client: Client can be either produces, which post messages to topic. Or, subscriber, that listens on a topic for messages.
  • Broker: Broker is the server part. All topics exist on broker. Clients can either post to it or subscribe to it.

Install

We have leaned a lot now. Now we need to do some practicle to retain our knowledge for a long time. I am a Debian fan. I use Ubuntu for both the server and my local computer. I’ll show how can we install:

Now you can install it on your raspberry pi. But here I’m going to install on one of my VPC.

  1. First, update the package manager index by running the following command:
sudo apt update
  1. Next, install mosquitto and its dependencies by running the following command:
sudo apt install mosquitto mosquitto-clients
  1. Once the installation is complete, you can start the mosquitto service by running the following command:
sudo systemctl start mosquitto
  1. To make sure that mosquitto starts automatically when the system boots up, you can enable the service by running the following command:
sudo systemctl enable mosquitto
  1. To check the status of the mosquitto service, you can run the following command:
sudo systemctl status mosquitto

This was for the installation part, let’s see how can we use the accompanying CLI provided with the package:

CLI sub/pub

  1. If you want to test that mosquitto is working correctly, you can use the mosquitto_sub and mosquitto_pub utilities to publish and subscribe to messages. For example, to subscribe to the test topic, you can run the following command:
mosquitto_sub -h localhost -t test

To publish a message to the test topic, you can run the following command:

mosquitto_pub -h localhost -t test -m "Hello, World!"

This should cause the message Hello, World! to be displayed in the terminal window where you are running the mosquitto_sub command.

Post-install config

As you know, I have install mosquitto broker on a remote public machine. There is an additional step if you too have this kind of setup. What I have found out is, even if you client is reachable to the broker, you need to enable broker to allow client from different host.

I am using below configuration. Please note that this is not the most secure setup out these. This is just for testing. Please let me know if want me to cover that in an another post.

For now, please stick this to your /etc/mosquitto/mosquitto.conf:

listener 1883
allow_anonymous true
protocol mqt

Now you can connect your local client to the remote broker.

But that is not enough. Working from CLI is just for demonstration. In real life you’d be using different libraries in different language to interact with brokers.

In coming sections we’ll see demos in some of the most popular language of time.

Python sub/pub

To be able to publish or subscribe to a MQTT topic, you first have some library on your local computer. paho is one of the most popular such library available in the wild.

Let us first install paho:

pip install paho-mqtt

We can now write our clients.

subscriber.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys
import paho.mqtt.client as paho

def on_message(client, userdata, msg):
    print(msg.topic + ": " + msg.payload.decode())

client = paho.Client()
client.on_message = on_message

if client.connect("localhost", 1883, 60) != 0:
    print("Couldn't connect to MQTT broker!")
    sys.exit(-1)

client.subscribe("test/status")

try:
    print("Press CTRL+C to exit")
    client.loop_forever()
except:
    print("Exiting...")

client.disconnect()

You should replace localhost with some public IP or hostname.

As you can see, there’s a on_message function which is called everytime a new message is seen on the topic. We are connecting to the broker in next few lines. And later on, we subscribe to the topc test/status. loop_forever tells the client to do not exit the script.

The code is pretty self explanatory.

Now let us see the publisher.

publisher.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import sys
import paho.mqtt.client as paho

client = paho.Client()

if client.connect("localhost", 1883, 60) != 0:
    print("Couldn't connect to MQTT broker!")
    sys.exit(-1)

client.publish("test/status", "Hello World from paho-mqtt!")

client.disconnect()

This publisher code is also pretty self explanatory. We are publishing Hello World from paho-mqtt! to test/status topic.

You can run the subscriber in a different terminal and then publisher in the other. You can see that you see a new message.

In a real life message, you’d process those message and check if server needs to perform any action based on the content of the message.

Golang sub/pub

  1. The library which is used in Go world is eclipse/paho.mqtt.golang. You can get it via below command:
go get github.com/eclipse/paho.mqtt.golang
  1. Next, create a new Go file and import the MQTT library:
1
import "github.com/eclipse/paho.mqtt.golang"
  1. To create a new MQTT client, you can use the following code:
1
client := mqtt.NewClient(mqtt.NewClientOptions().AddBroker("tcp://localhost:1883"))

I have added localhost above, but use the address of your broker.

  1. To subscribe to a topic, you can use the following code:
1
2
3
4
5
6
token := client.Subscribe("mytopic", 0, func(client mqtt.Client, msg mqtt.Message) {
  // This function is called whenever a message is received
  fmt.Printf("Received message on topic %s: %s\n", msg.Topic(), string(msg.Payload()))
})

token.Wait()

This code subscribes to the “mytopic” topic and sets up a callback function that is called whenever a message is received.

  1. To publish a message to a topic, you can use the following code:
1
client.Publish("mytopic", 0, false, "Hello, World!")

This code publishes the message “Hello, World!” to the “mytopic” topic.

That’s it! You should now have a basic MQTT publish/subscribe client that can send and receive messages. You can use this as a starting point for more complex MQTT applications.

Share on

Santosh Kumar
WRITTEN BY
Santosh Kumar
Santosh is a Software Developer currently working with NuNet as a Full Stack Developer.