New: the AI firewall watches the whole platform and explains each incident in plain words. See how it works

MQTT vs HTTP for IoT devices: choosing by what the device does

Pick MQTT or HTTP by the device's job: how often it reports, whether it must hear commands at once, and what happens when its link drops.

For IoT devices, choose MQTT when the device stays connected, reports often or must receive commands at once. Choose HTTP when it wakes, sends a reading and sleeps, or sits on a network that only lets web traffic out. Decide by how often the device reports, whether it must listen, and how its network fails.

I write the software on both ends of these links, for logistics trackers and agriculture sensors among others. The header-size argument rarely decides anything; the device’s daily routine does.

When should you use MQTT instead of HTTP?

Use MQTT when the server must reach the device without waiting to be asked. MQTT 3.1.1, an OASIS standard, is a client-server publish/subscribe protocol that runs over TCP/IP or another ordered, lossless, bi-directional connection, so a command can come down the same connection the device publishes on.

HTTP works the other way round. RFC 9110 defines it as a stateless request/response protocol: the client asks and the server answers. A device on HTTP hears about a command only when it asks, so it polls or holds a request open.

The device…Fits betterWhy
reports every few seconds and takes commandsMQTTOne open connection; the server can publish to it at any time
wakes once an hour, sends one reading, sleepsHTTPNo connection to keep alive between readings
sits behind a firewall that only lets web traffic outHTTPS, or MQTT over WebSocketBoth travel as web traffic
must be reported when it drops offMQTTThe open connection shows when it is gone: the server drops a client that misses its keep-alive, and can publish a will message

Is MQTT faster than HTTP?

The cost per message and per connection matters more than raw speed. An MQTT client opens one connection and keeps it, and its keep-alive packet, PINGREQ, is two bytes. An HTTP request carries a request line and headers every time, and a device that reconnects for each request pays the connection setup, and the TLS handshake on HTTPS, each time.

The open connection has a price too. An MQTT client must send a packet within every Keep Alive interval, a PINGREQ if nothing else, and the server disconnects a client it has not heard from in one and a half times that interval. On a metered cellular link, that interval sets a floor on traffic even when nothing happens, which is one reason I put sleeping devices on HTTP.

Hermetry connects field devices to live dashboards, rules and alerts: IoT telemetry and SCADA in one platform. It takes device data over MQTT 3.1.1, with TLS on port 8883, and decides a device’s connection state itself, so a will message is accepted but ignored. It also takes data over an HTTPS device API where each message is one POST with the device’s token in an Authorization: Bearer header. Both end in the same place, a device with its telemetry, attributes, alarms and dashboards, and an HTTPS device can wait for commands with a long poll. The comparison is in choose how to connect your devices.

Can a web browser use MQTT?

Yes, over WebSocket, as long as the broker offers a WebSocket listener. Section 6 of MQTT 3.1.1 defines how MQTT runs over a WebSocket connection: control packets travel in binary WebSocket frames, and the client offers the subprotocol name mqtt.

The same route helps a device behind a proxy that passes only web traffic. I try plain MQTT over TLS first, though: the spec notes that TCP ports 8883 and 1883 are registered with IANA for MQTT with and without TLS, and a firewall rule for one known port is easier to review, as in IoT network segmentation for plants.

Frequently asked questions

What do the MQTT QoS levels mean?

MQTT has three quality of service levels. QoS 0 is at most once: the message may be lost. QoS 1 is at least once: it arrives but may be duplicated. QoS 2 is exactly once, for when neither loss nor duplication is acceptable, at the cost of more packets.

Does MQTT need a broker?

Yes. MQTT is a client-server protocol: devices and applications are clients, and the server, usually called the broker, accepts their connections and passes each published message to the clients that subscribed to it. Publishers and subscribers do not need to know about each other; the specification calls this decoupling of applications.

Is CoAP an alternative to MQTT and HTTP?

For very constrained devices, yes. RFC 7252 defines the Constrained Application Protocol (CoAP) as a web transfer protocol for low-power, lossy networks, with a request/response model like HTTP and very low overhead. It is designed to interface easily with HTTP.

Share

See your field data live this week

Start a trial in your region with your first devices, or book a demo and bring your own use case.