Skip to content

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:

//channel is received as a result of the subscribe() method
channel.removeMessageListeners({Function callback});

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:

//channel is received as a result of the subscribe() method
channel.removeMessageListeners({String selector, Function callback});

Removing all message listeners for a selector

//channel is received as a result of the subscribe() method
channel.removeMessageListeners({String 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
Channel channel = await Backendless.messaging.subscribe("MyChannel");

// declare a listener
Function(String) callback = (stringMessage) => print("got string message: $stringMessage");

// add the listener to the channel
await channel.addMessageListener(callback);

// the following call removes the listener
channel.removeMessageListeners(callback: callback);