NATS Logo by Example

Iterating Over Multiple Subscriptions in Messaging

Core NATS subscription support flexible subject model with tokens and wildcards, but there are cases that require setting up separate subscriptions (i.e. user want transport.cars, transport.planes and transport.ships, but not transport.spaceships).

Such approach works and is performant, but not very convenient when reading the messages. This example shows how to achieve required behaviour without sacrificing usability.

CLI Go Python JavaScript Rust C# Java Ruby Elixir Crystal C
Jump to the output or the recording
$ nbe run messaging/iterating-multiple-subscriptions/rust
View the source code or learn how to run this example yourself

Code

use std::{env, str::from_utf8};


use futures::stream::StreamExt;


#[tokio::main]
async fn main() -> Result<(), async_nats::Error> {

Use the NATS_URL env variable if defined, otherwise fallback to the default.

    let nats_url = env::var("NATS_URL").unwrap_or_else(|_| "nats://localhost:4222".to_string());


    let client = async_nats::connect(nats_url).await?;

Core NATS client.subscribe() can subscribe to more than one explicit subject using > and * wildcards, but not to two or more distinct subjects. However, it’s still possible to have just one stream of messages for such use case.

First, let’s subscribe to desired subjects.

    let subscription_cars = client.subscribe("cars.>").await?;
    let subscription_planes = client.subscribe("planes.>").await?;
    let subscription_ships = client.subscribe("ships.>").await?;

Then, spawn three tasks that publishes to each subject at the same time.

    tokio::task::spawn({
        let client = client.clone();
        async move {
            for i in 0..100 {
                client
                    .publish(format!("cars.{}", i), format!("car number {}", i).into())
                    .await?;
            }
            Ok::<(), async_nats::Error>(())
        }
    });
    tokio::task::spawn({
        let client = client.clone();
        async move {
            for i in 0..100 {
                client
                    .publish(format!("ships.{}", i), format!("ship number {}", i).into())
                    .await?;
            }
            Ok::<(), async_nats::Error>(())
        }
    });
    tokio::task::spawn({
        let client = client.clone();
        async move {
            for i in 0..100 {
                client
                    .publish(
                        format!("planes.{}", i),
                        format!("plane number {}", i).into(),
                    )
                    .await?;
            }
            Ok::<(), async_nats::Error>(())
        }
    });

Now, let’s create the stream that will receive messages from all subscriptions. We also leverage the take() combinator to limit number of messages we want to get in total.

If you need to get messages from at most two subscriptions, futures::stream::select can be used instead.

    let mut messages =
        futures::stream::select_all([subscription_cars, subscription_planes, subscription_ships])
            .take(300);

Receive the messages from both subjects.

    while let Some(message) = messages.next().await {
        println!(
            "received message on subject {} with paylaod {}",
            message.subject,
            from_utf8(&message.payload)?
        );
    }


    Ok(())
}

Output

received message on subject cars.0 with paylaod car number 0
received message on subject cars.1 with paylaod car number 1
received message on subject cars.2 with paylaod car number 2
received message on subject cars.3 with paylaod car number 3
received message on subject cars.4 with paylaod car number 4
received message on subject cars.5 with paylaod car number 5
received message on subject cars.6 with paylaod car number 6
received message on subject ships.0 with paylaod ship number 0
received message on subject cars.7 with paylaod car number 7
received message on subject ships.1 with paylaod ship number 1
received message on subject cars.8 with paylaod car number 8
received message on subject ships.2 with paylaod ship number 2
received message on subject cars.9 with paylaod car number 9
received message on subject ships.3 with paylaod ship number 3
received message on subject cars.10 with paylaod car number 10
received message on subject ships.4 with paylaod ship number 4
received message on subject cars.11 with paylaod car number 11
received message on subject ships.5 with paylaod ship number 5
received message on subject cars.12 with paylaod car number 12
received message on subject ships.6 with paylaod ship number 6
received message on subject ships.7 with paylaod ship number 7
received message on subject ships.8 with paylaod ship number 8
received message on subject cars.13 with paylaod car number 13
received message on subject ships.9 with paylaod ship number 9
received message on subject cars.14 with paylaod car number 14
received message on subject ships.10 with paylaod ship number 10
received message on subject ships.11 with paylaod ship number 11
received message on subject ships.12 with paylaod ship number 12
received message on subject cars.15 with paylaod car number 15
received message on subject ships.13 with paylaod ship number 13
received message on subject cars.16 with paylaod car number 16
received message on subject ships.14 with paylaod ship number 14
received message on subject ships.15 with paylaod ship number 15
received message on subject planes.0 with paylaod plane number 0
received message on subject cars.17 with paylaod car number 17
received message on subject ships.16 with paylaod ship number 16
received message on subject cars.18 with paylaod car number 18
received message on subject ships.17 with paylaod ship number 17
received message on subject planes.1 with paylaod plane number 1
received message on subject cars.19 with paylaod car number 19
received message on subject ships.18 with paylaod ship number 18
received message on subject planes.2 with paylaod plane number 2
received message on subject ships.19 with paylaod ship number 19
received message on subject cars.20 with paylaod car number 20
received message on subject planes.3 with paylaod plane number 3
received message on subject ships.20 with paylaod ship number 20
received message on subject planes.4 with paylaod plane number 4
received message on subject cars.21 with paylaod car number 21
received message on subject ships.21 with paylaod ship number 21
received message on subject planes.5 with paylaod plane number 5
received message on subject ships.22 with paylaod ship number 22
received message on subject planes.6 with paylaod plane number 6
received message on subject cars.22 with paylaod car number 22
received message on subject ships.23 with paylaod ship number 23
received message on subject planes.7 with paylaod plane number 7
received message on subject cars.23 with paylaod car number 23
received message on subject ships.24 with paylaod ship number 24
received message on subject planes.8 with paylaod plane number 8
received message on subject ships.25 with paylaod ship number 25
received message on subject planes.9 with paylaod plane number 9
received message on subject cars.24 with paylaod car number 24
received message on subject ships.26 with paylaod ship number 26
received message on subject planes.10 with paylaod plane number 10
received message on subject cars.25 with paylaod car number 25
received message on subject ships.27 with paylaod ship number 27
received message on subject planes.11 with paylaod plane number 11
received message on subject ships.28 with paylaod ship number 28
received message on subject cars.26 with paylaod car number 26
received message on subject planes.12 with paylaod plane number 12
received message on subject ships.29 with paylaod ship number 29
received message on subject cars.27 with paylaod car number 27
received message on subject planes.13 with paylaod plane number 13
received message on subject ships.30 with paylaod ship number 30
received message on subject cars.28 with paylaod car number 28
received message on subject planes.14 with paylaod plane number 14
received message on subject ships.31 with paylaod ship number 31
received message on subject cars.29 with paylaod car number 29
received message on subject planes.15 with paylaod plane number 15
received message on subject ships.32 with paylaod ship number 32
received message on subject cars.30 with paylaod car number 30
received message on subject planes.16 with paylaod plane number 16
received message on subject ships.33 with paylaod ship number 33
received message on subject cars.31 with paylaod car number 31
received message on subject planes.17 with paylaod plane number 17
received message on subject ships.34 with paylaod ship number 34
received message on subject cars.32 with paylaod car number 32
received message on subject planes.18 with paylaod plane number 18
received message on subject ships.35 with paylaod ship number 35
received message on subject planes.19 with paylaod plane number 19
received message on subject ships.36 with paylaod ship number 36
received message on subject planes.20 with paylaod plane number 20
received message on subject ships.37 with paylaod ship number 37
received message on subject planes.21 with paylaod plane number 21
received message on subject ships.38 with paylaod ship number 38
received message on subject ships.39 with paylaod ship number 39
received message on subject cars.33 with paylaod car number 33
received message on subject planes.22 with paylaod plane number 22
received message on subject planes.23 with paylaod plane number 23
received message on subject ships.40 with paylaod ship number 40
received message on subject cars.34 with paylaod car number 34
received message on subject planes.24 with paylaod plane number 24
received message on subject ships.41 with paylaod ship number 41
received message on subject cars.35 with paylaod car number 35
received message on subject ships.42 with paylaod ship number 42
received message on subject planes.25 with paylaod plane number 25
received message on subject ships.43 with paylaod ship number 43
received message on subject planes.26 with paylaod plane number 26
received message on subject cars.36 with paylaod car number 36
received message on subject ships.44 with paylaod ship number 44
received message on subject planes.27 with paylaod plane number 27
received message on subject cars.37 with paylaod car number 37
received message on subject ships.45 with paylaod ship number 45
received message on subject planes.28 with paylaod plane number 28
received message on subject ships.46 with paylaod ship number 46
received message on subject planes.29 with paylaod plane number 29
received message on subject cars.38 with paylaod car number 38
received message on subject ships.47 with paylaod ship number 47
received message on subject planes.30 with paylaod plane number 30
received message on subject cars.39 with paylaod car number 39
received message on subject ships.48 with paylaod ship number 48
received message on subject planes.31 with paylaod plane number 31
received message on subject ships.49 with paylaod ship number 49
received message on subject planes.32 with paylaod plane number 32
received message on subject cars.40 with paylaod car number 40
received message on subject ships.50 with paylaod ship number 50
received message on subject planes.33 with paylaod plane number 33
received message on subject ships.51 with paylaod ship number 51
received message on subject planes.34 with paylaod plane number 34
received message on subject ships.52 with paylaod ship number 52
received message on subject cars.41 with paylaod car number 41
received message on subject planes.35 with paylaod plane number 35
received message on subject ships.53 with paylaod ship number 53
received message on subject cars.42 with paylaod car number 42
received message on subject planes.36 with paylaod plane number 36
received message on subject ships.54 with paylaod ship number 54
received message on subject cars.43 with paylaod car number 43
received message on subject planes.37 with paylaod plane number 37
received message on subject ships.55 with paylaod ship number 55
received message on subject planes.38 with paylaod plane number 38
received message on subject cars.44 with paylaod car number 44
received message on subject planes.39 with paylaod plane number 39
received message on subject cars.45 with paylaod car number 45
received message on subject ships.56 with paylaod ship number 56
received message on subject planes.40 with paylaod plane number 40
received message on subject ships.57 with paylaod ship number 57
received message on subject planes.41 with paylaod plane number 41
received message on subject cars.46 with paylaod car number 46
received message on subject ships.58 with paylaod ship number 58
received message on subject planes.42 with paylaod plane number 42
received message on subject cars.47 with paylaod car number 47
received message on subject ships.59 with paylaod ship number 59
received message on subject planes.43 with paylaod plane number 43
received message on subject cars.48 with paylaod car number 48
received message on subject ships.60 with paylaod ship number 60
received message on subject planes.44 with paylaod plane number 44
received message on subject cars.49 with paylaod car number 49
received message on subject ships.61 with paylaod ship number 61
received message on subject planes.45 with paylaod plane number 45
received message on subject cars.50 with paylaod car number 50
received message on subject ships.62 with paylaod ship number 62
received message on subject planes.46 with paylaod plane number 46
received message on subject cars.51 with paylaod car number 51
received message on subject planes.47 with paylaod plane number 47
received message on subject cars.52 with paylaod car number 52
received message on subject ships.63 with paylaod ship number 63
received message on subject planes.48 with paylaod plane number 48
received message on subject cars.53 with paylaod car number 53
received message on subject ships.64 with paylaod ship number 64
received message on subject planes.49 with paylaod plane number 49
received message on subject cars.54 with paylaod car number 54
received message on subject ships.65 with paylaod ship number 65
received message on subject planes.50 with paylaod plane number 50
received message on subject cars.55 with paylaod car number 55
received message on subject ships.66 with paylaod ship number 66
received message on subject planes.51 with paylaod plane number 51
received message on subject cars.56 with paylaod car number 56
received message on subject ships.67 with paylaod ship number 67
received message on subject planes.52 with paylaod plane number 52
received message on subject cars.57 with paylaod car number 57
received message on subject ships.68 with paylaod ship number 68
received message on subject planes.53 with paylaod plane number 53
received message on subject cars.58 with paylaod car number 58
received message on subject ships.69 with paylaod ship number 69
received message on subject ships.70 with paylaod ship number 70
received message on subject planes.54 with paylaod plane number 54
received message on subject cars.59 with paylaod car number 59
received message on subject ships.71 with paylaod ship number 71
received message on subject ships.72 with paylaod ship number 72
received message on subject planes.55 with paylaod plane number 55
received message on subject cars.60 with paylaod car number 60
received message on subject ships.73 with paylaod ship number 73
received message on subject planes.56 with paylaod plane number 56
received message on subject cars.61 with paylaod car number 61
received message on subject ships.74 with paylaod ship number 74
received message on subject planes.57 with paylaod plane number 57
received message on subject cars.62 with paylaod car number 62
received message on subject ships.75 with paylaod ship number 75
received message on subject planes.58 with paylaod plane number 58
received message on subject cars.63 with paylaod car number 63
received message on subject planes.59 with paylaod plane number 59
received message on subject cars.64 with paylaod car number 64
received message on subject planes.60 with paylaod plane number 60
received message on subject ships.76 with paylaod ship number 76
received message on subject cars.65 with paylaod car number 65
received message on subject planes.61 with paylaod plane number 61
received message on subject ships.77 with paylaod ship number 77
received message on subject cars.66 with paylaod car number 66
received message on subject ships.78 with paylaod ship number 78
received message on subject planes.62 with paylaod plane number 62
received message on subject cars.67 with paylaod car number 67
received message on subject ships.79 with paylaod ship number 79
received message on subject cars.68 with paylaod car number 68
received message on subject planes.63 with paylaod plane number 63
received message on subject ships.80 with paylaod ship number 80
received message on subject planes.64 with paylaod plane number 64
received message on subject cars.69 with paylaod car number 69
received message on subject ships.81 with paylaod ship number 81
received message on subject planes.65 with paylaod plane number 65
received message on subject cars.70 with paylaod car number 70
received message on subject ships.82 with paylaod ship number 82
received message on subject planes.66 with paylaod plane number 66
received message on subject cars.71 with paylaod car number 71
received message on subject ships.83 with paylaod ship number 83
received message on subject planes.67 with paylaod plane number 67
received message on subject cars.72 with paylaod car number 72
received message on subject ships.84 with paylaod ship number 84
received message on subject planes.68 with paylaod plane number 68
received message on subject ships.85 with paylaod ship number 85
received message on subject cars.73 with paylaod car number 73
received message on subject planes.69 with paylaod plane number 69
received message on subject cars.74 with paylaod car number 74
received message on subject ships.86 with paylaod ship number 86
received message on subject planes.70 with paylaod plane number 70
received message on subject cars.75 with paylaod car number 75
received message on subject ships.87 with paylaod ship number 87
received message on subject planes.71 with paylaod plane number 71
received message on subject planes.72 with paylaod plane number 72
received message on subject cars.76 with paylaod car number 76
received message on subject planes.73 with paylaod plane number 73
received message on subject ships.88 with paylaod ship number 88
received message on subject planes.74 with paylaod plane number 74
received message on subject ships.89 with paylaod ship number 89
received message on subject cars.77 with paylaod car number 77
received message on subject ships.90 with paylaod ship number 90
received message on subject planes.75 with paylaod plane number 75
received message on subject cars.78 with paylaod car number 78
received message on subject ships.91 with paylaod ship number 91
received message on subject planes.76 with paylaod plane number 76
received message on subject cars.79 with paylaod car number 79
received message on subject ships.92 with paylaod ship number 92
received message on subject planes.77 with paylaod plane number 77
received message on subject cars.80 with paylaod car number 80
received message on subject ships.93 with paylaod ship number 93
received message on subject planes.78 with paylaod plane number 78
received message on subject cars.81 with paylaod car number 81
received message on subject ships.94 with paylaod ship number 94
received message on subject planes.79 with paylaod plane number 79
received message on subject cars.82 with paylaod car number 82
received message on subject ships.95 with paylaod ship number 95
received message on subject planes.80 with paylaod plane number 80
received message on subject cars.83 with paylaod car number 83
received message on subject planes.81 with paylaod plane number 81
received message on subject planes.82 with paylaod plane number 82
received message on subject planes.83 with paylaod plane number 83
received message on subject planes.84 with paylaod plane number 84
received message on subject cars.84 with paylaod car number 84
received message on subject planes.85 with paylaod plane number 85
received message on subject planes.86 with paylaod plane number 86
received message on subject cars.85 with paylaod car number 85
received message on subject ships.96 with paylaod ship number 96
received message on subject planes.87 with paylaod plane number 87
received message on subject ships.97 with paylaod ship number 97
received message on subject cars.86 with paylaod car number 86
received message on subject planes.88 with paylaod plane number 88
received message on subject planes.89 with paylaod plane number 89
received message on subject ships.98 with paylaod ship number 98
received message on subject ships.99 with paylaod ship number 99
received message on subject cars.87 with paylaod car number 87
received message on subject cars.88 with paylaod car number 88
received message on subject cars.89 with paylaod car number 89
received message on subject cars.90 with paylaod car number 90
received message on subject planes.90 with paylaod plane number 90
received message on subject cars.91 with paylaod car number 91
received message on subject cars.92 with paylaod car number 92
received message on subject planes.91 with paylaod plane number 91
received message on subject planes.92 with paylaod plane number 92
received message on subject planes.93 with paylaod plane number 93
received message on subject planes.94 with paylaod plane number 94
received message on subject planes.95 with paylaod plane number 95
received message on subject planes.96 with paylaod plane number 96
received message on subject cars.93 with paylaod car number 93
received message on subject cars.94 with paylaod car number 94
received message on subject planes.97 with paylaod plane number 97
received message on subject cars.95 with paylaod car number 95
received message on subject cars.96 with paylaod car number 96
received message on subject planes.98 with paylaod plane number 98
received message on subject cars.97 with paylaod car number 97
received message on subject planes.99 with paylaod plane number 99
received message on subject cars.98 with paylaod car number 98
received message on subject cars.99 with paylaod car number 99

Recording

Note, playback is half speed to make it a bit easier to follow.