Removing Listeners¶
To stop receiving messages listeners can be removed using the following API:
Removing a specific listener object¶
The object must be the same as the one added with the method which adds the listener:
function onMessage( message ) {
}
//channel is received as a result of the subscribe() method
channel.removeMessageListener( onMessage );
Removing a specific listener for a selector¶
The listener object must be the same as the one added with the method which adds the listener:
var selector = "your selector value";
function onMessage( message ) {
}
//channel is received as a result of the subscribe() method
channel.removeMessageListener( selector, onMessage );
Removing all message listeners for a selector¶
var selector = "your selector value";
//channel is received as a result of the subscribe() method
channel.removeMessageListeners( selector );
Remove all listeners for a channel¶
channel.removeAllMessageListeners();
Example¶
The following example declares a messaging listener, adds and then removes it. The example as shown does not provide any subscription functionality as the listener is removed right after it is added, however, it clearly demonstrates how a listener can be removed from a channel:
// subscribe to a channel
var channel = Backendless.Messaging.subscribe( "MyChannel" );
// declare a listener
function onMessage( message ) {
}
// add the listener to the channel
channel.addMessageListener( onMessage );
// the following call removes the listener
channel.removeMessageListener( onMessage );