How-to
Sending Sparkplug B payloads over an MQTT WebSocket client
Short answer
Yes. A WebSocket MQTT client carries Sparkplug B traffic as long as it transports payloads as binary, because the Sparkplug B payload is a protobuf byte sequence that must not pass through text conversion. Decide who encodes and decodes: your application or a component that documents Sparkplug support, then verify byte-for-byte passthrough on the deployed version.
On this page
Yes — any MQTT client that connects over a WebSocket can carry Sparkplug B traffic, because WebSocket is only a different pipe under the same MQTT messages. The hard requirement sits at the payload: the Sparkplug B payload is a protobuf structure, defined with Google Protocol Buffers as a compact binary serialization format, so the client must move it as binary and hand it back unchanged. A clean connection proves the transport and nothing more. The real decision is which component encodes and decodes the Sparkplug packets — your application, or a client that documents that capability. This article gives you the boundary, a procedure that proves binary passthrough end to end, and the decision for both integration cases.
What a WebSocket changes and what it does not #
Over an MQTT-over-WebSocket connection the client still speaks MQTT: the WebSocket session replaces the raw TCP socket, and the MQTT packets travel inside WebSocket binary frames. The broker routes every message by topic, whichever listener it arrived on, and it treats the payload as opaque bytes. Sparkplug B adds its topic namespace and protobuf payload on top of MQTT, so it inherits that indifference — the transport underneath can change without touching the message.
That is the client-side twin of a point this library makes about brokers: broker support is not Sparkplug support, because support at one layer never proves support at the layer above it.
One operational note: a WebSocket listener crosses the plant network boundary exactly the way a plain MQTT listener does, so put the same TLS and authentication on both.
The boundary that decides compatibility #
Separate the two layers that feature lists tend to blur:
| Layer | What it does | Who provides it |
|---|---|---|
| MQTT client | Connects, subscribes and publishes, over TCP or WebSocket | The client library or tool |
| Sparkplug codec | Builds the protobuf payload before publish, parses it after receive | Your application, or a component whose documentation states Sparkplug support |
| Broker | Routes messages by topic | The broker; the payload is opaque to it |
A WebSocket client that passes binary payloads untouched can carry Sparkplug packets it never understands — that is the normal case. What you must not do is infer encoding or decoding from the words MQTT and WebSocket in a feature list. If the vendor does not document Sparkplug support for that client and version, the codec is your job.
Decide where the Sparkplug codec lives #
- Your application already builds Sparkplug B packets. You need transport only. Any MQTT WebSocket client that accepts raw bytes on publish and returns raw bytes on receive will carry them; choose the client on transport grounds such as reconnect behavior, TLS support and last-will handling.
- You expect the client to encode and decode Sparkplug packets. Treat the capability as absent until the vendor documents it for the exact client and version you will run. Where it is absent, keep the codec in your application or in an edge component that documents Sparkplug support, and let the WebSocket client stay pure transport.
Decision: in the first case, pick the client on transport qualities alone; in the second, do not deploy until the codec's home is documented, because an undocumented assumption here is what turns into payloads your decoder rejects.
Prove binary passthrough before you trust the path #
- Build or capture one known Sparkplug B payload you can reproduce exactly; an illustrative choice for the test is a small DDATA message carrying a handful of metrics with known values and types.
- Publish it through the WebSocket client to a test topic, for example
sparkplug-test/binary-check, handing the payload to the client as raw bytes. If the client's API accepts strings only, that client is the wrong tool for this job. - Subscribe to that topic over a plain TCP MQTT connection and save exactly what arrives.
- Compare what arrived against what you sent: the byte count must match and the bytes must match one for one. A longer printable string means something in the chain converted binary to text.
- Feed the received bytes to your Sparkplug decoder and confirm it returns the metric values and types you published.
- Run the test in the other direction as well — publish from the plain connection, receive on the WebSocket client, decode — because publish and subscribe paths can behave differently.
- Repeat the whole test on the deployed client version after every upgrade; a client that passed on one release proves nothing about the next.
What text conversion does to a payload #
A protobuf decoder reads an exact byte sequence, so any text conversion breaks it. Worked example with illustrative sizes: a DDATA payload whose protobuf encoding is 120 bytes gets base64-encoded somewhere in the chain. Base64 writes every 3 bytes as 4 characters: 120 / 3 = 40 groups and 40 x 4 = 160 characters, and because 120 divides evenly by 3 there is no padding. The subscriber then receives 160 bytes of printable text instead of the original 120 — 40 extra bytes, one third more (40 / 120 = 0.33) — and the first protobuf field tag no longer matches, so the decoder rejects the message on arrival.
Hex conversion fails the same way and doubles the length, at 2 characters per byte. These are example sizes for the arithmetic, not a universal payload figure; real sizes depend on the metric count and types in each message.
One caution when you inspect traffic: viewers that auto-detect payload formats can misfire. A public issue report on MQTT-Explorer 0.4.0-beta4 describes some payloads being decoded as Sparkplug when they were not meant to be Sparkplug at all. A clean decode in a viewer is a hint about the bytes, never proof about your pipeline; validate against your own decoder and known tag values.
The receiving side has failure modes of its own, covered in the knowledge-base page on decoding Sparkplug payloads and resolving aliases.
The decision #
- If your application produces the protobuf payloads: use any WebSocket MQTT client that passes binary untouched, run the byte-for-byte test on the deployed version and again after each upgrade, and you are done. The WebSocket choice changes nothing about how Sparkplug works.
- If you expected the client to encode and decode: do not deploy on a feature-list assumption. Put the codec in your application or in an edge component whose documentation states Sparkplug support, keep the WebSocket client as transport, and verify with the same test.
Either way the rule is one sentence: Sparkplug B over WebSocket works when the bytes that enter the client are the bytes that leave it.
Questions asked
Do I need to change the broker to run Sparkplug B over WebSocket?
No. A broker that offers a WebSocket listener alongside its plain MQTT listener carries Sparkplug traffic on both, because it routes by topic and treats every payload as opaque bytes. The change is on the client side: connect the client to the WebSocket listener and keep the payload binary. Broker-side Sparkplug behavior does not depend on which listener a client used.
Which component should encode and decode Sparkplug B in a browser-based client?
Plan on your application doing the codec work. Protobuf libraries exist for browser JavaScript, so your own web code can build and parse Sparkplug payloads, but a general-purpose MQTT WebSocket client should not be assumed to understand Sparkplug. If a vendor claims client-side Sparkplug support, treat it as unverified until the documentation states it for the client version you actually run.
What is the fastest way to confirm binary passthrough?
Publish one known Sparkplug B payload through the WebSocket client, receive it over a plain MQTT connection, and compare byte counts and a checksum before handing the bytes to your Sparkplug decoder. Run the test in both directions and repeat it after any client upgrade. A session that connects cleanly proves the transport and nothing about the payload path.
Why does my Sparkplug payload arrive as a base64 string?
Something between the publisher and your decoder converted the binary payload to text — a bridge, a logging rule, or a client or gateway configured to handle payloads as strings. Locate the component doing the conversion and remove or reconfigure it. A protobuf decoder expects the raw byte sequence, so it cannot read the base64 text no matter how intact the content is.