How to subscribe to multiple streams in RabbitMQ with single consumer? (C#)

550 views Asked by At

Is it possible to subscribe to receive messages from multiple streams with a single consumer in RabbitMQ?

I have the following code to subscribe to a single stream and this works correctly. Is it possible to have similar code to subscribe to multiple streams with single consumer, or are multiple consumer required?

using RabbitMQ.Stream.Client;
using RabbitMQ.Stream.Client.Reliable; 

var streamSystem = await StreamSystem.Create(
    new StreamSystemConfig()
    {
        UserName = "guest",
        Password = "guest",
        Endpoints = new List<EndPoint>
        {
            new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5552)
        }
    }
).ConfigureAwait(false);

var confirmationTaskCompletionSource = new TaskCompletionSource<int>();
var consumer = await Consumer.Create( // (1)
    new ConsumerConfig( // (2)
        streamSystem,
        "my-stream")
    {
        OffsetSpec = new OffsetTypeFirst(), // (3)
        MessageHandler = async (stream, consumer, context, message) => // (4)
        {
            Console.WriteLine($"Received message.");
            await Task.CompletedTask.ConfigureAwait(false);
        }
    }
).ConfigureAwait(false);
1

There are 1 answers

0
Gabriele Santomaggio On

The subscription is for a single stream. So, by default, you can't have more streams.

I'd suggest having a look at the super-stream feature. https://rabbitmq.github.io/rabbitmq-stream-dotnet-client/stable/htmlsingle/index.html#super-streams

Here is an example of how to use it: https://github.com/rabbitmq/rabbitmq-stream-dotnet-client/tree/main/docs/SuperStream