Getting Started with MQTT — Part 1

This tutorial is first part of three-part tutorial series on “Getting Started with Mqtt” and will cover basic concepts of Mqtt

MQTT — Introduction

MQTT is a publish-subscribe based messaging protocol that works on top of TCP/IP protocol. It is designed for connections with remote locations (eg:IOT devices) where our network bandwidth is limited and we require quick delivery.

Aspects of MQTT protocol

  • To communicate with each other, publisher and subscriber only needs to know IP and port of the broker and a common Topic. This removes dependency of each client knowing address of other client.
  • Even though most messages are delivered on the go, MQTT also supports storing of messages. If desired, broker can store these messages when a client is not online. For this client needs to connect using a persistent session with QOS greater than zero.
  • MQTT mostly works in synchronous manner which results in no lagging in delivery.
  • MQTT uses subject based filtering and broker uses Topics to decide on which subscriber is to receive which message.
Figure : MQTT in temperature sensing.

MQTT - Client

  • In MQTT, both publishers and subscribers are clients.
  • A client can act both as a publisher or subscriber and name only signifies sending or receiving party.
  • Ease of implementation and MQTT-client’s ability to work on low bandwidth network is what makes it perfect to use in IOT based devices.
  • Micro-controllers, pi, AWS servers running on MQTT can act as clients.
  • Different languages based client libraries are available here on wiki.

MQTT - Broker

  • Broker acts as a mediator between publisher and subscriber.
  • MQTT brokers can handle thousands of messages at a time due to its simplified architecture. Here is a comparison between MQTT and HTTP protocols.
  • Work done by broker : Receive all messages, filter these messages, find out subscribers to a particular topic, Send these message to those subscribed clients.
  • Broker can also hold missed messages of persisted clients which can be sent to it once he comes online again. This is important for a case when some client gets disconnected unwillingly.
  • Broker is also responsible for handling authentication and authorization.

MQTT - Connection

  • MQTT broker and clients both are TCP/IP based.
  • Connection is always between one broker and one client.
  • Some important parameters needed for the connection :

Publisher/Subscriber in MQTT

  • A MQTT Publisher client can start publishing messages as soon as it connects to the broker. Each message will contain a topic and payload to be sent.
  • A MQTT Subscriber are the ones that receives messages that are published to a particular topic.

MQTT - Topics

  • MQTT-Topic is a string that broker uses to filter messages. Only subscribers of a particular topic receives messages published to that topic.
  • Topics consist of topic levels separated by a “/”. Example : “home/ground/room/temp”
  • Properties :
    1. Must contain atleast 1 character
    2. Can contain empty spaces
    3. “/” is a valid topic name

Relevant Reads

Apart from above mentioned details, there are few more topics that needs your attention :

  • Persistent Session in MQTT : In MQTT a client can request a persistent session when it connects to the broker. Persistent sessions save all information that is relevant for the client on the broker. Read : AWS Doc, IBM Doc.
  • Last Will : This basically is a message that will notify other clients subscribed to a particular topic that this client has lost connection. HiveMQ Read explains it in details.

Masters, NIT Surathka l Java Developer | Bachelors, IIIT Jabalpur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store