Subscribe rss
Blog categories
All | Backendless features
Feature 72: Message selectors or conditional delivery in pub/sub messaging
March 21, 2015 by markpiller

Previously I wrote an introduction to Backendless pub/sub messaging which included a sample for broadcasting and receiving messages. Today I am going to show how to use Backendless messaging for conditional message delivery. As you will see in the example, the publisher can attach headers to the published message. The headers is a collection of arbitrary key/value pairs. A subscriber can specify a selector, which is an SQL query defining the condition a message must match in order to be delivered to that subscriber. Each subscriber for the same messaging channel can specify its own selector and Backendless will be handling all of the selectors individually.

In the example below, the publisher sends a message announcing a discount available at a retailer. The code randomly picks the retailer and the amount of the discount given. The name of the retailer, the merchandise and the amount of the discount are added as message headers:

Asynchronous sample (Android and Plain Java):

Synchronous sample (Plain Java only):

When you run the code, you will see output similar to this:

Suppose a subscriber would like to receive notifications only from the “Jeans-R-Us” retailer, for any discounts less than 50% and for the “Jacket” and “Shorts” merchandise. The question is how to express that condition in a selector. Using the SQL syntax makes it very simple:

Below is the source code listing for creating and registering a subscriber which uses that selector:

When you run the subscriber code, you will see that it receives only the messages which satisfy the selector criteria:


Share this post
Tweet about this on TwitterShare on FacebookGoogle+