Azure Service Bus - Topic

This articles explains about Azure Service Bus Topic and Subscription which is commonly known as pub-sub with a real world scenario.

Business Requirement

The business requirement is in my previous article,

So far we have seen about mobile recharge where we have used queuing mechanism. Now a company wants to provide some offers on regular recharge. But how will registered users come to know about these offers?

So again the company has started looking for some solution and finally it has come up with the solution of Azure Service Bus - Topic. Topic works like pub-sub model where one is the publisher and there can be many subscribers for the same publisher. In our scenario the publisher will publish offers and subscribed users will get the offers immediately.

Azure Service Bus Topic And Subscription (Pub-Sub)

Now let's see the step by step implementation of the below to design a solution:

We can see that Topic is created in it is shown under topics list.

Azure Service Bus Topic And Subscription (Pub-Sub)

We can see both the subscriptions in the list.

Azure Service Bus Topic And Subscription (Pub-Sub)

Credentials

Click on 'Shared access policies' from the left panel, click on 'RootManageSharedAccessKey' to explore keys and connection strings which will be used further to connect.

Azure Service Bus Topic And Subscription (Pub-Sub)

Publish Message

We can see that published message is added in all available subscriptions.

Azure Service Bus Topic And Subscription (Pub-Sub)

Read message from subscription

Now if you closely observe message count for all available subscriptions, one of the subscriptions has a zero message count as we have already read from that subscription and another subscription shows 1 as a message count means we need to publish once and it will be available for all subscriptions and whom so ever is read that only will be deleted and for rest it will be as it is.

Azure Service Bus Topic And Subscription (Pub-Sub)

Azure service bus topic is very similar to Azure service bus queue, the difference is queue works on one to one; i.e. one sender and one receiver whereas topic works on one to many; i.e. One publisher and many subscribers.

Note
Download complete sample code from here.