LastTribunal
LastTribunal

Reputation: 1

Rebus: Combining Pub/Sub and Request/Reply patterns

I have a requirement to use one publisher with multiple subscribers where each subscriber only receives messages that have a unique ID for that subscriber. Each subscriber needs to be able to respond to the publisher.

Is it possible to do this with Rebus somehow?

Upvotes: 2

Views: 2659

Answers (1)

mookid8000
mookid8000

Reputation: 18628

You can do that with Rebus by using topic-based routing, which unfortunately has not been documented so much at the moment.

The most prominent pub/sub API in Rebus allows for subscribing to messages of a certain type

await bus.Subscribe<SomeMessage>();

but what it does underneath the covers is to establish a subscription for the topic SomeMessageAssembly.SomeMessage, SomeMessageAssembly (i.e. the simple assembly-qualified name of the type).

You have access to the topics via the bus.Advanced.Routing API though, so in your case your subscribers could just establish appropriate subscriptions for the topics they are interested in, and then the publisher would publish each event to an appropriate topic.

You can see the topic-based subscriptions in action in this sample. Please note though, that this sample happens to use a special feature of RabbitMQ that allows for wildcards in topics, but that is only supported with RabbitMQ at the moment.

I do have a question for you though: Are you sure that you want pub/sub for this? This sounds much more like request/reply to me. If each "event" is intended for one specific recipient, and the "publisher" expected a reply, I would call it request/reply and use explicit routing instead:

var destinationQueue = "queuename_of_specific_recipient";
var request = new SomeRequest { ... };

await bus.Advanced.Routing.Send(destinationQueue, request);

Upvotes: 3

Related Questions