MQTT Explorer uses Protocol Buffers (protobuf) for efficient binary serialization of internal IPC messages. This provides significant performance improvements over JSON serialization for high-throughput message passing between the renderer and main process.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/thomasnordquist/MQTT-Explorer/llms.txt
Use this file to discover all available pages before exploring further.
Why Protocol Buffers?
Protocol Buffers offer several advantages for IPC:Performance
Binary encoding is faster to serialize/deserialize than JSON
Size
Encoded messages are smaller, reducing memory overhead
Type Safety
Schema validation ensures message structure integrity
Backwards Compatibility
Field numbers allow schema evolution without breaking changes
Message Codec Implementation
TheMessageCodec class provides binary encoding/decoding for MQTT messages:
Schema Structure
The protobuf schema defines anEnvelope message with two fields:
| Field | Type | ID | Description |
|---|---|---|---|
topic | string | 1 | MQTT topic string |
payload | bytes | 2 | Binary payload data |
Field IDs (1, 2) are permanent identifiers. Never reuse or change IDs to maintain backwards compatibility.
MessageCodec API
TheMessageCodec class provides three static methods:
encode(topic, data)
Serializes a message to binary format:decode(binary)
Decodes a binary message:decodeWithPayload<T>(binary)
Decodes and parses the payload as JSON:Usage Pattern
The MessageCodec is used for IPC between Electron processes:Performance Characteristics
Encoding Benchmark
Size Comparison
For a typical MQTT message:| Format | Size | Reduction |
|---|---|---|
| JSON | 89 bytes | - |
| Protobuf | 61 bytes | 31% smaller |
Size savings increase with larger payloads and more structured data.
Type Definitions
The module exports TypeScript interfaces:Protobuf vs Sparkplug
MQTT Explorer uses Protocol Buffers in two different contexts:| Context | Purpose | Schema | Library |
|---|---|---|---|
| IPC Messages | Internal communication | Custom (Envelope) | protobufjs |
| Sparkplug B | MQTT payload decoding | Sparkplug spec | sparkplug-payload |
Advanced: Schema Evolution
Protocol Buffers support backwards-compatible schema changes:Safe Changes
Add Optional Fields
New fields with higher IDs are ignored by old decoders
Add New Message Types
Extend the schema without affecting existing messages
Unsafe Changes
Error Handling
The MessageCodec doesn’t explicitly throw errors, but decoding can fail:Common Errors
Error: invalid wire type
Error: invalid wire type
The binary data is not valid Protocol Buffers:
- Check that you’re decoding data encoded with the same schema
- Verify the binary wasn’t corrupted during transmission
- Ensure you’re not trying to decode JSON as protobuf
Error: Unexpected token in JSON
Error: Unexpected token in JSON
The payload contains invalid JSON:
- The encoder expects JSON-serializable data
- Check for circular references in the data object
- Verify the data isn’t undefined or a function
TypeError: Envelope.encode is not a function
TypeError: Envelope.encode is not a function
The protobuf schema wasn’t loaded correctly:
- Ensure
protobufjsis installed:npm install protobufjs - Verify the schema definition is valid JSON
- Check that
Root.fromJSON()succeeded
Dependencies
The MessageCodec requires:Using with Custom Decoders
You can use protobuf schemas in custom MQTT message decoders:Loading .proto Files
For complex schemas, use.proto files instead of JSON:
.proto file:
MQTT Explorer uses JSON schemas for simplicity, but
.proto files offer better tooling support.Best Practices
Cache Compiled Types
Compile protobuf types once at startup, not per message
Validate Before Encoding
Use
Type.verify() to catch invalid data before encodingHandle Partial Data
Protobuf decodes partial messages - validate completeness
Document Field Numbers
Comment what each field ID represents for maintainability
Use Type Guards
Add TypeScript type guards for decoded message validation
Profile Performance
Measure encoding/decoding time for your specific use case
See Also
- Message Decoders - Custom decoder development
- Sparkplug B Decoder - Sparkplug B protobuf usage
- Protocol Buffers Documentation - Official protobuf docs
- protobufjs GitHub - Library documentation