Skip to content

Topic Based Routing

Pete Smith edited this page Jul 8, 2014 · 19 revisions

AzureNetQ provides a feature called topic based routing. The name comes from our parent project EasyNetQ, and and has nothing to do with the concept of 'Topics' as used by Azure Service Bus. See the section Casing in point: Topics and topics, Subscriptions and subscriptions for more details.

This feature allows a subscriber to filter messages based on multiple criteria. A topic is a list of words delimited by dots that are published along with the message. Examples would be, "stock.usd.nyse" or "book.uk.london" or "a.b.c", the words can be anything you like, but would usually be some attributes of the message.

To publish with a topic, simply use the overloaded Publish method with a topic:

bus.Publish(message, "X.A");

Subscriptions and Wildcards

Subscribers can filter messages by specifying a topic to match to. These can include the wildcard characters:

* (star) or # (hash) or % (percent)

All three match zero or more characters. Please note that the wildcard * behaves differently from it's counterpart in EasyNetQ. It is kept in use purely for ease of migration.

So a message that is published with the topic "X.A.2" would match "#", "X.#", "*.A.*" but not "X.B.*" or "A". To subscribe with a topic, use the overloaded Subscribe method with configuration:

bus.Subscribe(handler, x => x.WithTopic("X.*"));

Relationship between Subscriptions and topics

Two subscribers with the same Subscription name but different topic strings are created as separate Subscriptions by the broker. If you want to create a single Subscription that matches on multiple topics ("X.*" OR "*.B") you can use another overload of the Subscribe method that takes multiple topics, like this:

bus.Subscribe(handler, x => x.WithTopic("X.*").WithTopic("*.B"));

There are topic overloads for the SubscribeAsync method that work in exactly the same way. Subscribers with the exact same subscription name and topics combination will be treated as competing consumers just like regular publish/subscribe usage.