🔥 Checkout the Azure Developer page at azure.com/developer (opens new window).

💡 Learn more : about Azure Service Bus (opens new window).

📺 Watch the video : How to use Azure Service Bus Topics (opens new window).

# How to use Azure Service Bus Topics

# Use Azure Service Bus Topics for publish/subscribe

Modern applications are made up of many components and services. You might have a website, a mobile application, and an API that are being used to send data to your backend APIs. Sending this data directly to the APIs becomes a problem when the APIs are overwhelmed or offline.

Azure Service Bus Topics (opens new window) can help with this scenario by providing a publish/subscribe mechanism. Applications, like your website and mobile app, publish messages to Azure Service Bus. And receiving applications, like the backend APIs, subscribe to certain message topics, and process the messages that are meant for them. The APIs and the sending applications can be scaled independently, and don't depend on each other for availability.

In this post, we'll explore Azure Service Bus Topics by creating and using one in the Azure portal.

# Prerequisites

If you want to follow along, you'll need the following:

# Creating and using an Azure Service Bus Topic in the Azure portal

Let's create an Azure Service Bus. We'll use the Service Bus Explorer in the Azure portal to send messages to the queue.

  1. Go to the Azure portal (opens new window)
  2. Click the Create a resource button (the plus-sign in the top left corner)
  3. Search for service bus, select the "Service Bus" result and click Create
    1. This brings you to the Create namespace blade
    2. Select a Resource Group
    3. Fill in a Name for the service bus namespace
    4. Pick a Location
    5. Select a Pricing Tier. The Basic tier is fine for this demo
    6. Click Review + create and Create after that

(Create an Azure Service Bus Namespace in the Azure portal)

When the Service Bus Namespace is created, navigate to it in the Azure portal.

  1. In the Service Bus Namespace in the Azure portal, select the + Topic button in the top menu of the overview blade

(Create a Topic button in the Azure portal)

  1. Fill in the Create topic blade
    1. Fill in a Name for the topic
    2. Leave everything else as it is. Notice the settings for the advanced features like duplicate detection, auto-delete, and partitioning
    3. Click Create to create the topic

(Create a Topic blade in the Azure portal)

  1. Click on the topic to open it
  2. Messages are put on the topic and are read using subscriptions. Each subscription can receive certain messages and acts like a standalone queue. Click on the Subscription menu
  3. There are no subscriptions yet. Click on the + Subscription button in the top menu to create a subscription. This opens the Create subscription blade
    1. Fill in a Name for the subscription
    2. Type a Max delivery count, like 5. This tells the subscription that messages can be read 5 times before they are removed from the subscription queue
    3. Leave the rest of the settings as they are. These are settings that Azure Service Bus Queues also have
    4. Click Create to create the subscription

(Create a Subscription in the Azure portal)

  1. When the subscription is created, click on it
  2. The subscription needs one or more filters. Messages that match the filter will be routed to the subscription queue. Click on + Add filter
    1. Give the filter a Name
    2. You can create several types of filters. We'll use the Correlation filter
    3. Under System Properties, select label for Key, and type "video" in Value
    4. Click Save changes to add the filter. The subscription will now receive all messages that have a Label property with the value "post"

(Create a Filter in the Azure portal)

  1. Create another subscription and filter by repeating step 5 through 7 with different values for the names and for the filter value

Let's test the Service Bus Topic. Navigate to the Topic in the Azure portal.

  1. Select the Service Bus Explorer menu item
  2. First, we'll send a message to the topic
    1. Select Text/Plain for the Content Type. Messages for Azure Service Bus can also contain XML or Json content
    2. Type a text message in the text field
    3. Select Expand Advanced Properties. This allows you to edit advanced properties, like Label and Message ID. You can use these in your application to track or filter messages
    4. Type the value "video" in the Label property
    5. Click Send to put the message on the queue

(Send message to the Service Bus Topic)

  1. Scroll up to the menu of the Service Bus Explorer
  2. Message in the Service Bus Topic will stay there until they are deleted or expire. You can use the Peek function to take a look at a message, without deleting it. Click on the Peek menu
  3. Select the subscription that has the "video" filter
  4. You'll see that there is 1 message on the queue. Click Peek
  5. Select the message to see its details
  6. Navigate to the Receive menu
  7. Select the subscription that doesn't have the "video" filter. This doesn't have any messages in it. Switch back to the subscription that does have the "video" filter
  8. The message is still on the subscription queue. Click Receive to perform a ReceiveAndDelete action, which reads the message and deletes it from the subscription queue
  9. Select the message to read its content. You'll see that the value of the Label property is "video"

(Receive a message from the Service Bus Topic)

# Conclusion

You can use Azure Service Bus Topics (opens new window) to create a scalable publish/subscribe architecture. Azure Service Bus Topics comes with advanced features like first-in, first-out (FIFO) guarantee (opens new window), chaining Service Bus entities with autoforwarding (opens new window), and Scheduled messages (opens new window). Go and check it out!