Send and Receive in Different Tasks

If you need to split the different actions of your client across different tasks, you can do so like this:

// Copyright 2023 - Nym Technologies SA <>
// SPDX-License-Identifier: Apache-2.0

use futures::StreamExt;
use nym_sdk::mixnet;
use nym_sdk::mixnet::MixnetMessageSender;

async fn main() {

    // Passing no config makes the client fire up an ephemeral session and figure stuff out on its own
    let mut client = mixnet::MixnetClient::connect_new().await.unwrap();

    // Be able to get our client address
    let our_address = *client.nym_address();
    println!("Our client nym address is: {our_address}");

    let sender = client.split_sender();

    // receiving task
    let receiving_task_handle = tokio::spawn(async move {
        if let Some(received) = {
            println!("Received: {}", String::from_utf8_lossy(&received.message));


    // sending task
    let sending_task_handle = tokio::spawn(async move {
            .send_plain_message(our_address, "hello from a different task!")

    // wait for both tasks to be done
    println!("waiting for shutdown");