Tech notes 10 min read

MQTT is not arcane either: devices do not shout, they hand messages to a broker

MQTT is not another cable, but a messaging convention that runs on TCP: devices publish data to topics and the broker passes it on to subscribers. QoS sets how far each message is confirmed, retained messages let latecomers see the current state, and the last will reports a dropped connection.

MQTT is not arcane either: devices do not shout, they hand messages to a broker

Connecting devices to the cloud usually comes down to two terms: TCP and MQTT. TCP delivers bytes in order; MQTT is a messaging convention on top of it, solving a different problem: with hundreds of devices, how do you deliver each message only to the people who should see it?

Think of a community notice board. Residents do not knock on every door; they post a notice in the right section, and anyone interested comes to read it. The person posting does not know who will read it, and the readers do not know the poster; both sides only care about the section.

What MQTT actually is

MQTT is a lightweight publish/subscribe protocol, usually running over TCP. It has three roles: publishers, subscribers and the broker in the middle.

Devices do not shout at each other, and they do not connect to every module in the platform. Every message goes to the broker first, and the broker forwards it by topic. Devices only publish; the platform only subscribes to the topics it cares about. Neither side needs to know who the other is.

This is not “another cable”. TCP is the road; MQTT is the envelope format and delivery rules on that road. Without a TCP connection, MQTT cannot run; but TCP alone does not give you topics and subscriptions.

Four words: broker, topic, publish, subscribe

broker The message relay. Common choices are EMQX and Mosquitto; cloud IoT platforms also include a broker. It maintains connections, checks permissions and forwards by topic.

topic The topic, layered with slashes, for example factory/line1/temp. When subscribing, + matches one level and # matches everything below, for example factory/+/temp. A good topic design makes it easy to add devices and sites later; a poor one leaves you patching special cases.

publish Publishing. A device sends a message to a topic; the payload can be JSON or vendor-specific binary. The broker does not care what is inside the payload; it only knows topics and subscriptions.

subscribe Subscribing. If the platform subscribes to factory/#, it receives messages from every device below that level; a device usually subscribes only to its own command topic, not anyone else's.

There are also two easy-to-neglected points: client IDs are different, two devices with the same ID will kick each other off the line; the subject is not a right, knowing that the subject name is not equal to the right to subscribe, and the real doorbar is the ACL of the broker.

Connecting, keepalive and the last will

The device first opens a TCP connection to the broker, by default on port 1883; with TLS it is usually 8883. It then sends CONNECT with the client ID, username and password; the broker replies CONNACK, and only then is the connection established.

Once connected, the client sends PINGREQ at the keepalive interval and the broker replies PINGRESP. If no packet arrives within the agreed time, the broker considers the device offline. This checks whether the connection is alive, not whether the device application is stuck, so field deployments usually add an application-level heartbeat.

A device can register a Last Will when it sends CONNECT. If the connection drops unexpectedly, the broker publishes that message to a designated topic, for example marking the device offline. The platform does not need to keep polling “are you still there?”; it can subscribe to the will topic.

QoS: how far each message is confirmed

MQTT has three QoS levels:

  • QoS 0: At most once. Fire and forget: fast, but messages can be lost. Suitable for high-frequency telemetry where one or two missing points do not change the conclusion.
  • QoS 1: At least once. The receiver acknowledges; if no acknowledgement arrives, the sender retransmits. Nothing is lost, but duplicates are possible. Suitable for alarms, state changes and commands.
  • QoS 2: Exactly once.A few more steps to shake hands, with higher costs, would fit a scenario that does not allow for double-counting; but it would be complicated and would not be open by default.

One caveat: QoS describes the leg between client and broker, not end-to-end exactly-once processing in your business. Reconnects, bridges and consumer retries can all deliver the same message again. Database writes, billing and command execution still need idempotency.

Retained messages: late subscribers can still see current state

Normal messages are gone once published; new subscribers do not see earlier ones. A retained message is different: the broker keeps the last message on each topic and delivers it as soon as a new subscriber connects.

It is good for current state — device online status, current setpoint, latest alarm level — not as a history store. History curves belong in the platform database; do not expect the broker to keep a year of data. Clearing a retained message also takes care: usually you publish an empty retained message to the same topic.

How to use it: start with one device

Step one, choose a broker. For a few devices or a proof of concept, run Mosquitto in Docker yourself. For production, look at concurrent connections, throughput, clustering, access control and operations. A broker is a single point; production needs high availability and backups.

Step two, design the topic tree. Separate tenant, site, device and message type, for example {tenant}/{site}/{device}/telemetry, .../command, .../ack. Do not put passwords or tokens in topics; topics appear in logs and subscription lists.

Step three, give every device its own identity. One account or client certificate per device; ACLs allow it to publish only its own topics and subscribe only to its own command topic. Do not expose 1883 on the public internet; use TLS 8883 at minimum, plus authentication.

Step four, agree on the payload. Define field names, units, timestamps, sequence numbers and version first. MQTT only delivers; the broker does not know whether the JSON says temperature or humidity, and the platform should not guess.

Step five, choose QoS and retention per scenario. High-frequency telemetry uses QoS 0 or 1; alarms and commands use QoS 1; device state uses retained messages. Do not turn on QoS 2 for every topic, and do not retain every message.

Step six, treat offline as normal. Devices lose networks, reboot and change IPs. Use keepalive for the connection, the last will to mark disconnection and an application heartbeat to confirm the program is alive. Commands need timeouts and retries, and retries need deduplication.

What MQTT does not do

It is not a replacement for TCP; it is an application-layer protocol on top of TCP. It is not a database: a retained message keeps only the last value, not history. It is not for large files; firmware and large images belong in object storage. It does not provide encryption or authorization by itself; TLS, accounts and ACLs are separate.

The broker is still a critical component. If the broker goes down, every device looks offline at once. Production needs clustering, monitoring and backups; do not treat it as middleware you install once and forget.

On the factory floor, what it solves

In the field, a gateway often collects from PLCs, meters and sensors, converts the data to MQTT and publishes it to the broker; the platform subscribes to topics and maps data to the right points by device ID. Devices only make outbound connections to the broker, so you do not open a port on the router for every device — a big help behind NAT.

Downlink works the same way: the platform publishes a command to the device's command topic, the gateway subscribes and executes it, then publishes the result to an ack topic. Whether the command ran, when it ran and whether it failed can all be traced through topics.

But MQTT only solves how messages travel, not what the numbers mean. Whether a device reports Modbus registers, vendor binary or ready-made JSON must be settled at the gateway or the integration layer. Decide early which side does the protocol conversion.

How HG IoT connects

HG IoT supports standard MQTT and TCP access channels. MQTT devices can connect directly; where there is a gateway, sub-devices can hang below it, and the platform maps data into a unified device model through a point table.

Once connected: live values, history curves, GPS tracks, online/offline records, threshold alarms, remote IO, command dispatch and OTA. It can run as a public cloud account, or be deployed privately with Docker so data stays in your own server room.

The boundary is clear: MQTT is the envelope; what goes in the payload depends on the device vendor's protocol. We do not reverse-engineer protocols or modify device hardware. Whether a device can be connected starts with its topics, message samples, QoS, authentication and point table.

Do not connect the whole plant at once. Start with one device or gateway: confirm it connects, publishes, the platform receives, and disconnections are visible. Then copy the pattern to the second batch.

If you have an MQTT device, send the topics, a payload sample and the authentication method. We will first see whether the path works, then discuss the schedule.

A similar project to scope?

Send the scenario, the scale and the systems you already run. We will come back with an executable plan and a time estimate — no contract needed first.