Azure Service Bus: Filters and Actions for Subscriptions

Azure Service Bus allows the use of filters and actions on subscriptions to enable more sophisticated message handling and routing. This document explains the different types of filters available in Azure Service Bus, how to apply them, and how to receive messages based on these filters.
1. Introduction to Filters in Azure Service Bus
Filters in Azure Service Bus subscriptions allow you to specify criteria that messages must meet to be delivered to the subscription. By using filters, you can route specific messages to different subscriptions, enabling more efficient processing.
Types of Filters:
SQL Filters
Boolean Filters
Correlation Filters
2. SQL Filters
SQL Filters enable you to define conditions using a SQL-like syntax to evaluate message properties and system properties. Only messages that satisfy the condition will be delivered to the subscription.
Example:
const subscriptionRuleManager = serviceBusClient.getSubscriptionRuleManager(topicName, subscriptionName);
await subscriptionRuleManager.createRule("HighPriorityMessages", {
sqlExpression: "priority = 'high'"
});
In this example, only messages with the priority
property set to 'high' will be delivered to the subscription.
3. Boolean Filters
Boolean Filters are simpler and check whether a condition is true or false. The most common Boolean Filter is a TrueFilter
, which means all messages are accepted.
Example:
await subscriptionRuleManager.createRule("AllMessages", {
filter: true
});
This filter allows all messages to pass through to the subscription.
4. Correlation Filters
Correlation Filters are used to match messages with specific properties. This filter matches based on the message properties such as correlationId
, messageId
, contentType
, etc.
Example:
await subscriptionRuleManager.createRule("OrderFilter", {
correlationId: "order123"
});
In this example, only messages with a correlationId
of 'order123' will be delivered to the subscription.
5. SQL Filters and Actions
In addition to filtering messages, you can also apply actions to modify message properties as they are delivered to the subscription. Actions are defined in the SQL-like syntax and can perform tasks like setting or modifying properties.
Example:
await subscriptionRuleManager.createRule("AddProperty", {
sqlExpression: "priority = 'low'",
sqlAction: "SET messageType = 'lowPriority'"
});
This filter accepts messages with a priority
of 'low' and adds a new property messageType
with the value 'lowPriority'.
6. Receiving Messages from Subscriptions with Filters
To receive messages from a subscription that has filters applied, you can use the Service Bus receiver as usual. The filters work on the Service Bus side, and you receive only the messages that match the defined criteria.
Example:
const receiver = serviceBusClient.createReceiver(topicName, subscriptionName);
const messages = await receiver.receiveMessages(10);
for (const message of messages) {
console.log(`Received message: ${message.body}`);
await receiver.completeMessage(message);
}
In this example, receiver.receiveMessages
fetches messages that match the filter criteria defined for the subscription.
7. Practical Use Cases
SQL Filters: Useful when you need fine-grained control over which messages a subscription receives based on custom properties.
Boolean Filters: Suitable for scenarios where all messages need to be accepted or rejected based on a simple condition.
Correlation Filters: Ideal for routing messages that belong to specific transactions or sessions identified by correlation IDs.
Conclusion
Filters and actions in Azure Service Bus provide powerful mechanisms to route and manage messages effectively. By understanding and applying these filters, you can build more efficient, scalable, and maintainable messaging solutions. Whether it's SQL Filters for complex conditions or Correlation Filters for transaction-based routing, Azure Service Bus equips you with the tools needed for robust message processing.
Subscribe to my newsletter
Read articles from Muhammad Sufiyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Muhammad Sufiyan
Muhammad Sufiyan
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021. With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences. In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS. I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.