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" );
// var selector = "define the condition here"
channel.addMessageListener( selector, function( message ) { } );

Filtering of String Messages

Adding a listener to receive filtered messages as string objects:

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

function onMessage( stringMessage ) {
  console.log( "Message received: " + stringMessage.data )
}

var selector = "importance = 'high'"; 
channel.addMessageListener( selector, onMessage );

Filtering of Dictionary/Map messages

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

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

function onMessage( message ) {
  console.log( "Message received" );
  console.log( "importance header " + message.headers.importance )
  console.log( "customerId header " + message.headers.customerId );
  console.log( "Property x " + message.data.x )
  console.log( "Property y " + message.data.y )
}

var selector = "importance = 'high' and customerId = 55561"; 
channel.addMessageListener( selector, onMessage );

Filtering of custom type/class messages

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

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

function onMessage( message ) {
  console.log( "Message received" );
  console.log( areaOfInterest header " + message.headers.areaOfInterest );
  console.log( "Property name " + message.data.name )
  console.log( "Property age " + message.data.age )
}

var selector = "areaOfInterest = 'music'";
channel.addMessageListener( selector, onMessage );