CodeBlog.xyz

MQTT Key Concepts

September 1, 2023 | by Meir Achildiev

MQTT Key Concepts

I’ll deep-dive into MQTT for you, focusing on 20 key concepts, examples, pros, and cons of each, when to use them.

1. Publish-Subscribe Pattern

Example: IoT devices publish sensor data to specific topics; a cloud-based dashboard subscribes to those topics to display real-time data.
Pros: Efficient real-time data transmission, less bandwidth usage.
Cons: Complexity increases with topic management.
When to Use: Real-time data transfer, IoT devices.

2. Broker

Example: Mosquitto, a popular MQTT broker.
Pros: Centralizes message routing, simplifying client-side logic.
Cons: Single point of failure.
When to Use: When you require a message-orchestrating layer.

3. Topic

Example: /home/livingroom/temperature.
Pros: Granular message filtering.
Cons: Too many topics can become hard to manage.
When to Use: When you need to categorize messages.

4. QoS (Quality of Service)

Example: QoS 0 (At most once), QoS 1 (At least once), QoS 2 (Exactly once).
Pros: Allows control over message delivery.
Cons: Higher QoS levels are more resource-intensive.
When to Use: Depends on message importance.

5. Last Will and Testament (LWT)

Example: A device sets an LWT message to signal its unexpected disconnection.
Pros: Ensures proper failure notifications.
Cons: Might not cover all failure cases.
When to Use: Critical monitoring systems.

6. Retained Messages

Example: The last state of a smart light bulb is retained.
Pros: Simplifies state tracking.
Cons: Potentially outdated or irrelevant data.
When to Use: To retain the last-known state of a system.

7. Clean Session

Example: A device sets a clean session when it doesn’t want message persistence.
Pros: Reduces storage overhead.
Cons: Loses undelivered messages.
When to Use: Temporary, non-critical connections.

8. Keep Alive Interval

Example: Set to 60 seconds to ping the broker for keeping the connection.
Pros: Detects lost connections efficiently.
Cons: Adds minor overhead.
When to Use: Long-lived connections.

9. Payload

Example: Sending JSON/XML/Plain text as the payload.
Pros: Flexibility in data format.
Cons: Limited by MQTT packet size (256 MB max).
When to Use: Depending on the data requirement.

10. PINGREQ and PINGRESP

Example: Heartbeat messages between client and broker.
Pros: Verifies the health of the connection.
Cons: Extra network packets.
When to Use: In unreliable network conditions.

11. SUBSCRIBE and UNSUBSCRIBE

Example: Subscribing to /weather/+ to get updates for all subtopics under weather.
Pros: Dynamic message filtering.
Cons: Managing subscriptions can become complex.
When to Use: When you need to dynamically filter incoming messages.

12. Wildcards

Example: Using # and + for topic filtering.
Pros: Simplifies topic subscription.
Cons: Could receive unwanted messages.
When to Use: To reduce the number of SUBSCRIBE calls.

13. Persistent Session

Example: A device subscribes with a persistent session to ensure message retention during disconnections.
Pros: Guarantees message delivery after reconnection.
Cons: Storage overhead.
When to Use: In critical applications where messages can’t be lost.

14. Birth and Death Certificates

Example: Device sends a ‘birth’ message when it connects and a ‘death’ message when it disconnects cleanly.
Pros: Makes the system aware of device status.
Cons: Extra messages to handle.
When to Use: For lifecycle monitoring.

15. Username/Password

Example: Sending credentials for MQTT over TLS.
Pros: Enhances security.
Cons: Risk of credential exposure.
When to Use: In secured environments.

16. SSL/TLS

Example: Encrypting messages with SSL/TLS.
Pros: Secured communication.
Cons: Increased CPU load.
When to Use: When high security is required.

17. Shared Subscription

Example: Multiple subscribers share a subscription to balance the load.
Pros: Load balancing.
Cons: Complexity in message ordering.
When to Use: In high-traffic scenarios for scaling.

18. Packet Identifier

Example: Each packet can have an optional identifier.
Pros: Helps in tracking and acknowledgment.
Cons: Extra bytes in the packet.
When to Use: When QoS > 0.

19. Throttling

Example: Limiting the rate at which messages are published to avoid broker overload.
Pros: Protects broker.
Cons: Delays in message delivery.
When to Use: High-traffic, resource-constrained systems.

20. MQTT-SN (MQTT for Sensor Networks)

Example: A lighter version for sensor networks.
Pros: Designed for low-bandwidth, high-latency networks.
Cons: Limited features.
When to Use: For extremely constrained environments.

RELATED POSTS

View all

view all