DEV Community

Cover image for Here's everything you need to know about MQTT Broker (simple explanation)
Behnam Nasehi
Behnam Nasehi

Posted on

Here's everything you need to know about MQTT Broker (simple explanation)

The MQTT broker helps devices communicate with each other like a messenger. It's like a digital postman, except it goes "beep" instead of "ding dong"! Imagine that you have a group of friends who want to share messages with each other. They can't just shout at each other because they might be far away or not in the same room. So, they need a messenger to deliver their messages.

The MQTT broker is like that messenger. It helps different devices like smartphones, sensors, and other machines talk to each other over the internet. Each device sends a message to the broker, and the broker delivers that message to the device or devices that need to receive it.

So, just like a messenger helps friends communicate with each other, the MQTT broker helps devices communicate with each other over the internet.


Websocket vs MQTT: what's the difference

Websockets and MQTT are both protocols used for communication over the internet, but they have some differences in how they work and what they are best suited for.

Web sockets are designed for real-time bidirectional communication between a client and a server. They establish a long-term connection between the two, allowing for low-latency communication and efficient data transfer. Websockets are often used in web applications for tasks like live chat or real-time data visualization.

MQTT, on the other hand, is a lightweight publish-subscribe messaging protocol that is optimized for machine-to-machine communication in IoT (Internet of Things) applications. It is designed to be efficient and reliable, even in low-bandwidth or unreliable network conditions. MQTT uses a broker to mediate communication between multiple clients, making it well-suited for applications where many devices need to send and receive data with low overhead and minimal latency.

In summary, while both Websockets and MQTT can be used for real-time communication, Websockets are typically used for client-server communication in web applications, while MQTT is used for machine-to-machine communication in IoT applications.


Here's how MQTT brokers work

MQTT broker is a central communication hub that facilitates communication between devices using the MQTT protocol. It receives messages published by devices and delivers them to devices that have subscribed to receive them. The MQTT broker works based on the publish-subscribe pattern, which means that devices publish messages to a specific topic, and other devices subscribe to that topic to receive those messages.

Here is how the MQTT broker works in more detail:

  1. Devices publish messages to the MQTT broker by connecting to it and specifying a topic to publish to. Each message has a payload (the data being sent) and a topic (a string that identifies the message).

  2. The MQTT broker receives the message and stores it in a buffer until it can be delivered to subscribers. The broker checks the topic of the message and compares it to the list of subscribed topics.

  3. Subscribers can connect to the MQTT broker and subscribe to specific topics by specifying the topic they are interested in. Once subscribed, the MQTT broker delivers any messages published on that topic to the subscriber.

  4. When the MQTT broker receives a message that matches a subscribed topic, it sends the message to all subscribers that have subscribed to that topic.


The Publish Method

Imagine you have a magic toy telephone that can talk to your friend's toy telephone. When you want to tell your friend something, you pick up your magic phone and say your message to it. Then you press a button to send the message to your friend's magic phone.

In MQTT, the publish method works the same way as your magic toy telephone. When you want to send a message to someone else's device (like a smartwatch or a thermostat), you use the publish method. You start by picking a topic for your message, which is like picking a phone number for your friend's toy phone. Then, you enter your message into the publish method and press a button to send it to the other device.

The other device, which is subscribed to the same topic as your message, will receive your message and can take action based on what you said.


Subscribe Method

Imagine you have a special mailbox that only opens when someone sends you a letter with a specific code written on it. When you want to receive messages from someone, you give them the code and they send you a letter with the code on it. Then, when the letter arrives in the mailbox you have set up, it opens and you can read the message inside.

In MQTT, the subscribe method works the same way as your special mailbox. When you want to receive messages from another device (like a sensor or a smart light), you use the subscribe method. You start by choosing a topic that you're interested in, which is like choosing a special code for your mailbox. Then you use the subscribe method to tell the MQTT broker that you want to receive any messages that are published on that topic.

When a message is published on a topic that you subscribe to, the MQTT broker sends it to your device so you can take action based on the message. It's like opening your special mailbox and reading the message that someone sent you!


To illustrate how an MQTT broker can be used in a real-world scenario, here's an example:

Let's say you have a smart home system with multiple sensors and devices, such as a thermostat, a smart lock, and smart lightbulbs. These devices can communicate with each other and with a central hub using the MQTT protocol.

When the temperature in your home drops below a certain threshold, the temperature sensor publishes a message to the MQTT broker with the topic "temperature" and the temperature reading as the payload. The thermostat subscribes to the "temperature" topic and receives the message from the MQTT broker. It then adjusts the heating system to increase the temperature in your home.

If you are away from home and want to let someone in, you can use the smartphone app to send a message to the MQTT broker with the topic "smart-lock" and the command to unlock the door as the payload. The smart lock subscribes to the "smart-lock" topic and receives the message from the MQTT broker. It then unlocks the door, allowing the person to enter.

In the evening, when you want to switch on the lights, you can use the smartphone app to send a message to the MQTT broker with the topic "smart-lights" and the command to turn on the lights as the payload. The smart lightbulbs subscribe to the "smart-lights" topic and receive the message from the MQTT broker. They then turn on, illuminating your home.

In this example, the temperature sensor, smart lock, and smart lightbulbs are all publishers that send messages to the MQTT broker on different topics. The thermostat, smart lock, and smart lightbulbs are subscribers that receive messages from the MQTT broker based on the topics they have subscribed to. The smartphone app acts as a publisher that sends commands to the MQTT broker with different topics, which are then received by the smart lock and smart lightbulbs. The MQTT broker acts as a mediator, delivering messages between the devices and the smartphone app.


What is QoS (Quality of Service) in the MQTT protocol?

QoS (Quality of Service) is like asking your friend to give you something, but with different levels of confirmation.

Level 0 :is like asking your friend to give you something, but not waiting for them to confirm that they heard you or that they will do it. You just assume they will do it and move on.

Level 1 :is like asking your friend to give you something. They confirm that they heard you and will do it, but they might forget or something might happen and they might not actually do it. So you might ask again later to make sure.

Level 2 :is like asking your friend to give you something. They confirm that they heard you and will do it. They keep track of it to make sure it gets done. This way, you can be sure that you will get what you asked for.

Similarly, in MQTT, QoS is used to determine the level of confirmation required for message delivery between devices. A higher QoS level means increased assurance of successful message delivery, but also increased overhead and potential network congestion.


Security of the MQTT protocol

The MQTT protocol has several security mechanisms in place to protect the confidentiality, integrity, and availability of data exchanged between devices. Here are some of the key security features of MQTT:

  1. Encryption: MQTT supports encryption of data using the SSL/TLS protocol. This provides secure communication between devices and the MQTT broker, preventing eavesdropping and unauthorized access to sensitive information.

  2. Authentication: MQTT allows for authentication of devices and users using various mechanisms, including usernames and passwords, X.509 certificates, and JSON Web Tokens (JWTs). This ensures that only authorized devices can publish and subscribe to topics on the MQTT broker.

  3. Access Control: MQTT brokers can implement access control mechanisms to restrict access to topics based on the identity of the device or user, or based on the content of the message being published. This helps prevent unauthorized access and ensures that sensitive data is only visible to authorized parties.

  4. Message Filtering: MQTT brokers can implement message filtering to prevent the delivery of messages to unauthorized subscribers. This helps prevent unauthorized access to sensitive data and reduces network bandwidth usage.

  5. Quality of Service (QoS): MQTT provides different levels of QoS for message delivery, ranging from at-most-once delivery (QoS 0) to at-least-once delivery (QoS 1) to exactly-once delivery (QoS 2). The higher levels of QoS provide increased reliability and consistency in message delivery, ensuring that messages are not lost or duplicated.

Overall, the security features of MQTT make it a suitable protocol for use in IoT applications where security and privacy are critical concerns. However, it is imperative to properly configure and implement security measures to ensure the effectiveness of these features.


What is the best way to build an MQTT broker in Nest.js?

pigeon-mqtt-nest is an NPM package that simplifies the process of integrating an MQTT broker into Nest JS applications.

Using the MQTT.js library, pigeon-mqtt-nest provides a wrapper that enables seamless integration with Nest JS applications. The package provides both MQTT client and server functionality, allowing developers to customize the behavior of the MQTT broker according to their specific requirements.

One of the key benefits of using pigeon-mqtt-nest is that it enables real-time communication between devices and services, which is particularly helpful for IoT applications where data needs to be exchanged in real-time. With pigeon-mqtt-nest, Nest JS developers can easily add MQTT functionality to their applications, without having to worry about the low-level details of MQTT protocol implementation.

Overall, pigeon-mqtt-nest is a valuable tool for Nest JS developers looking to integrate MQTT functionality into their applications. The package is easy to use, customizable, and provides a range of features that make it an excellent choice for building real-time IoT applications.

NPM link : https://www.npmjs.com/package/pigeon-mqtt-nest


Contact Me

Thank you to everyone who took the time to read this article! I hope you find the information useful and informative. The world of IoT and MQTT is constantly evolving, and I am grateful for the opportunity to share my knowledge with you. Now, go ahead and show off your new found knowledge by making your own IoT projects and, who knows, maybe you'll be the one to change the world!

If you have questions or feedback, please feel free to reach out. I would be happy to help in any way I can. Thank you again for your time and attention!

My linkedin : Check out my profile here. And while youโ€™re at it, why not give me a follow and make my day?

Top comments (0)