When Should You Use MQTT Instead of HTTP for APIs?

MQTT excels for IoT devices with limited bandwidth and unreliable networks. Learn when MQTT beats HTTP and how Modern PetstoreAPI uses MQTT for pet tracking devices and smart feeders.

Ashley Innocent

Ashley Innocent

13 March 2026

When Should You Use MQTT Instead of HTTP for APIs?

TL;DR

Use MQTT for IoT devices with limited battery, unreliable networks, or pub-sub messaging patterns. Use HTTP for standard web/mobile APIs. MQTT uses 2-byte headers vs HTTP’s 100+ bytes, making it ideal for constrained devices. Modern PetstoreAPI implements MQTT for pet tracking collars and smart feeders.

Introduction

Your pet tracking collar needs to send location updates every 5 minutes. It runs on a coin battery that should last 6 months. Using HTTP, the battery dies in 2 weeks. Using MQTT, it lasts the full 6 months.

HTTP is standard for APIs, but it’s designed for web browsers, not IoT devices. MQTT (Message Queuing Telemetry Transport) was built for constrained devices with limited bandwidth and unreliable networks.

Modern PetstoreAPI uses HTTP for web and mobile apps, but MQTT for IoT devices: pet tracking collars, smart feeders, and health monitors.

💡
If you’re building or testing IoT APIs, Apidog supports MQTT testing alongside HTTP. You can test pub-sub patterns, validate message formats, and simulate network failures.
button

In this guide, you’ll learn when MQTT beats HTTP, see real examples from Modern PetstoreAPI, and discover how to choose the right protocol for your use case.

What Is MQTT?

MQTT is a lightweight pub-sub messaging protocol designed for IoT.

How MQTT Works

Devices publish messages to topics. Other devices subscribe to topics:

Publisher (Pet Collar):
  Topic: pets/019b4132/location
  Payload: {"lat":37.7749,"lng":-122.4194,"battery":85}

Subscriber (Mobile App):
  Subscribe to: pets/019b4132/location
  Receives: {"lat":37.7749,"lng":-122.4194,"battery":85}

MQTT Broker sits in the middle, routing messages from publishers to subscribers.

Key MQTT Features

1. Tiny headers - MQTT: 2 bytes, HTTP: 100-500 bytes

2. Persistent connections - MQTT keeps connections open

3. Quality of Service (QoS) - QoS 0/1/2 for delivery guarantees

4. Last Will - Message sent if device disconnects unexpectedly

5. Retained messages - Broker stores last message for new subscribers

MQTT vs HTTP Comparison

Feature MQTT HTTP
Header Size 2 bytes 100-500 bytes
Pattern Pub-Sub Request-Response
Connection Persistent Per-request
Bandwidth Very low Higher
Battery Impact Minimal Significant
Browser Support Via WebSocket Native

Bandwidth Example

1000 location updates per day:

MQTT uses 8x less bandwidth.

When MQTT Wins

1. IoT Devices with Limited Battery

Pet tracking collar:

Why: Persistent connection, tiny headers, less radio time.

2. Unreliable Networks

Cellular IoT devices with spotty coverage:

3. Many-to-Many Communication

Smart pet feeder scenario:

Feeder 1 → pets/019b4132/feeding
Feeder 2 → pets/019b4127/feeding
App 1 → subscribes to pets/+/feeding (all pets)
App 2 → subscribes to pets/019b4132/feeding (one pet)

4. Real-Time Sensor Data

Pet health monitor sending updates every second:

When HTTP Wins

1. Standard Web/Mobile Apps

HTTP is universal:

2. Request-Response Patterns

Getting pet details:

GET /pets/019b4132
200 OK
{"name":"Fluffy","species":"CAT"}

HTTP is simpler for request-response.

3. Caching Requirements

HTTP caching works out of the box:

MQTT has no caching.

4. RESTful APIs

HTTP status codes, methods, and semantics:

How Modern PetstoreAPI Uses MQTT

Modern PetstoreAPI implements MQTT for IoT devices.

Pet Tracking Collars

Collar publishes location:

Topic: pets/019b4132/location
QoS: 1 (at least once)
Payload: {
  "lat": 37.7749,
  "lng": -122.4194,
  "battery": 85,
  "timestamp": "2026-03-13T10:30:00Z"
}

Mobile app subscribes:

const mqtt = require('mqtt');
const client = mqtt.connect('mqtts://mqtt.petstoreapi.com');

client.subscribe('pets/019b4132/location');

client.on('message', (topic, message) => {
  const location = JSON.parse(message);
  updateMap(location.lat, location.lng);
});

Smart Feeders

Feeder subscribes to schedule:

Topic: pets/019b4132/feeding-schedule
Retained: true
Payload: {
  "times": ["08:00", "18:00"],
  "amount": 100
}

Feeder publishes feeding events:

Topic: pets/019b4132/feeding-events
Payload: {
  "timestamp": "2026-03-13T08:00:15Z",
  "amount": 100,
  "dispensed": true
}

Health Monitors

Monitor publishes vitals:

Topic: pets/019b4132/health
QoS: 0 (fire and forget, high frequency)
Payload: {
  "heartRate": 120,
  "temperature": 38.5,
  "activity": "resting"
}

Testing MQTT with Apidog

Apidog supports MQTT testing alongside HTTP and other protocols.

Test MQTT Pub-Sub

  1. Connect to MQTT broker
  2. Subscribe to topics
  3. Publish test messages
  4. Validate message format
  5. Test QoS levels

Simulate Network Failures

Compare with HTTP

Test the same functionality with both protocols:

Conclusion

MQTT and HTTP serve different purposes. Use MQTT for IoT devices with limited resources. Use HTTP for standard web/mobile APIs.

Modern PetstoreAPI shows how to use both: HTTP for user-facing APIs, MQTT for IoT devices. The right protocol depends on your constraints, not which is “better.”

Test both protocols with Apidog to find the best fit for your use case.

button

FAQ

Can MQTT work over HTTP?

MQTT can run over WebSocket, which works over HTTP. This helps with firewall traversal but loses some MQTT efficiency benefits.

What are MQTT QoS levels?

Is MQTT secure?

Yes, MQTT supports TLS encryption (MQTTS) and username/password authentication. Modern PetstoreAPI uses MQTTS for all IoT devices.

Can browsers use MQTT?

Browsers can use MQTT over WebSocket. Libraries like MQTT.js support browser environments.

How does MQTT compare to WebSocket?

MQTT is a protocol that can run over WebSocket. WebSocket is a transport layer. MQTT adds pub-sub, QoS, and IoT-specific features on top of WebSocket.

Explore more

Claude Mythos vs Claude Opus 4.6: what the leaked benchmarks mean for developers

Claude Mythos vs Claude Opus 4.6: what the leaked benchmarks mean for developers

Claude Mythos (internal codename “Capybara”) appeared in accidentally exposed Anthropic documents. Reported to achieve “dramatically higher scores” than Opus 4.6 on coding, academic reasoning, and cybersecurity.

10 April 2026

Grok Imagine Video vs Sora 2, Veo 3, Seedance, WAN, and Vidu: 2026 comparison

Grok Imagine Video vs Sora 2, Veo 3, Seedance, WAN, and Vidu: 2026 comparison

Grok Imagine Video ($0.05/second) competes on price with Seedance 1.5 Pro but caps at 720p while most competitors offer 1080p. Granular duration control (1-second increments up to 15 seconds) and no cold starts are genuine advantages.

10 April 2026

GLM-5 vs DeepSeek V3 vs GPT-5: speed, cost, and practical developer comparison

GLM-5 vs DeepSeek V3 vs GPT-5: speed, cost, and practical developer comparison

For real-time apps, GLM-5 and DeepSeek are fastest at short prompts. For tool-heavy assistants, GPT-5 leads on schema stability.

10 April 2026

Practice API Design-first in Apidog

Discover an easier way to build and use APIs