rzmq

A fast, asynchronous, pure-Rust implementation of ZeroMQ messaging patterns on Tokio, with io_uring and TCP cork support.

rzmq is a pure-Rust implementation of ZeroMQ messaging built on Tokio. You get the familiar socket patterns (REQ/REP, PUB/SUB, PUSH/PULL and DEALER/ROUTER) as async sockets you .await, with no dependency on the C libzmq library.

It speaks ZMTP 3.1 on the wire, so an rzmq socket can talk to libzmq and other ZeroMQ implementations using NULL, PLAIN or CURVE security. It also falls back to ZMTP 2.0 when an older peer asks for it. Transports are tcp://, ipc:// (Unix only) and inproc://. For rzmq-to-rzmq links there is an optional Noise_XX security mechanism.

Each socket is run by a background actor that owns the connections and protocol state. Your code talks to it through a cloneable Socket handle, which is why dropping a pending send or recv future is safe: the message either stays with you or stays queued for the next call. On Linux, an optional io-uring feature moves TCP I/O onto io_uring, with TCP corking, zero-copy sends and multishot receives available per socket.

The smallest useful program binds a PULL socket, connects a PUSH socket to it and passes one message across:

use rzmq::{Context, Msg, SocketType, ZmqError};

#[tokio::main]
async fn main() -> Result<(), ZmqError> {
    let ctx = Context::new()?;
    let pull = ctx.socket(SocketType::Pull)?;
    pull.bind("tcp://127.0.0.1:5555").await?;

    let push = ctx.socket(SocketType::Push)?;
    push.connect("tcp://127.0.0.1:5555").await?;
    push.send(Msg::from_static(b"hello")).await?;

    let msg = pull.recv().await?;
    println!("{}", String::from_utf8_lossy(msg.data().unwrap_or_default()));
    ctx.term().await
}

rzmq is in beta. It is tested on macOS and Linux. Full parity with every libzmq option (ZAP in particular) is not a goal.

Tutorials

Build a task ventilator with PUSH/PULL and DEALER/ROUTERYou will build a small job pipeline over TCP on localhost: a ventilator hands numbered tasks to two workers over PUSH/PULL, the workers send their answers back over DEALER sockets to a ROUTER collector and the whole thing runs and shuts down cleanly from one cargo run.Build a lookup service with REQ and REPYou will build a small port lookup service over TCP on localhost. A REP socket answers questions like "what runs on port 22?" and REQ clients ask them. Along the way you will break the send/receive rule on purpose, share one server between three clients, give up on a slow reply with RCVTIMEO and recover when the server goes away.Build a price feed with PUB/SUB topic filteringYou will build a small price feed over TCP on localhost. One PUB socket publishes stock and currency prices as two-frame messages (topic, then price) and several SUB sockets pick out the topics they care about by prefix. Along the way you change a subscription with UNSUBSCRIBE, see why updates published before a subscriber is ready never reach it and cap what a slow subscriber can hold up with SNDHWM.Build a log shipping pipeline with PUSH and PULLYou will build a small log shipper over TCP on localhost. Three apps push their log lines into one collector, which stamps each line and pushes it on to a writer stage. Then a stalled collector shows what the high-water marks and a send timeout do when the next stage can't keep up.Build an asynchronous server with DEALER and ROUTERYou will build a server on one ROUTER socket that answers several DEALER clients at once. Each client keeps a handful of requests in flight and matches the replies by request id as they come back out of order. Along the way you will see how the ROUTER names each client, what happens to a reply for a client that has already left and how to turn that into a visible error with ROUTER_MANDATORY.Stream messages over io_uring with PUSH/PULL and PUB/SUBYou will build two streams over TCP on localhost and run both on rzmq's io_uring backend. A PUSH/PULL pipeline carries 10,000 readings to a sink and a PUB socket streams price ticks to two subscribers. Along the way you pick the number of io_uring workers, switch TCP_CORK on and off and ask for zero-copy sends and multishot receives.Secure a request-reply exchange with CURVE and Noise_XXYou will take a small REQ/REP price lookup over TCP, generate keypairs for it with the rzmq CLI and turn on CURVE encryption. Then you run the same exchange over Noise_XX. Along the way you will point a client at the wrong server key and watch the handshake fail in the socket monitor.Tune the adaptive I/O throttle on a PUSH/PULL streamYou will stream 1,000 readings from a PUSH source to a PULL sink over TCP on localhost and read the adaptive throttle's state on both ends. Then you give the sink its own throttle settings, see what happens when they arrive too late and switch the throttle off on the source.