Expand description
Native async Rust implementation of ZeroMQ.
The ZeroMQ guide is the canonical reference for the messaging patterns below; this crate’s API mirrors libzmq’s so chapters of the guide apply directly.
Wire protocol: ZMTP 3.1.
§Quick start
[dependencies]
rustzmq2 = "0.1"
tokio = { version = "1", features = ["full"] }use rustzmq2::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rep = rustzmq2::RepSocket::new();
rep.bind("tcp://127.0.0.1:5555").await?;
let msg: String = rep.recv().await?.try_into()?;
rep.send(format!("{msg} world")).await?;
Ok(())
}§Socket types
| Pattern | Types |
|---|---|
| Request / Reply | ReqSocket, RepSocket, DealerSocket, RouterSocket |
| Publish / Subscribe | PubSocket, SubSocket, XPubSocket, XSubSocket |
| Pipeline | PushSocket, PullSocket |
| Peer-to-peer | PairSocket |
| Scatter / Gather | ScatterSocket, GatherSocket |
| Channel | ChannelSocket |
All socket types implement Socket (for bind / connect) and either
SocketSend, SocketRecv, or both. Import them via prelude:
use rustzmq2::prelude::*;§Configuration
Use SocketBuilder (via SocketType::builder()) to configure security,
heartbeats, and peer identity before creating a socket:
use rustzmq2::{RepSocket, Socket};
let mut rep = RepSocket::builder()
.plain_server()
.zap_domain("myapp")
.build();§Security
The full ZMTP security stack is supported: NULL (default), PLAIN, CURVE,
and ZAP. See SocketBuilder for configuration methods and
set_zap_handler / clear_zap_handler for ZAP.
Modules§
- family
- Sealed role-marker traits that gate kind-specific builder methods. Used only in trait bounds; you should never need to name these directly. Sealed role markers that gate kind-specific builder methods at the type level.
- prelude
- Convenience re-exports.
use rustzmq2::prelude::*;brings in the traits and types needed for nearly every program.
Structs§
- Channel
Socket - Channel socket (CHANNEL). Bidirectional point-to-point channel.
- Dealer
Recv Half - Recv half of a
DealerSocketproduced byDealerSocket::split. - Dealer
Send Half - Send half of a
DealerSocketproduced byDealerSocket::split. - Dealer
Socket - Dealer socket (DEALER). Async request socket without the strict send/recv alternation of REQ.
- Gather
Socket - Gather socket (GATHER). Receives messages fair-queued from connected SCATTER peers.
- Pair
Socket - Pair socket (PAIR). Exclusive one-to-one bidirectional channel.
- Peer
Identity - Per-peer identity (up to 255 bytes). Equivalent to libzmq’s
ZMQ_ROUTING_ID— used to address a specific peer through a ROUTER socket and surfaced inSocketEvents. - PubSocket
- Publish socket (PUB). Broadcasts every message to all matching subscribers.
- Pull
Socket - Pull socket (PULL). Receives messages from any connected PUSH peer, fair-queued.
- Push
Socket - Push socket (PUSH). Distributes messages round-robin to connected PULL peers.
- Reconnect
Stop - Conditions under which the reconnect task gives up rather than
retrying. Mirrors libzmq’s
ZMQ_RECONNECT_STOPflag set. - RepSocket
- Reply socket (REP). Receives a request, then must send exactly one reply.
- ReqSocket
- Request socket (REQ). Sends one message and waits for a reply before sending again.
- Router
Envelope - A message received from a
RouterSocket: the originating peer’s identity plus its payload, with the envelope already split apart. - Router
Recv Half - The recv half of a
RouterSocketproduced byRouterSocket::split. - Router
Send Half - The send half of a
RouterSocketproduced byRouterSocket::split. - Router
Socket - Router socket (ROUTER). Async reply socket that tracks each peer’s identity so replies can be addressed by identity.
- Scatter
Socket - Scatter socket (SCATTER). Distributes messages round-robin to connected GATHER peers.
- Socket
Builder - Fluent builder for any socket type.
- Socket
Options - Per-socket configuration passed to
Socket::with_options. - SubSocket
- Subscribe socket (SUB). Receives messages from publishers matching subscribed prefixes.
- XPub
Socket - Extended publish socket (XPUB). Like PUB, but surfaces subscribe/unsubscribe events via
recv. - XSub
Socket - Extended subscribe socket (XSUB). Raw counterpart of
SubSocket. - ZapRequest
- A request sent to the ZAP handler for authentication.
- ZapResponse
- Response from the ZAP handler.
- ZmqEmpty
Message Error - Error returned when attempting to construct a
ZmqMessagewith no frames. - ZmqMessage
- A multi-frame ZMQ message.
Enums§
- Codec
Error - Represents an error when encoding/decoding raw byte buffers and frames
- Endpoint
- Represents a ZMQ Endpoint.
- Endpoint
Error - Represents an error when parsing an
crate::Endpoint - Host
- Represents a host address. Does not include the port, and may be either an ip address or a domain name
- Join
Error - The error type returned by awaiting a [
crate::async_rt::task::JoinHandle]. - Message
Conversion Error - Error returned when converting a
ZmqMessageinto a single-frame representation (Vec<u8>/String/Bytes) fails. - Socket
Event - Event emitted by the socket monitor channel (see
Socket::monitor). - Socket
Type - Identifies the ZMQ socket pattern. Used internally and in
SocketEvents. - Task
Error - Transport
- The type of transport used by a given endpoint
- XPub
Event - A subscription event surfaced by
XPubSocket::recv_event: a subscriber is asking for a topic to be added or removed. - ZmqError
Traits§
- Capture
Socket - Marker trait expressing the fact that only certain types of sockets
may be used in the
proxyfunction as a capture parameter. - Multi
Peer Backend - Backend trait for sockets that manage multiple peer connections.
- Socket
- Core trait implemented by every ZMQ socket type.
- Socket
Backend - Low-level interface implemented by every socket backend.
- Socket
Recv - Receive half of a ZMQ socket.
- Socket
Send - Send half of a ZMQ socket.
Functions§
- clear_
zap_ handler - Remove the global ZAP handler. After this call, all connections are permitted without authentication (permissive default).
- proxy
- Bidirectional proxy between a frontend and backend socket.
- proxy_
with_ capture - Bidirectional proxy that also forwards every message to a capture socket before relaying — useful for monitoring or logging traffic.
- set_
zap_ handler - Register a ZAP authentication handler for all connections.