Developers
Mixnet Module

Mixnet Module

The mixnet module is the core of the Nym SDK. It provides MixnetClient (opens in a new tab) for connecting to the Nym Mixnet, sending messages through Sphinx packet encryption and 5-hop routing, and receiving reconstructed messages on the other side.

⚠️

Messages are individually routed through the Mixnet with no guaranteed ordering or persistent connections. If you want familiar socket-like I/O (read/write), use the Stream module instead. See the Tour for how the two approaches compare.

Two operating modes

The client operates in one of two mutually exclusive modes:

Message mode (default): send and receive raw message payloads:

use nym_sdk::mixnet::{self, MixnetMessageSender};
 
let mut client = mixnet::MixnetClient::connect_new().await.unwrap();
 
// Send a message
client.send_plain_message(*client.nym_address(), "hello").await.unwrap();
 
// Receive messages
if let Some(msgs) = client.wait_for_messages().await {
    for msg in msgs {
        println!("Got: {}", String::from_utf8_lossy(&msg.message));
    }
}
 
client.disconnect().await;

Stream mode: persistent AsyncRead + AsyncWrite channels. See the Stream module for details.

Stream mode is activated by calling open_stream() or listener(). Once active, message-mode methods return Error::StreamModeActive. This is a one-way transition.

API reference

Run any example with:

cargo run --example <example_name>

Next steps