Receiving Filtered Messages¶
Backendless message filtering is a powerful mechanism enabling conditional message delivery, interest-based subscriptions and private (point-to-point) messaging. To enable filtering, a messaging listener registration must include a special condition called selector. Backendless applies the selector to every message published into a channel and if the condition is true, the message is delivered to the subscriber.
A selector is a query expressed in the SQL-92 syntax and formatted as the condition part of the SQL's WHERE clause. The condition references headers in the published messages. When a message is published and a subscriber has a selector, Backendless checks the condition of the selector against the headers of the published message. If the result of the condition is true, the message is delivered to the subscriber.
General signature¶
A general signature for receiving filtered messages as instances of a custom type:
// channel object must be obtained via a subscription call, for example:
// Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener<T>(void callback(T response), {String selector});
Filtering of String Messages¶
Adding a listener to receive filtered messages as string objects:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((String stringMessage) {
print("Received string message: $stringMessage");
}, selector: "importance = 'high'");
Filtering of Dictionary/Map messages¶
Adding a listener to receive filtered messages as dictionary/map objects:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((Map message) {
print("Received message as a map: $message");
}, selector: "importance = 'high' and customerId = 55561");
Filtering of custom type/class messages¶
Adding a listener to receive messages as instances of a custom type (the Person
class):
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((Person message) {
print("Received message as a Person object: $message");
}, selector: "areaOfInterest = 'music'");
Filtering of messages in Backendless internal format¶
Adding a listener to receive message in the Backendless internal format:
Channel channel = await Backendless.messaging.subscribe("MyChannel");
channel.addMessageListener((PublishMessageInfo message) {
print("Published message - ${message.message}");
print("Publisher ID - ${message.publisherId}");
print("Message headers - ${message.headers}");
print("Message subtopic ${message.subtopic}");
}, selector: "headerName1 > value and headerName2 = 'value'");