Skip to content

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:

Backendless.Messaging.Subscribe( "MyChannel" )
                     .AddMessageListener<T>( string selector, 
                                                MessageReceived<T> callback );

where MessageReceived is declared as:

public delegate void MessageReceived<T>( T message );

Filtering of String Messages

Adding a listener to receive filtered messages as string objects:

string selector = "importance = 'high'";
Backendless.Messaging.Subscribe( "MyChannel" )
                     .AddMessageListener<String>( selector,
                                                     message => 
  {
    System.Console.WriteLine( $"message received {message}");
  }
);

Filtering of Dictionary/Map messages

Adding a listener to receive filtered messages as dictionary/map objects:

string selector = "importance = 'high' and customerId = 55561";
Backendless.Messaging.Subscribe()
                     .AddMessageListener<Dictionary<string, object>>( selector,
                                                                            message =>
{
  Console.WriteLine( "Received message: ");

  foreach( KeyValuePair<string, object> kvp in message )
    Console.WriteLine( $"Key = {kvp.Key}, Value = {kvp.Value}" );
});

Filtering of custom type/class messages

Adding a listener to receive messages as instances of a custom type (the Person class):

String selector = "areaOfInterest = 'music'";
Backendless.Messaging.Subscribe()
                     .AddMessageListener<Person>( selector,
                                                     message =>
{
  Console.WriteLine( $"Received message: person's ame: {message.name}, person's age: {message.age}");
});