rustzmq2 0.1.0

A native async Rust implementation of ZeroMQ
Documentation
// 09_pair — PAIR socket: exclusive one-to-one bidirectional channel.
//
// PAIR is the simplest socket type: exactly one peer, full duplex.
// Use it for tightly-coupled thread or process pairs.
// For multi-peer patterns use DEALER/ROUTER instead.
//
// This example spawns both sides in the same process using tokio tasks.
use rustzmq2::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut server = rustzmq2::PairSocket::new();
    server.bind("tcp://127.0.0.1:5580").await?;

    let client_task = tokio::spawn(async {
        let mut client = rustzmq2::PairSocket::new();
        client.connect("tcp://127.0.0.1:5580").await.unwrap();

        for i in 0..5u32 {
            let msg = format!("ping {i}");
            client.send(msg).await.unwrap();
            let reply: String = client.recv().await.unwrap().try_into().unwrap();
            println!("client got: {reply}");
        }
    });

    // Server: echo each message back with "pong".
    for _ in 0..5 {
        let msg: String = server.recv().await?.try_into()?;
        println!("server got: {msg}");
        server.send(msg.replace("ping", "pong")).await?;
    }

    client_task.await?;
    Ok(())
}