Home » guide » Basic MQTT protocol walk-through

Basic MQTT protocol walk-through

MQTT is among the popular connectivity protocol for Internet of Things application, considering the fact that it’s supported by mainstream cloud platform like AWS, Azure, IBM and ThingWorx.

MQTT has been gaining popularity for IoT application due to its small overhead comparing to HTTP, which makes it easy to implement on constrained device (e.g. limited power and processing). Most prototyping platform like the Arduino, Intel Edison and TI CC3x00 supports MQTT out-of-the-box.

What is MQTT?

MQTT standard is governed by OASIS. It adopts the publish-subscribe architecture, where all communication has to be facilitated by a message broker. Typically, the producer (e.g. sensor) publishes while consumer subscribes to desired topic/channel. When message is published to a topic, all clients (i.e. connected device) subscribing to the particular topic will receive the message in near real-time.

Unlike enterprise service bus, the topic do not need to be initialized. No “queue” is maintained, by design it’s not possible to store messages in the broker for consumption later.

There are several basic actions that can be performed by the client:

  • Connect – connect to the message broker
  • Publish – publish message to a specific topic
  • Subscribe – indicate its interested to receive messages from topic(s)
  • Receive – receive incoming message (if done subscribing)

MQTT by Example

Let’s have a look at a quick example. Fortunately, there are free MQTT broker publicly available for testing – it is not advisable for production usage.

  1. Install MQTT client on Windows/Mac. I recommend MQTT.fx.
  2. Configure the MQTT client with broker address, port and generate random client ID. Leave the rest as default. For this testing we’ll be doing it without authentication.
  3. Connect to the broker. You should see successful message. In the case of MQTT.fx it’s the green indicator.
  4. On the MQTT client let’s subscribe to topic “kitchen/uid1/temperature”. 
    Note: This example is assuming that our topic design is based on <logical-location>/<dev-id>/<type>. The forward slash denotes the next hierarchy/layer. Example, temperature sensor on device 1 located in the kitchen will publishing to the topic “kitchen/uid1/temperature”.
  5. Test publishing to the same topic
  6. On the subscribe tab, we are able to receive incoming message that is recently published in near real-time.
  7. You may also try installing MQTT client on Android/iOS or any supported device. Example, the MQTT Dashboard App I installed on my Android is able to receive the same message.

Subscribing with wildcard

In practice, it is not practical to subscribe to every topic individually. It is possible to subscribe using wildcard. We either use “+” to substitute single layer or “#” at the end for multi-layer. Example subscription:

  • All temperature data from kitchen regardless of device “kitchen/+/temperature”
  • All data from device uid1 in kitchen “kitchen/uid1/#”
  • All data from kitchen “kitchen/#”

More about MQTT

That’s all for getting started with MQTT. In designing your application, there are many other features that have to be considered such as quality of service, message retention, last will and testament, etc.

If you like to learn more about MQTT, refer to the guide at HiveMQ.

MQTT Essentials



3 Responsesso far.

  1. Sindi Oktaviani says:

    can you show me how to assign the subscribed data into chart? i know MQTT Dashboard has feature for showing realtime chart, but i couldn’t figure it how. please help. thanks

    • calvin says:

      Hello Sindi,

      Realistically speaking, for graph, you’re generally need a database (i.e. so that the visualiser can get the data from a database and plot it on a dashboard). However, MQTT is just a message broker, it doesn’t have the capability to store data. For this I’d recommend writing a simple script to forward all messages to an InfluxDB/ElasticSearch and then using Kibana/Grafana to visualise. You can see an example using AWS services https://techblog.calvinboey.com/raspberrypi-aws-iot-python/

      Back to your question, if you want to see real-time data as it comes into the MQTT broker, fortunately there are client-side web-based solution for this. Checkout this project https://github.com/matbor/mqtt2highcharts

      Regards, Calvin

    • calvin says:

      Hi Sindi, reading your comment again, I misunderstood your question earlier. To see the graph in MQTT dashboard, you’re have to select the option “Is Numeric” at the same page you defined the topic. Once this is done, clicking on the panel will show the real-time graph. Let me know if you still have trouble finding it.

      Regards, Calvin

Leave a Reply

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