nng-rs / Rust pattern guide

bus communication topology

BUS — Routing

BUS lets peers exchange messages in a simple mesh.

nng-rs is an independently maintained Rust binding for NNG. Its maintainers and release cycle are separate from the NNG core project.

This guide is based on the project’s default-branch example, with the peer list corrected so a node does not dial its own listener. See the upstream example for its current history.

bus.rs

//! Bus (routing) example.
//!
//! The bus protocol is useful for routing applications or for building fully interconnected mesh
//! networks. In this pattern, messages are sent to every directly connected peer.
//!
//! This example was derived from [this NNG example][1].
//!
//! [1]: https://nanomsg.org/gettingstarted/nng/bus.html
use nng::{Error, Protocol, Socket};
use std::{env, process, str, thread, time::Duration};

/// Entry point of the application
fn main() -> Result<(), Error> {
    let args: Vec<_> = env::args().collect();

    if args.len() < 3 {
        println!("Usage: bus <NODE_NAME> <URL> <URL> ...");
        process::exit(1);
    }

    node(&args[1], &args[2], &args[3..])
}

/// Bus node.
fn node(name: &str, listen: &str, dial: &[String]) -> Result<(), Error> {
    let s = Socket::new(Protocol::Bus0)?;
    s.listen(listen)?;

    // Give time for peers to bind.
    thread::sleep(Duration::from_secs(1));
    for peer in dial {
        s.dial(peer)?;
    }

    // SEND
    println!("{0}: SENDING \"{0}\" ONTO BUS", name);
    s.send(name.as_bytes())?;

    // RECV
    loop {
        let msg = s.recv()?;
        let peer = str::from_utf8(&msg).expect("invalid UTF-8");

        println!("{}: RECEIVED \"{}\" FROM BUS", name, peer);
    }
}
      
Run it

# Set up once.
git clone --recurse-submodules https://github.com/nanomsg/nng-rs.git
cd nng-rs

# Run these in separate terminals from the checkout.
cargo run -p nng --example bus -- node0 tcp://127.0.0.1:6000 tcp://127.0.0.1:6001
cargo run -p nng --example bus -- node1 tcp://127.0.0.1:6001 tcp://127.0.0.1:6000