Skip to content
Mogens Heller Grabe edited this page Oct 22, 2019 · 4 revisions

Routing

RabbitMQ has native support for topic-based messaging, and Rebus will use that capability to implement pub/sub.

Therefore, the following configuration is enough for two Rebus instances to be able to communicate:

var subscriber = Configure.With(...)
	.Transport(t = t.UseRabbitMq(connectionString, "my-subscriber"))
	.Start();

var publisher = Configure.With(...)
	.Transport(t = t.UseRabbitMq(connectionString, "my-publisher"))
	.Start();

after which you may go

await subscriber.Subscribe<MyEvent>();

and then the subscriber will be subscribed to everything the publisher publishes like so:

await publisher.Publish(new MyEvent(...));

Since the subscriber will receive all types of events that it has subscribed to, it will need to be able to handle those events, so a slightly more full code example for the subscriber could look like this:

using (var activator = new BuiltInHandlerActivator())
{
	// add handler for MyEvent
	activator.Handle<MyEvent>(async message => {
		// handle event in here
	});

	// configure subscriber
	var subscriber = Configure.With(activator)
		.Transport(t = t.UseRabbitMq(connectionString, "my-subscriber"))
		.Start();

	await subscriber.Subscribe<MyEvent>();

	Console.WriteLine("Press ENTER to quit")
	Console.ReadLine();
}

Depending in your type of application, you will probably need to rearrange how BuiltInHandlerActivator is created and disposed.

Clone this wiki locally