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:

- (RTSubscription *)addStringMessageListenerWithSelector:(NSString * _Nonnull)selector responseHandler:^(NSString * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

- (RTSubscription *)addDictionaryMessageListenerWithSelector:(NSString * _Nonnull)selector responseHandler:^(NSDictionary<NSString *,id> * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

- (RTSubscription *)addCustomObjectMessageListenerForClass:(Class  _Nonnull __unsafe_unretained)customClass selector:(NSString * _Nonnull)selector responseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

- (RTSubscription *)addMessageListenerWithSelector:(NSString * _Nonnull)selector responseHandler:^(PublishMessageInfo * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func addStringMessageListener(selector: String, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

func addDictionaryMessageListener(selector: String, responseHandler: (([String : Any]) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

func addCustomObjectMessageListener(forClass: AnyClass, selector: String, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription? 

func addMessageListener(selector: String, responseHandler: ((PublishMessageInfo) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

Filtering of String Messages

Adding a listener to receive filtered messages as string objects:

Channel *channel = [Backendless.shared.messaging subscribeWithChannelName:@"MyChannel"];
RTSubscription *subscrpiption = [channel addStringMessageListenerWithSelector:@"importance = 'high'" responseHandler:^(NSString *message) {
    NSLog(@"Received string message: %@", message);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let channel = Backendless.shared.messaging.subscribe(channelName: "MyChannel")
let _ = channel.addStringMessageListener(selector: "importance = 'high'", responseHandler: { message in
    print("Received string message: \(message)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Filtering of Dictionary/Map messages

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

Channel *channel = [Backendless.shared.messaging subscribeWithChannelName:@"MyChannel"];
RTSubscription *subscription = [channel addDictionaryMessageListenerWithSelector:@"importance = 'high'" responseHandler:^(NSDictionary *message) {
    NSLog(@"Received message as a dictionary: %@", message);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let channel = Backendless.shared.messaging.subscribe(channelName: "MyChannel")
let _ = channel.addDictionaryMessageListener(selector: "importance = 'high'", responseHandler: { message in
    print("Received message as a dictionary: \(message)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Filtering of custom type/class messages

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

Channel *channel = [Backendless.shared.messaging subscribeWithChannelName:@"MyChannel"];
RTSubscription *subscription = [channel addCustomObjectMessageListenerForClass:[Person class] selector:@"areaOfInterest = 'music'" responseHandler:^(Person *message) {
    NSLog(@"Received message as a custom object: %@", message);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let channel = Backendless.shared.messaging.subscribe(channelName: "MyChannel")
let _ = channel.addCustomObjectMessageListener(forClass: Person.self, selector: "areaOfInterest = 'music'", responseHandler: { message in
    print("Received message as a custom object: \(message)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Filtering of messages in Backendless internal format

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

Channel *channel = [Backendless.shared.messaging subscribeWithChannelName:@"MyChannel"];
RTSubscription *subscription = [channel addMessageListenerWithSelector:@"headerName1 > value and headerName2 = 'value'" responseHandler:^(PublishMessageInfo *message) {
    NSLog(@"Published message - %@", message.message);
    NSLog(@"Publisher ID - %@", message.publisherId);
    NSLog(@"Message headers - %@", message.headers);
    NSLog(@"Message subtopic - %@", message.subtopic);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let channel = Backendless.shared.messaging.subscribe(channelName: "MyChannel")
let _ = channel.addMessageListener(selector: "headerName1 > value and headerName2 = 'value'", responseHandler: { message in
    print("Published message - \(message.message ?? NSNull())")
    print("Publisher ID - \(message.publisherId ?? "")")
    print("Message headers - \(message.headers ?? [String : Any]())")
    print("Message subtopic - \(message.subtopic ?? "")")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})