Msg: High-Performance C++20 Pub/Sub Messaging
Distributed systems, high-performance computing clusters, and real-time simulations rarely rely on a single networking transport. One subsystem might require ultra-low-latency point-to-point UDP multicast, another relies on an enterprise message broker like NATS, while a third pushes updates through Redis.
Msg is a modern C++20 library designed to decouple application code from transport mechanics. It unifies transport protocols, in-flight compression, and message serialization behind a clean, URI-driven publish/subscribe interface.
URI-Based Configuration
Rather than writing transport-specific initialization boilerplate, msg configures endpoints via a concise URI schema:
<transport>;<compression>;<address>
Supported Transports
| Transport | Identifier | Protocol | Typical Use Case |
|---|---|---|---|
| ZeroMQ Pub/Sub | zmq_pub_sub |
TCP | Reliable inter-process and node-to-node telemetry |
| ZeroMQ Radio/Dish | zmq_radio_dish |
UDP | High-rate multicast and loss-tolerant sensor streams |
| NATS | nats |
TCP | Resilient cloud messaging and microservice pub/sub |
| Redis | redis |
TCP | In-memory pub/sub and persistent message cache |
Transparent Compression Engine
Compression is applied and verified automatically in the transport layer:
none: Zero-overhead raw transmission.lz4: Microsecond-speed LZ4 compression for high-bandwidth real-time streams.zstd/zstd:<level>: Configurable Facebook Zstandard compression levels (e.g.zstd:5orzstd:19) for bandwidth-constrained satellite and long-distance WAN telemetry.
Code Examples
1. ZeroMQ Pub/Sub with LZ4 Compression
Publishing and subscribing with transparent compression requires only a couple lines of code:
#include "msg/msg.hpp"
#include <iostream>
int main() {
// Publisher with in-flight LZ4 compression
msg::Publisher pub("zmq_pub_sub;lz4;tcp://*:5555");
// Subscriber automatically decompresses incoming frames
msg::Subscriber sub("zmq_pub_sub;lz4;tcp://127.0.0.1:5555");
sub.subscribe("telemetry");
std::string payload = "SensorPacket: lat=38.29 lon=-76.54 alt=1200";
pub.publish("telemetry", std::as_bytes(std::span(payload)));
if (auto message = sub.receive(std::chrono::milliseconds(500))) {
std::cout << "Received on topic [" << message->topic << "]: "
<< std::string_view(reinterpret_cast<const char*>(message->data.data()),
message->data.size()) << "\n";
}
}
2. Strongly-Typed Google FlatBuffers
In addition to raw byte spans and JSON, msg natively integrates with Google FlatBuffers for zero-copy schema evolution:
#include "msg/msg.hpp"
#include "telemetry_generated.h"
// Publish a typed FlatBuffer message over NATS with Zstandard compression
msg::Publisher pub("nats;zstd:8;nats://127.0.0.1:4222");
flatbuffers::FlatBufferBuilder fbb;
auto loc = CreateLocation(fbb, 38.291, -76.542, 450.0f);
fbb.Finish(loc);
pub.publish("nav.gps", fbb);
Performance Design
- Zero Unnecessary Allocations: Hot transmission paths reuse memory buffers and utilize C++20
std::spanfor zero-copy slicing. - Benchmark Validated: Includes comprehensive Google Benchmark suites evaluating throughput (messages/sec) and round-trip latency across all combinations of transport and compression.