IntroMQTT Paskauskas 20241022
IntroMQTT Paskauskas 20241022
CONSTRAINTS
2 hours
Introduction to MQTT
What is MQTT?
MQTT is is a lightweight, publish/subscribe messaging protocol designed for
constrained devices and unreliable networks.
Message Flow messages sent to topics, received by client requests data from a server,
subscribers via broker receives a response
HTTP: Best for web applications and REST-based APIs where resource exchange and direct
request-response interactions are required (e.g., file sharing, data retrieval from a server). Inefficient for
IoT scenarios due to its high overhead and statelessness.
CoAP: Designed for constrained IoT devices that need efficient, low-overhead communication. Ideal for
sensor networks, smart energy management, and low-power wireless communication.
AMQP: Preferred in enterprise-level applications where reliable, secure, and transactional messaging is
required (e.g., financial institutions, message queuing systems, distributed systems). Suitable for complex
routing and store-and-forward messaging scenarios where full delivery guarantees and reliability are
crucial.
MQTT in the Modern IoT and Cloud Ecosystem
Use cases: Home automation, industrial IoT, autonomous vehicles, smart cities.
Supported by cloud services: like AWS IoT, Azure IoT Hub, Google Cloud IoT
Core, IBM Watson, Oracle IoT
Subscribe: The action of a client registering to receive messages from a specific topic.
Topic: A string used to organize and categorize messages, acting as a routing key for message distribution.
Broker: Central server that manages communication between clients by routing messages from publishers to subscribers.
Client: Any device or application that connects to the broker to publish or subscribe to topics.
QoS (Quality of Service): Defines the level of guarantee for message delivery (0, 1, or 2).
Retain Flag: Option to store the last message sent to a topic for future subscribers.
Will Message: A message defined by a client that the broker will send if the client unexpectedly disconnects.
Clean Session: An option to specify whether a client’s subscription and message queue should be cleared when it
disconnects.
Session Persistence: The broker can retain client subscriptions and undelivered messages between disconnections.
MQTT Packet Structure
QoS 0 (At most once) Sending a postcard: no guarantee it For non-critical data, such as sensor updates
will arrive, and no follow-up.
QoS 1 (At least once) Sending a registered letter: guaranteed Guarantees message delivery but may result
delivery, but might arrive multiple in duplicates
times.
QoS 2 (Exactly once) Sending a package with a signature: For critical messages, like financial
delivered once, and only once, no transactions, where no loss or duplication is
duplicates. acceptable.
QoS 1 case study
> mosquitto_sub -q 1 -h localhost -p 1883 -t '#'
1729512609: New connection from 127.0.0.1:36906 on port 1883.
1729512609: New client connected from 127.0.0.1:36906 as
auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4 (p2, c1, k60).
1729512609: No will message specified.
1729512609: Sending CONNACK to auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4 (0, 0)
1729512609: Received SUBSCRIBE from auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4
1729512609: # (QoS 1)
1729512609: auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4 1 #
1729512609: Sending SUBACK to auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4
…
> mosquitto_pub -q 1 -h localhost -p 1883 -t 'hello' -m
'world'
1729512717: New connection from 127.0.0.1:46972 on port 1883.
1729512717: New client connected from 127.0.0.1:46972 as
auto-35FE21EB-AE06-DF79-5138-A11159EAEA94 (p2, c1, k60).
1729512717: No will message specified.
1729512717: Sending CONNACK to auto-35FE21EB-AE06-DF79-5138-A11159EAEA94 (0, 0)
1729512717: Received PUBLISH from auto-35FE21EB-AE06-DF79-5138-A11159EAEA94 (d0, q1, r0,
m1, 'hello', ... (5 bytes))
1729512717: Sending PUBLISH to auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4 (d0, q1, r0,
m1, 'hello', ... (5 bytes))
1729512717: Sending PUBACK to auto-35FE21EB-AE06-DF79-5138-A11159EAEA94 (m1, rc0)
1729512717: Received PUBACK from auto-ED3A5FED-675B-366D-EC69-0B6BD83796F4 (Mid: 1,
RC:0)
1729512717: Received DISCONNECT from auto-35FE21EB-AE06-DF79-5138-A11159EAEA94
1729512717: Client auto-35FE21EB-AE06-DF79-5138-A11159EAEA94 disconnected.
● Imagine a smart device that suddenly loses its connection without warning.
The LWT is a special message the device prepares in advance. If the device
disconnects without saying goodbye properly, the MQTT broker automatically
sends this message to let other devices know something went wrong.
● For example, in a smart home, if a sensor stops working, the LWT could notify
the system to alert the user or switch to a backup sensor. It helps keep
everything running smoothly even when a device fails
Retained Messages
Retained Messages ensure that the latest message is always available to new
subscribers, even if they weren’t connected when it was first sent.
It’s like leaving a sticky note with the most recent information for anyone who checks
later.
Example:
Imagine you have a weather station that publishes temperature data.
If a new device (like a phone or dashboard) subscribes to the weather topic,
without retained messages, it would only get new updates and miss the latest
temperature.
With a retained message, the MQTT broker keeps the most recent temperature data and
immediately sends it to any new subscriber.
MQTT Security: protecting data and devices during communication
It means how well a system can handle growth: as more users, devices, or data are added, a scalable
system can continue to work smoothly without slowing down or breaking.
1. Broker Capacity: As more devices connect, the broker needs to handle more messages. You may
need a more powerful broker or multiple brokers working together to keep things running smoothly.
2. Network Traffic: More devices mean more messages flying around. To avoid slowdowns, it’s
important to optimize how often devices send data and keep messages as small as possible.
3. Shared Subscriptions: To balance the load, MQTT can use shared subscriptions where multiple
devices share the responsibility of processing incoming messages.
4. Efficient Use of Resources: As your system grows, it’s important to manage how devices use
resources like bandwidth, power, and processing, so everything runs efficiently even with more
devices connected.
To improve scalability in MQTT
You can focus on several key areas:
1. Increase server capacity: Upgrade or add more powerful servers (brokers)
to handle more traffic.
2. Horizontal scaling: Add more servers (brokers) and use clustering and load
balancing to distribute the load evenly.
3. Optimize network traffic: Reduce message sizes and control how often
devices send data to prevent bottlenecks.
4. Efficient resource management: Ensure that devices and systems use
bandwidth, processing power, and memory efficiently.
5. Multiple brokers for high availability: Use multiple brokers for fault
tolerance, ensuring that if one broker fails, others can take over.
Challenges and Considerations
in Real-World MQTT
Deployments
Scalability and Performance
Handling Unreliable Networks
Security Concerns
Scalability: As the number of connected devices increases,
brokers need to handle higher traffic. This may involve horizontal
scaling, clustering, and load balancing.
Fault Tolerance: Multiple brokers may be used for high
availability, so if one fails, others can take over without downtime.
Security: Brokers must ensure secure communication (encryption,
authentication) to protect data and control who can publish or
subscribe to topics.
Message Management: The broker must efficiently store and
forward messages to ensure they reach their intended devices,
even after temporary disconnects (retained messages, Last Will,
and Testament).
Network:
● Network Reliability: In real-world environments, network
connections may be unreliable. The system must be designed to
handle disconnects and reconnects without losing data.
● Bandwidth Management: With many devices communicating,
it’s important to optimize how much data is sent to avoid
overloading the network.
● Latency: The network should be optimized to reduce delays in
communication, especially for real-time applications.
● Data Traffic: Managing large amounts of data from many
devices requires network capacity planning to ensure smooth
communication and avoid bottlenecks.
Devices:
● Power Consumption: Many IoT devices run on batteries, so
communication must be efficient to conserve power.
● Message Delivery Guarantees: Devices need to use the right QoS
(Quality of Service) to ensure important messages are delivered
reliably.
● Efficient Resource Usage: Devices should use bandwidth, memory,
and processing power efficiently to avoid performance issues.
● Handling Disconnects: Devices must be able to reconnect and
continue communication after losing the network connection without
data loss.
Demonstration
Implementing a Simple MQTT Communication
Setup Environment