Scala-IOT : What is Mqtt ? How it is lightweight ?

Table of contents
Reading Time: 4 minutes

We have already stepped in the world of IOT ! And if you do find it fascinating then stay tuned to this series , or you can signup for our newsletter so you can directly get a message in your inbox whenever it is published !

So we established our basic understanding in our last blog post of what is IOT and how it is effecting our world and making it a better place to live 😉 (I guess I love to mock Hooli 😀 )

If you just want a quick recap here is the blog: IOT: Introduction to IOT

So in this blog we will talk about the MQTT  and its headers , what is this protocol ? (where we left in the last blog Remember ??  😉 )

So once again lets start with first things first :

What is MQTT ?

So MQTT official website says :

MQTT is a Client Server publish/subscribe messaging transport protocol.
It is light weight, open, simple, and designed so as to be easy to implement.
These characteristics make it ideal for use in many situations, 
including constrained environments such as for communication 
in Machine to Machine (M2M) and Internet of Things (IoT) contexts
where a small code footprint is required and/or network bandwidth
is at a premium.

MQTT  was invented because Andy and Arren wanted to create a protocol for minimal battery loss and minimal bandwidth connecting oil pipelines over satellite connection.

Comparison between MQTT and HTTP ?  How it consumes less Power ?

HTTP(S) doesn’t keep a connection open – so to get the “effect” of a continuous connection, the HTTPS client has to keep making poll requests, each of which involves creating the TCP connection and negotiating SSL/TLS encryption etc. each time a poll is needed.

By contrast, MQTT makes a connection at the start, so the TCP and SSL/TLS overhead cost is only paid once. MQTT has a keep alive message flowing between applications on top of the TCP connection, so that the applications can detect when the connection is broken, this allows the MQTT broker to reliably publish the (optional) client last will/testament when the connection is broken.

Stephen Nicholas did a fascinating apples to apples comparison of MQTT vs HTTPS on Android, 3G and WiFi which you can read here.  The 3G results are quite interesting:

  • 93x faster throughput
  • 11.89x less battery to send
  • 170.9x less battery to receive
  • 1/2 as much power to keep connection open
  • 8x less network overhead

If you want to know more about this you can visit these links :

  1. REST is for sleeping and MQTT is for mobile
  2. Power Profiling: HTTPS Long Polling vs. MQTT with SSL, on Android

MQTT Architecture :

The protocol uses publish/subscribe architecture in contrast to HTTP with its request/response paradigm. Hence its architecture something looks like this:


These are the basic terms that one should know of to understand this architecture:

  1. Broker: The broker is the server that buffers data and pushes them to the subscriber according to the topic to which the subscriber has subscribed.
  2. Subscriber: Subscriber consumes the message , it selects the topic of which it wants to consume the message and registers it to the broker and hence broker pushes the message accordingly.
  3. Publisher: Publisher is the one that generates the message , it generates the message and assign it to the topic and then sends it to the broker.
  4. Message: Message is an entity that gets exchanged between the Publisher-Broker-Subscriber.
  5. Topic: Topic is like a type in scala. It defines the type of message it is for example temperature of RaspberryPi is being sent to broker and its type would be : Temperature.

There are many implementations of broker like : IBM-Bluemix has Organization , Mosquitto by Eclipse etc.

I am myself working on a Scala, AKKA based Mqtt broker just like Akka-http server but this will implement the Mqtt protocol, that would be reactive and one can prioritize the message as well , I hope it may be completed in sometime in near future.

So the whole working application would have three parts and looks something like this:



So now you know what the architecture looks like and how the protocol is being implemented,

Next are some advanced stuff ! Where you will be seeing the MQTT packets and the Mqtt protocol. We will only be having an overview of it so if you have bear so far you may wanna walk two to three steps more .

MQTT Header:

So here is what MQTT header looks like :


There are many control packets and you can see the list of them down below:


If you want to know more about the headers and the algorithm visit this link , it has all one will ever need !

I hope now you have basic understanding of what MQTT is ? and why it is better?

I hope you liked the blog ! For more info stay tuned ! If you want any more info ping me here or on twitter : @shiv4nsh

Our next blog will be discussing about : Getting started with RaspberryPi , Headless: Without Screen !

Till then Stay tuned !

happy hAKKAing !


  1. HiveMQ’s  blog .
  2. Mqtt’s official website.
  3. REST is for sleeping and MQTT is for mobile
  4. Power Profiling: HTTPS Long Polling vs. MQTT with SSL, on Android