MQTT is a lightweight and flexible carrier for moving production data from the field to the centre. But it does not define the topic structure, the payload format or the answer to the question “is this device really online right now?”. When every project fills this gap differently, gateways’ topic and payload structures diverge from each other. Sparkplug B is the specification published by the Eclipse Foundation and also standardised as ISO/IEC 20237; it fixes the topic structure, the birth/death mechanism, sequence numbers, aliases and Primary Host Application behaviour. Below, we describe these together with the problems we encounter during commissioning.
Where plain MQTT gets stuck in the field
In projects that start with plain MQTT, the following problems are common:
- No agreement on topics and payloads: Each gateway uses its own topic tree and JSON fields; a separate parser is written for each source on the consumer side.
- Stale data: A retained message stores the last value, but when the device shuts down, this value continues to look current. SCADA takes a temperature from two hours ago for a live value.
- No discovery: When a new device is added, the consumer does not know in advance which variables it will publish, with which data type and unit.
- No detection of lost messages: If a change sent with QoS 0 is lost, the consumer does not notice.
- Uncertainty after an outage: When the connection comes back, whether the data from the gap was buffered and sent is undefined.
Four concepts do different jobs: QoS determines how a message is delivered between the client and the broker; a retained message stores the last value of a topic but does not show that the value is current; session state determines whether subscriptions and pending messages are kept while the connection is down; and preventing the same record from being processed twice in the application is a separate check the consuming application performs with the record ID.
Sparkplug B solves these not by inventing a new protocol, but by using MQTT’s existing features (Last Will and Testament, QoS, retained messages) with specific rules and by defining a binary payload schema. If the protocol decision has not been made yet, first look at the comparison of OPC UA and MQTT.
Topic namespace structure
In Sparkplug B, topics are not free-form; they follow this pattern:
spBv1.0/<group_id>/<message_type>/<edge_node_id>[/<device_id>]
# Examples
spBv1.0/Factory1/NBIRTH/Line3-Edge
spBv1.0/Factory1/DDATA/Line3-Edge/Filler01
spBv1.0/Factory1/DCMD/Line3-Edge/Filler01
spBv1.0/STATE/scada-primary
group_id groups edge nodes logically (plant or department). edge_node_id is the gateway or PLC that establishes the MQTT connection. device_id is the device behind that edge node. Message types:
| Message | Direction | Purpose |
|---|---|---|
| NBIRTH / DBIRTH | Edge → Host | Announces that the node or device is online, with all metric definitions |
| NDATA / DDATA | Edge → Host | Changed metric values |
| NDEATH / DDEATH | Edge → Host | Announces that the node or device is offline |
| NCMD / DCMD | Host → Edge | Write command or rebirth request |
| STATE | Host | Online/offline state of the host application |
The topic being fixed and flat is a deliberate choice: hierarchical information such as area, line and cell is carried not in the topic but in metric names and the definitions in the birth message. This difference matters for teams that want to build a topic tree with an ISA-95-like hierarchy. We described how the two approaches can live together in the article Unified Namespace architecture.
State management with birth and death messages
The core idea is this: all data gains meaning within a session. The flow:
- When the edge node connects to the broker, it registers NDEATH as its MQTT Will message (QoS 1, retain off). The payload contains a
bdSeq(birth/death sequence number) metric. - Once connected, it publishes NBIRTH. NBIRTH contains the name, data type and initial value of all the node’s metrics, and the same
bdSeqvalue. - A DBIRTH is published for each device behind it; device metrics are defined here.
- After that, only changed values are sent with NDATA/DDATA.
- If communication with a device is lost, the edge node publishes DDEATH. If the edge node itself drops unexpectedly, the broker publishes the registered Will message, i.e. NDEATH.
When the host receives NDEATH, it marks the metrics belonging to that node and all devices under it as stale. Matching the bdSeq prevents a delayed NDEATH from an old session from accidentally closing the new session; the host only takes into account an NDEATH with the same bdSeq as the last NBIRTH. If a gateway restarts frequently, without this matching a live machine appears “offline” on screen.
Birth messages are not sent retained. If the host connects later, it requests a rebirth to learn the current state: it sends the relevant edge node an NCMD with the Node Control/Rebirth metric set to true, and the edge node republishes its NBIRTH and DBIRTHs.
Sequence numbers, aliases and payload encoding
Every message the edge node publishes carries a seq value that increments from 0 to 255 and wraps back to 0 after 255; in version 3.0 of the specification, the sequence starts at 0 in NBIRTH. By tracking this value, the host catches a message lost in between. If a gap is detected and the expected message does not arrive within a reasonable time, the host requests a rebirth to resynchronise the state. This way, missing messages can be detected on the host side even for data sent with QoS 0.
The payload is a binary structure encoded with Google Protocol Buffers. Each metric contains a name, an optional alias, a timestamp (Unix epoch, milliseconds), a data type and a value; it can also carry flags such as is_historical and is_null, and property sets for information such as units. Template definitions are also supported for complex structures.
Aliases are for bandwidth. In the birth message, each metric is assigned an integer alias; in subsequent DATA messages, only this alias is sent instead of the long metric name. Carrying an integer of a few bytes instead of “Line3/Filler01/Process/ProductTemperature” significantly reduces message size on a gateway publishing thousands of metrics. The cost is clear too: a consumer that has not seen the birth message cannot interpret DATA messages. That is why rebirth exists.
A binary payload makes debugging harder. mosquitto_sub -h broker -t 'spBv1.0/#' -v shows the topics, but the payload scrolls on screen as meaningless characters. Keep an MQTT client with a Sparkplug decoder, or a small script working with the protobuf schema, at hand; don’t start looking for one at the moment of a fault.
The Primary Host Application and reconnection
The main consumer of the data, usually SCADA or a data platform, can be defined as the Primary Host Application. When the host connects, it publishes a retained message to the topic spBv1.0/STATE/<host_id> announcing that it is online, and registers a Will message with “offline” content on the same topic. In version 3.0, this payload is a JSON object with online and timestamp fields.
The edge node subscribes to this STATE topic and follows this rule: if the primary host is not online, it does not publish data; if the host drops, it disconnects and, if available, moves to the next broker on its list. It buffers the data produced during this time (store-and-forward) and sends it with the is_historical flag when the host comes back. This prevents data flowing to the broker in vain while the main consumer is absent. Buffer capacity is a limit that depends on the gateway hardware; we gave a worked example of the calculation in the article edge gateway selection criteria.
In a redundant broker design, edge nodes are configured with a list of brokers; the host also publishes STATE to each broker separately. The edge node connects to the broker on which the primary host appears online. If you use broker clustering, test separately that Will and retained messages behave consistently across the cluster. Try this in a test environment by deliberately taking a broker node out of service.
Comparison with plain MQTT
| Topic | Plain MQTT | Sparkplug B |
|---|---|---|
| Topic structure | Free-form, project-specific | spBv1.0/group/type/node/device |
| Payload | Free-form (mostly JSON) | Protocol Buffers, defined schema |
| Online state | Project-specific use of LWT | NBIRTH/NDEATH, DBIRTH/DDEATH, STATE |
| Metric discovery | None | All definitions in the birth message |
| Detecting lost messages | None | With seq |
| Data after an outage | Undefined | Store-and-forward, is_historical |
| Readability | High (JSON) | Low; a decoder is needed |
| Navigation by hierarchical topic | Possible | Topic is flat; hierarchy in metric names |
Sparkplug B is strong in installations where many gateways write to a single SCADA or data platform and state awareness is essential. If most consumers are general-purpose IT applications and readable JSON is expected, using Sparkplug B in the field and publishing to hierarchical JSON topics at the centre with a converter is a common middle way. The current text of the specification is on the Eclipse Sparkplug site, and MQTT’s own resources are at mqtt.org.
Commissioning checklist
- The broker supports MQTT 3.1.1 or 5.0; Will and retained message behaviour has been tested.
- Edge nodes connect with a clean session (Clean Session in 3.1.1; Clean Start and zero session expiry in 5.0).
- A naming rule for
group_id,edge_node_idanddevice_idis written down; the characters+,#and/, which have special meaning in MQTT, are not used in IDs. - The keep-alive period was chosen so that a disconnection is noticed within an acceptable time.
- The primary host publishes STATE; edge nodes are configured to behave according to this host.
- Store-and-forward capacity has been calculated for the longest expected outage at the centre.
- The rebirth request and
seqgap scenarios have been tested. - TLS and user/certificate authentication are enabled on the broker; each edge node can write only to its own topics.
An example ACL definition in Mosquitto for the last item:
user edge-line3
topic write spBv1.0/Factory1/+/Line3-Edge/#
topic read spBv1.0/Factory1/NCMD/Line3-Edge
topic read spBv1.0/Factory1/DCMD/Line3-Edge/#
topic read spBv1.0/STATE/#
This rule prevents a compromised gateway from publishing fake data or NDEATH on behalf of other nodes. Without an ACL, a single gateway can show the whole line as “offline”. For Sparkplug B-based data collection architecture and gateway configuration, see our OPC UA, MQTT and data integration and edge gateways and protocol conversion pages.
Frequently asked questions
Does Sparkplug B require a special broker?
No. Sparkplug B runs on a standard broker supporting MQTT 3.1.1 or 5.0. It is enough for the broker to handle Will and retained messages correctly; there are brokers offering additional Sparkplug-specific functions, but they are not required.
Why can’t I see Sparkplug B messages in readable form?
The payload is a binary structure encoded with Google Protocol Buffers. To read it, you need an MQTT client with a Sparkplug decoder or a script working with the protobuf schema. If aliases are used in DATA messages, you also need to see the birth message.
Can we store the last value in a retained message in Sparkplug B?
The specification says birth and data messages are not sent retained; only the STATE message is retained. An application that connects later gets the current state with a rebirth request. This rule prevents stale values from looking like live data.
Can Sparkplug B and a Unified Namespace be used together?
Yes. The common approach is this: you use Sparkplug B’s state management for transport from the field to the centre, and at the centre you convert the metrics into readable topics organised according to the ISA-95 hierarchy and publish them.