MQTT: Emergence of pub/sub model

Nitee Shah
7 min readAug 17, 2020

--

How publish/subscribe model became an instant favourite in the IoT World.

Photo by Road Trip with Raj on Unsplash

Storytelling has been an integral part of Human Culture. It is believed to be an engaging way of communication that enchants the child within us. Since we are exposed to a million different concepts, it becomes strenuous to remember it all.

This blog intends to depict the Technical Knowledge in a story form so that it sketches a lasting impression on our minds.

For clarity and simplicity, this blog is divided into 3 parts.

The First part is the story about a guy named Mathew Queen, Mr. MQ(fictional character).

The second part is getting familiar with the fundamental concept of MQTT.

The Third part is a little Bonus, a mini practical set up that you can try at home, without any special edge devices.

So, let’s get started …

Once upon a time…

It was a beautiful summer evening in the city of Paris, a huge crowd had gathered near the river Seine to enjoy the sunset and the view of the Eiffel Tower. As the dusk was falling rapidly, Amelia eyed Clack warily, as he looked very suspicious to her. Little did she know Clark would steal her heart that night and win her forever. Amelia and Clark fell in love with each other. Happily married, now they were looking to buy their Dream House.

Like any great love story, they also had to face some challenges and get a victory over them. Buying a new house became a struggle for Amelia and Clark.

The process became detailed and time consuming for them (High Bandwidth)

  1. Amelia (Client) would call the seller of the house (Server) and explain their complete requirement every time (High Overhead)
  2. Amelia (Client) and House Seller (Server) had to decide the common time of availability to talk with each other (Synchronous)
  3. Amelia (Client) could only talk to House Seller (Server). (Coupled)
  4. Amelia (Client) faced a limitation concerning the number of houses she could search in a week. (Low Scalability)

Amelia started becoming worried and was mostly involved in looking for their dream house. Clark saw this and was upset that both of them could not spend some memorable time with each other. That is when, Mathew Queen, famously called as Mr. MQ entered their lives.

Mr. MQ was so charming and delightful. Looking at him, Amelia and Clark instantly knew, he was here to solve all their problems!

Mathew Queen, Mr. MQ introduced Amelia and Clark to New, quick and independent process to find their Dream House. (Low Bandwidth)

Now,

  1. Amelia (Client) would no longer have to call all the Sellers (Server) personally and explain exhaustive details. She only had to give all her requirements (Topic) to Mr. MQ (Broker) only once. (Low Overhead)
  2. Mr. MQ (Broker) registered all the requirements given by Amelia (Client) and contacted all the relevant Seller (Server). Thus, eliminating the time commitment headache for Amelia. (Asynchronous)
  3. Amelia (Client) didn’t have to know the sellers personally. Mr. MQ (Broker) would take care of everything for her. (Decoupled)
  4. Now Amelia was able to view multiple houses to choose her Dream House in one glance. So, were many other couples, all because of Mr. MQ (High Scalability)

Amelia and Clark were so Thrilled to find their Dream House, hassle-free.

And they lived happily ever after ….

Second Part: Technical

Taking the learnings from the above story, let us dive deep into understanding the Technology behind the MQTT protocol.

Andy Stanford-Clark (IBM) and Arlen Nipper (Arcom, now Cirrus Link) invented the MQTT protocol in the year 1999, to connect with oil pipelines via satellite.

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.“Citation from the official MQTT 3.1.1 specificatio
Image by https://www.opc-router.com/what-is-mqtt/

The Major Components of MQTT Protocol are :

  1. MQTT Broker
  2. Publish/Subscribe
  3. Topic

MQTT Broker

  • Broker is the heart and soul of this communication protocol. MQTT broker is responsible to store all the information sent by clients in the form of Topic and Payloads.
  • The important thing to remember is the Topic Value should be the same and agreed upon between the Publisher and Subscriber.
  • MQTT Broker can simultaneously connect to thousands of clients (Publisher/Subscriber)
  • Broker provides the quality of message delivery option, it is called QoS (Quality of Service).
  • Broker also stores the information in case of loss of network between client and broker.
  • It is the Broker’s responsibility to provide all the relevant information to the correct subscribers.
  • There is a lot of processing going on at the Brokers end, hence drastically reducing the responsibility with Publisher/Subscriber.

Publish/Subscribe

  • Every Client can be a publisher or a subscriber or both.

Low Bandwidth:

  • Client Connect with the Broker only once, giving all the configuration details. Later, only Topic related information is sent.

Asynchronous:

  • The Publisher and Subscriber need not be alive at the same time. Once the information is pushed to the Broker with a relevant topic, the publisher’s responsibility is finished.

Decoupled:

  • The Publisher need not know any information about the subscriber.
  • Publisher does not even know how many subscribers are accessing the payload at the same time.

Scalability:

  • Any number of publishers and subscriber can be connected, depending on the capacity of the broker. The only condition is the Topic name should be agreed upon by publisher and subscriber.

Topic

  • Topic is the command mode of understanding between Publisher and Subscriber.
  • For example, Publisher1 is sending data on the Topic called “Dream House”. Subscriber1 to SubcriberN can receive the same information published, if and only if the subscribe to topic “Dream House”.
  • Topic also provides different levels separated by a forward slash. You can give as detailed information as you like. It will only search for a Dream House which is River Facing in Paris.
DreamHouse/France/Paris/RiverFacing
  • Topic also has a concept of Wildcard. The single-level wildcard is represented by Plus symbol. This means that it will search for the Dream Houses in all the Cities of France which are River Facing.
DreamHouse/France/+/RiverFacing
  • There is also a multi-level wild card, represented by the # symbol. This wild card is placed at the end of the topic and is generally used to get all the information under one subject. This topic will search for all Dream Houses in the city of Paris, including RiverFacing, City Centre, in the Outskirts etc. All the information from multiple sources will be retrieved. Be careful with the use of # wildcard, as you don't want to overload your computing system.
DreamHouse/France/Paris/#
  • The Topic constitutes of payload and Quality of Service. (It is a whole discussion by itself, so not covered here)

Hope, this brings us deeper in understanding concerning where we had started.

Third Part: Practical

Its time for the promised Bonus.

No theoretical knowledge is complete until we have little fun with it in our practical way.

HiveMQ has provided with a Free Public Broker, which anybody can experiment with.

Requirements:

  • Laptop with an internet connection
  • Mobile (Edge Device) with an internet connection
  • Enthusiasm

Let’s get started….

  1. Go to http://www.mqtt-dashboard.com/, just to check out the free public MQTT broker. It also gives statistics on how many clients are communicating simultaneously.
  2. Go to http://www.hivemq.com/demos/websocket-client/.
  • A client ID is generated by default. Give any user name and hit Connect.
Experimenting with http://www.hivemq.com/demos/websocket-client/

3. Now the connection is indicated in green. Go to publish tab and give your custom Topic name.

Experimenting with http://www.hivemq.com/demos/websocket-client/

4. Go to ‘Add New Topic Subscription’ and enter the same topic given to the publisher.

Experimenting with http://www.hivemq.com/demos/websocket-client/

5. Write whatever message you wish to publish and you will see it reflecting in the message box.

Experimenting with http://www.hivemq.com/demos/websocket-client/

Yay! That’s it. You have now officially communicated using the MQTT protocol. You can play around with opening the publisher in your mobile phone and seeing the message getting reflected in the laptop.

Keep Playing!

--

--

Responses (3)