Modern PetstoreAPI Docs
HomeGuides
Modern PetstoreAPIClassic PetstoreAPI
HomeGuides
Modern PetstoreAPIClassic PetstoreAPI
  1. Protocol Guides
  • Introduction
  • Quick Start Guide
  • API Protocols Guide
  • Protocol Guides
    • REST API
    • GraphQL
    • gRPC
    • Server-Sent Events (SSE)
    • WebSocket
    • Socket.IO
    • MQTT
    • Webhooks
    • MCP (Model Context Protocol)
HomeGuides
Modern PetstoreAPIClassic PetstoreAPI
HomeGuides
Modern PetstoreAPIClassic PetstoreAPI
  1. Protocol Guides

PetstoreAPI MQTT Protocol Guide

Modern Petstore API provides MQTT (Message Queuing Telemetry Transport) support for IoT devices and resource-constrained environments. Our MQTT broker at mqtts://mqtt.petstoreapi.com:8883 enables publish/subscribe messaging for pet health monitoring devices, order status updates, payment notifications, and inventory alerts with minimal bandwidth overhead and reliable delivery guarantees.

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#

Publisher (IoT Device)              Broker                  Subscriber
    │                                 │                         │
    ├──────── Publish ───────────────>│                         │
    │      Topic: orders/abc123/status │                         │
    │                                 │────────────────────────>│
    │                                 │      Forward Message    │
    │                                 │                         │
    ├──────── Publish ───────────────>│                         │
    │      Topic: payments/xyz789/status                       │
    │                                 │────────────────────────>│
    │                                 │      Forward Message    │
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#

pets/{petId}/availability       # Pet availability updates
pets/{petId}/status             # Pet status changes
orders/{orderId}/status         # Order status updates
orders/{orderId}/payment        # Payment status
payments/{paymentId}/status     # Payment processing updates
inventory/{productId}/stock     # Stock level updates

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#

FeatureMQTTWebSocketHTTP
PatternPub/SubBidirectionalRequest/Response
OverheadVery LowLowHigh
Battery Efficient✅⚠️❌
Unreliable Networks✅❌❌
QoS Levels✅❌❌
IoT Support✅⚠️❌

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#

API Specification: AsyncAPI 3.0
MQTT Quick Start: MQTT Guide
Protocol Overview: All Protocols

Related Resources#

MQTT Specification
AsyncAPI for MQTT
Quick Start Guide
Modified at 2026-01-06 09:34:39
Previous
Socket.IO
Next
Webhooks
Built with