Skip to main content

Overview

Protocol: MQTT v5.0 over TLS (MQTTS) Pattern: Publish/Subscribe Endpoint: mqtts://mqtt.petstoreapi.com:8883 When to Use MQTT:
  • ✅ IoT devices and sensors
  • ✅ Low-bandwidth networks
  • ✅ Battery-powered devices
  • ✅ Unreliable network connections
  • ✅ Many devices publishing data
When NOT to Use MQTT:
  • ❌ Standard web applications (use REST/WebSocket instead)
  • ❌ High-bandwidth data transfer
  • ❌ Request/response pattern needed

How MQTT Works

Key Characteristics:
  • Extremely lightweight (2-byte header)
  • Publish/Subscribe pattern
  • Three Quality of Service (QoS) levels
  • Works on unreliable networks
  • Battery-efficient

Connection

Python (paho-mqtt)

JavaScript (MQTT.js)

ESP32 (Arduino)


Topics

Topic Structure

Wildcards


Publishing Messages

Python Example

JavaScript Example


Quality of Service (QoS)

QoS 0 - At Most Once

Use for:
  • Non-critical data
  • High-frequency updates
  • Where message loss is acceptable

QoS 1 - At Least Once

Use for:
  • Important data
  • Must be delivered at least once
  • Can handle duplicate messages

QoS 2 - Exactly Once

Use for:
  • Critical financial transactions
  • Where duplicates cannot be tolerated
  • Payment processing

Last Will and Testament


Retained Messages


Advanced Usage

Persistent Sessions

Batch Messages


Best Practices

1. Error Handling

2. Connection Management

3. Message Validation


Troubleshooting

Connection Failures

  • Check TLS/SSL certificate
  • Verify username/password
  • Ensure port 8883 is accessible
  • Check firewall rules

Missing Messages

  • Verify QoS level
  • Check topic subscriptions
  • Ensure broker permissions
  • Review message queue size

High Latency

  • Reduce message size
  • Optimize topic structure
  • Check network connectivity
  • Consider local broker

Comparison with Alternatives


Security Best Practices

  1. Always use TLS (mqtts://)
  2. Use strong authentication (username/password or client certificates)
  3. Implement access control (topic-based permissions)
  4. Validate all messages (schema validation)
  5. Monitor for anomalies (unusual message patterns)

Interactive Documentation