Beginners Guide to MQTT

mqtt-iconWhat is MQTT?

MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments.

It was designed by Andy Stanford-Clark (IBM) and Arlen Nipper in 1999 for connecting Oil Pipeline telemetry systems over satellite.

Although it started live as a proprietary protocol it was released Royalty free in 2010 and became an OASIS standard in 2014.

MQTT  is fast becoming one of the main protocols for IOT (internet of things) deployments.

MQTT Versions

There are two versions of MQTT.

The original  MQTT which was designed in 1999 and has been in use for many years and designed for TCP/IP networks and the newer MQTT-SN which was specified in around 2013, and designed to work over UDP,ZigBee and other transports. See MQTT-SN working Notes.

MQTT-SN doesn’t currently appear to be very popular. and the specification has changed for several years but I expect that to change as IOT deployments start.

MQTT Basics

Here is the actual Specification MQTT V3.1 and here is a more detailed overview of the MQTT packet structure,

MQTT uses a publish /subscribe model which requires the use of a central Broker.

The publisher and subscriber never connect directly to each other, and are unaware of each other.

It is like a radio or TV broadcast. The broadcast goes out even if there are no listeners.

MQTT- Publish-Subscribe-Model

How it Works

An MQTT client e..g. a sensor publishes a message to the broker.

The Broker sends the message to any MQTT clients that have subscribed to receive the data.

An MQTT client can function as a publisher,subscriber or both.

Client Software

There is client software available in almost all programming languages and for the main operating systems Linux, Windows, Mac from the Eclipse Paho project.

On this site I will be using the Python client.

Topics – MQTT Addressing

MQTT clients don’t have addresses like email addresses, phone numbers etc.

This means that you don’t need to assign addresses to clients like you do with most messaging systems.

Instead MQTT uses Topics.

Topics are created by the subscriber and publisher and are not pre-assigned by the broker.

As long as a topic name follows the naming standards it is accepted by the Broker.

A Topic name is a UTF-8 string, and must comprise of at least one character.

Topics can be created in a hierarchical manner (levels)  using a forward slash as a delimiter.

Simple topic examples are:

topic = bulb1,  topic = bulb2,  topic = bulb3 or

topic =bulbs/ bulb1,  topic =bulbs/ bulb2,  topic =bulbs/ bulb3

See understanding Topic Naming and Design Notes

MQTT Brokers or Servers

Note: The original term was broker but it has now been standardized as Server. You will see Both terms used.

There are many MQTT brokers available that you can use for testing and for real applications.

There are free self hosted brokers , the most popular being Mosquitto and commercial ones like HiveMQ.

Mosquitto  is a free open source MQTT broker  that runs on Windows and Linux.

If you don’t want to install and manage your own broker you can use a cloud based broker from Cloud service providers like IBM, Microsoft (Azure) etc

Eclipse has a free public MQTT broker and COAP server that you can also use for testing. The address is and the port is 1883 or 8883(SSL).

This article has a list of free cloud based brokers that can be used for testing.

This article has a list of self hosted brokers.

MQTT Security

MQTT supports various authentications and data security mechanisms.

It is important to note that these security mechanisms are configured on the MQTT broker, and it is up to the client to comply with the mechanisms in place.

See An Introduction to MQTT security mechanisms

Client to Broker Communication

MQTT uses a command and command acknowledgement format.

That means each command has an associated acknowledgment.


You can always tell if a command has been successful by examining the acknowledgement message.

Overview Video

This video gives a useful introduction to MQTT

Common Questions

If you are familiar with the web and email then you will probably find, as I did, that MQTT is very different. These are some of the questions I had, and saw on other sites and forums that may clear things up a little.

Q- What happens to messages that get published to topics that no one subscribes to?

A- They are discarded by the broker.

Q-How can I find out what topics have been published?

A- You can’t do this easily as the broker doesn’t seem to keep a list of published topics as they aren’t permanent.

Q- Can I subscribe to a topic that no one is publishing to?

A- Yes

Q- Are messages stored on the broker?

A- Yes but only temporarily. Once they have been sent to all subscribers they are then discarded. But see next question.

Q- What are retained messages?

A- When you publish a message you can have the broker store the last  published message. This message will be the first message that new subscribers see when they subscribe to that topic.  MQTT only retains 1 message.

Real World MQTT Example Deployments

It’s often useful and interesting to see how a particular technology is actually being used. Here are some examples I’ve come across:

MQTT Version 5

The next version of MQTT (autumn 2017) will be version 5. If you are wondering what happened to 4 then see here.

You can find a working draft here and the key ideas in version 5 here.

MQTT By Example

Fortunately when I started to learn about MQTT I came across the very good MQTT essentials series that explained the main features of MQTT.

However there is one thing reading about something and another seeing it really work so I started using Python to create simple scripts to demonstrate some of the aspects of MQTT that I found confusing.

Before you start I would recommend that you read through the MQTT essentials articles as they cover the theory in more detail.

To create these example I used my own Mosquitto broker installed on my local network. However most of the examples would work using the public Mosquitto broker at

I use the Paho Python MQTT client for the client scripts.

However A knowledge of python isn’t really required to understand the examples.

MQTT by Example:

Using the Python MQTT client


Working Notes

MQTT and Mosquitto WebSockets Working Notes

Was This Article Helpful? Please Rate..
[Total: 17    Average: 4.2/5]


  1. Dear writer,
    I am trying to implement MQTT on .NETCore for which there are no library or nuget packages . In my case I want to implement some very basic functionalities of MQTT – like CONNECT, PUBLISH, SUBSCRIBE and DISCONNECT. Is it possible to write all the class libraries by myself?? oI am not so good programmer. 🙂 Thank you ..

  2. hi!
    first of all, thank you! it was very helpful 🙂
    but i don’t understand one thing; is there any standard topic for mqtt? i mean, is it necessery to describe the topic names on the client? because there might be really huge number of topics for a big project. then it would be hard to define all of them.
    thank you 🙂

    1. Hi
      There is no standard like DNS. The only reserved name is $SYS.My personal view is that embedding a topic hierarchy into a sensor/client would not be a good idea. Here are some working notes I’ve put together that you may find useful.
      I think it is still early days yet and various best practices will emerge.

  3. I’m using Paho client and Mosquitto, all on Raspberry Pi and Python.
    I’m seeking better information on building not just working applications, but robust applications.

    There are issue such as “Don’t disconnect until all messages have published”, “Wait for Connect”, “Publish on Paho Python does not block”, among others, that need to be addressed. Every example I’ve seen omits these discussions.

    I’d love to hear from anyone who is working on creating more robust MQTT apps beyond just “Hello World”

    1. jeffrey
      You might want to take a look at client connections .
      I use a wait loop after a connection request and a subscribe and don’t proceed until I get a CONNACK or SUBACK. I have a general function to do this.
      def wait_for(client,msgType,period=0.25):
      if msgType==”SUBACK”:
      if client.on_subscribe:
      while not client.suback_flag:“waiting suback”)
      client.loop() #check for messages

      In the program I just call it wait_for(client,”CONNACK”). The script stops until I get the connack. I’ve also started to do a timeout in the wait loop.
      The flags I use I set in the client class at the beginning of the script.So my import is this
      import paho.mqtt.client as mqtt
      and then I set the flag like this
      mqtt.Client.suback_flag= False
      So I can create new clients and they will all have the flag available e.g.
      client= mqtt.Client(cname)
      Because I use several flags I use a function to set them all. It looks like this:
      def Initialise_client_object():
      mqtt.Client.suback_flag= False

      I hope that helps.
      Rgds Steve

  4. How the broker registers a topic? does the broker perform that at the subscribing, publishing or both phases? Does the broker store them in one list? if so, where is that or what is the name of the list?

    1. Sami
      You would need to dig into the source code to find that out. I assume,like you, that stores them in a list in memory.
      The broker registers a topic as a result of a client subscribe.It must also regsiter a topic if you Publish to a topic with the retain flag set to true.
      I would also expect it to register a topic if you set the last will message.

  5. Hi Steve,
    Thank for the great tutorial. I have a question regarding the MQTT setup for a local experiment. I’m trying to do a research experiment that simulates several MQTT that connected together. Can I use a single computer to run several MQTT in one machine? if not what do you suggest and what is the initial direction that I should take?

    Thank again.

    1. Yes you can run lots of brokers on the same computer by using different ports. This is the way online brokers work.To do that you need to add a new listener in mosquitto.conf
      listener port 1884
      listener port 1885


      You could also use the free online brokers as extra brokers.

      “”, “”,””

      1. Great!
        I noticed in your tutorials that you are using your own IP address for the hostname. Do you have a tutorial on that or a specific way of doing it?

Leave a Reply

Your email address will not be published. Required fields are marked *