What is Sparkplug B?
Sparkplug B is a specification that defines:- Topic namespace: Structured topic hierarchy for industrial devices
- Payload encoding: Binary Protocol Buffer encoding for efficiency
- State management: Birth/Death certificates for device lifecycle
- Data types: Rich metric types including templates and datasets
Sparkplug B is widely used in industrial automation, manufacturing, oil & gas, and other SCADA applications.
Automatic Topic Detection
The Sparkplug decoder automatically activates when it detects topics matching the Sparkplug B pattern:Topic Structure
Sparkplug topics follow this format:spBv1.0- Namespace (Sparkplug B version 1.0){group_id}- Logical grouping of edge nodes{message_type}- Message type (see below){edge_node_id}- Edge node identifier{device_id}- Optional device identifier
Message Types
NBIRTH
Node Birth CertificatePublished when an edge node comes online. Contains node-level metrics and metadata.
DBIRTH
Device Birth CertificatePublished when a device connects. Contains all device metrics and their current values.
NDATA
Node DataPublished periodically with updated node metrics.
DDATA
Device DataPublished with updated device metrics. Most common message type.
NCMD
Node CommandCommands sent to control an edge node.
DCMD
Device CommandCommands sent to control a device.
NDEATH
Node Death CertificatePublished by the broker when a node disconnects (via Last Will).
DDEATH
Device Death CertificatePublished when a device disconnects.
Example Topics
Decoder Implementation
The Sparkplug decoder uses thesparkplug-payload library to decode binary payloads:
How It Works
Topic pattern matching
When a message arrives, the decoder checks if the topic matches the Sparkplug B pattern.
Binary payload decoding
The binary payload is decoded using the
sparkplug-payload library’s Protocol Buffer decoder.Decoded Payload Structure
Sparkplug payloads contain a timestamp and array of metrics:Supported Metric Types
Sparkplug B supports a rich set of data types:Primitive Types
| Type | Description | Example |
|---|---|---|
Boolean | True/false value | true |
Int8 | 8-bit signed integer | 127 |
Int16 | 16-bit signed integer | 32000 |
Int32 | 32-bit signed integer | 2147483647 |
Int64 | 64-bit signed integer | 9223372036854775807 |
UInt8 | 8-bit unsigned integer | 255 |
UInt16 | 16-bit unsigned integer | 65535 |
UInt32 | 32-bit unsigned integer | 4294967295 |
UInt64 | 64-bit unsigned integer | 18446744073709551615 |
Float | 32-bit floating point | 3.14 |
Double | 64-bit floating point | 3.14159265359 |
String | UTF-8 string | "Hello" |
DateTime | Timestamp (ms since epoch) | 1638360000000 |
Text | Long-form text | "Description..." |
Complex Types
DataSet
DataSet
Represents tabular data with typed columns:
Template
Template
Reusable metric structure definitions:Template instances reference the definition:
Birth Certificates
Birth certificates establish the initial state and metadata for nodes and devices.Node Birth Example
Device Birth Example
Birth certificates should include all metrics a device supports. Data messages only need to include changed metrics.
Testing Sparkplug Decoding
MQTT Explorer includes a mock Sparkplug client for testing:Start the mock client
The test suite includes a Sparkplug simulator that publishes realistic NBIRTH, DBIRTH, and DDATA messages.
Mock Client Configuration
The mock client is configured insrc/spec/mock-sparkplugb.ts:
State Management
Sparkplug B includes sophisticated state management:Node Control/Rebirth
TheNode Control/Rebirth metric allows SCADA systems to request a rebirth:
Device Commands
Devices respond to DCMD messages:Best Practices
Use Birth Certificates
Always publish complete BIRTH messages with all metrics when connecting
Set Last Will
Configure NDEATH as the Last Will and Testament (LWT) message
Include Timestamps
Add timestamps to metrics for time-series data integrity
Namespace Your Metrics
Use hierarchical names like
Properties/hw_version for organizationMinimize DDATA Size
Only send changed metrics in data messages
Use Templates
Define templates for repeated metric structures
Dependencies
Sparkplug B support requires these packages:- sparkplug-payload: Encoder/decoder for Sparkplug B payloads
- sparkplug-client: Full Sparkplug B client (used in tests)
Troubleshooting
Decoder doesn't activate automatically
Decoder doesn't activate automatically
Verify the topic matches the Sparkplug pattern exactly:Common issues:
- Wrong version (must be
spBv1.0) - Invalid message type (must be DATA, CMD, DEATH, or BIRTH)
- Missing edge node ID
Failed to decode sparkplugb payload
Failed to decode sparkplugb payload
This error indicates the binary payload is not valid Sparkplug B:
- Check that the publisher is using
sparkplug-payloador compatible encoder - Verify the payload is binary (not JSON text)
- Try publishing from the mock client to confirm the decoder works
Missing metrics in DDATA
Missing metrics in DDATA
This is normal behavior. Sparkplug B only requires changed metrics in data messages:
- BIRTH messages contain all metrics
- DATA messages only contain updates
- Use the birth certificate as the schema reference
See Also
- Message Decoders - General decoder architecture
- Protobuf Integration - Protocol Buffer usage in MQTT Explorer
- Debugging Techniques - Troubleshooting decoder issues
- Eclipse Sparkplug Specification