Azure Service Bus: Topics and Subscriptions
Introduction to Topics
Azure Service Bus provides a robust messaging platform that supports advanced messaging patterns, including topics and subscriptions. Unlike queues, where each message is processed by a single consumer, topics enable a publish/subscribe model where messages can be sent to multiple subscribers.
Key Concepts:
Topics: Act as message distribution hubs. Messages sent to a topic are forwarded to one or more associated subscriptions.
Subscriptions: Represent logical receivers. Each subscription can receive a copy of each message sent to the topic, enabling multiple consumers to process the same message independently.
Creation of Topics
Creating topics in Azure Service Bus involves configuring a topic to act as a central message publisher. Here’s how you can create a topic:
Steps to Create a Topic:
Setup Azure Service Bus Namespace: Ensure you have an Azure Service Bus namespace in your Azure portal.
Create a Topic:
Navigate to your Service Bus namespace.
Select Topics from the left-hand menu.
Click + Topic and provide the necessary details such as the topic name, max size, and message time-to-live (TTL).
Click Create.
Sending Messages to Topics
Once a topic is created, you can send messages to it using an Azure Service Bus client in Node.js.
Example Code:
import { ServiceBusClient } from '@azure/service-bus';
const connectionString = "<Your Service Bus Connection String>";
const topicName = "<Your Topic Name>";
async function sendMessage() {
const client = new ServiceBusClient(connectionString);
const sender = client.createSender(topicName);
const message = {
body: "Hello World!",
subject: "Test Message"
};
await sender.sendMessages(message);
console.log(`Sent a message to topic: ${topicName}`);
await sender.close();
await client.close();
}
sendMessage().catch(console.error);
Sending Messages to Multiple Subscriptions
When you send a message to a topic, it is automatically delivered to all its subscriptions. Each subscription can have its own filters and rules to determine which messages it receives.
Example Code:
Create Subscriptions:
In the Azure portal, navigate to your topic.
Select Subscriptions and click + Subscription.
Provide a subscription name and other optional settings like TTL and rules.
Click Create.
Send Messages to Topic:
- The same
sendMessage
function above will deliver messages to all active subscriptions.
- The same
Receiving Messages from Multiple Subscriptions
Each subscription acts like an independent queue. You can receive messages from each subscription using a receiver.
Example Code:
async function receiveMessages(subscriptionName) {
const client = new ServiceBusClient(connectionString);
const receiver = client.createReceiver(topicName, subscriptionName);
const messages = await receiver.receiveMessages(10); // Receives up to 10 messages
for (const message of messages) {
console.log(`Received message: ${message.body}`);
await receiver.completeMessage(message); // Mark message as processed
}
await receiver.close();
await client.close();
}
receiveMessages("<Your Subscription Name>").catch(console.error);
Summary
Azure Service Bus Topics and Subscriptions offer a powerful publish/subscribe messaging model. Topics distribute messages to multiple subscriptions, enabling parallel processing by different subscribers. By following the steps to create topics, send messages, and receive messages from subscriptions, you can build scalable and flexible messaging systems in Azure.