Caio Landau Caio Landau
May 2, 2020 • 5 min read

Understanding MQTT - The Basics

What is MQTT, how does it work and why does it matter? In this post, we’ll go over this messaging protocol’s basics and some use cases for mobile apps.

What is MQTT?

In short, MQTT is a very lightweight publish/subscribe messaging protocol. What does this mean? It’s a protocol that enables the exchanging of messages between two or more parties in a network (i.e. the internet!). Messages can be anything - from binary data to simple text and data formats like JSON or Protocol Buffers.

How does MQTT work?

At a high level, MQTT is actually pretty simple. There are three main actors in play:

The broker is crucial: publishers and subscribers do not know each other. Instead, the broker coordinates receiving and delivering messages according to the topic and QoS (more on that later).

If you’re a mobile or front-end developer, chances are you’ve worked with REST APIs in the past. You will be used to a specific communication flow: the client makes a request, the server processes it and answers with a response. MQTT is different. With MQTT there is no concept of client and server. Instead, there are the three actors above, and any device can play publisher or subscriber (or both!).

The flow is as simple as:

  1. Subscriber connects to the broker (and subscribes to a topic)
  2. Publisher sends a message
  3. Subscriber receives the message

Two concepts are important to mention if you are just learning about MQTT now:

Topics

A somewhat lousy analogy would be to compare topics to URLs. A topic is a string value used to filter messages in the broker. Publishers send each message to a specific topic, and subscribers listen to the topics that are useful for them. An example topic to track a device’s GPS coordinates could be: device-1a2b3c4d5e6f/coordinates

QoS

Let’s not go too deep into this one for now. The important part to keep in mind: some messages are more important than others. For our GPS topic above, it may not be crucial for your application to guarantee that every single coordinate is sent and received. This is why QoS exists.

You have to define a QoS both when sending a message (i.e. how important it is that this message is send successfully) and when subscribing (i.e. how important it is that this message is received successfully). QoS is a number: 0, 1 or 2. The higher the number, the higher the guarantee, but the higher the overhead. Choose wisely.

Why does MQTT matter? What are some use cases?

Why not just use HTTP for everything, right? Have you ever had to poll a REST API endpoint constantly to keep asking for updates about something? Or push data repeatedly? HTTP has significant overhead, and was not designed for this type of interaction. You may have seen your mobile app draining a lot of battery due to too many HTTP calls, for example.

MQTT to the rescue! It has significantly less overhead when connecting, even over TLS (encrypted connection). Plus the data package is much smaller - meaning less bandwidth is used and transmission is faster overall.

In addition, MQTT is heavily optimized towards slow/unreliable connections. Quoting MQTT.org:

[MQTT is] designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery

Example MQTT use cases (in the mobile development world)

MQTT can be very useful in many cases. The options are limitless, but some examples are:

So is MQTT the holy grail?

Great, let’s ditch HTTP and use MQTT for everything! Everyone wants fast connections and low overhead, right? Not so fast. There are some important differences - HTTP and MQTT can live in peace together:

That all sounds great! Where do I learn more about MQTT?

Now that you know how MQTT works at a high level, there are many great resources where you can learn more in-depth, including some examples. I recommend the following two series of articles:

Post by: Caio Landau