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:

// channel object must be obtained via a subscription call, for example:
// var channel = Backendless.Messaging.subscribe( "MyChannel" );
channel.addMessageListener( String selector, AsyncCallback<T>, Class<T> callback );

Filtering of String Messages

Adding a listener to receive filtered messages as string objects:

Channel channel = Backendless.Messaging.subscribe( "MyChannel" );

String selector = "importance = 'high'";
channel.addMessageListener( selector, new AsyncCallback<String>()
{
  @Override
  public void handleResponse( String stringMessage )
  {
     Log.i( "MYAPP", "Received string message " + stringMessage );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
     Log.e( "MYAPP", "Error processing a message " + fault );
  }
} );

Filtering of Dictionary/Map messages

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

Channel channel = Backendless.Messaging.subscribe( "MyChannel" );

String selector = "importance = 'high' and customerId = 55561";
channel.addMessageListener( selector, new AsyncCallback<HashMap>()
{
  @Override
  public void handleResponse( HashMap message )
  {
    Log.i( "received message as a hashmap " + message );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "Error processing a message " + fault );
  }
}, HashMap.class );

Filtering of custom type/class messages

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

Channel channel = Backendless.Messaging.subscribe( "MyChannel" );

String selector = "areaOfInterest = 'music'";
channel.addMessageListener( selector, new AsyncCallback<Person>()
{
  @Override
  public void handleResponse( Person personObject )
  {
    Log.i( "received message as a custom class " + personObject );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "Error processing a message " + fault );
  }
}, Person.class );

Filtering of messages in Backendless internal format

Adding a listener to receive message in the Backendless internal format:

String selector = "headerName1 > value and headerName2 = 'value'";
channel.addMessageListener( selector, new MessageInfoCallback()
{
  @Override
  public void handleResponse( PublishMessageInfo message )
  {
    Log.i( "MYAPP", "Published message - " + message.getMessage() );
    Log.i( "MYAPP", "Publisher ID - " + message.getPublisherId() );
    Log.i( "MYAPP", "Message headers - " + message.getHeaders() );
    Log.i( "MYAPP", "Message subtopic " + message.getSubtopic() );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Error processing a message " + fault );
  }
} );