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.RemoveMessageListener<T>( MessageReceived<T> listener );

where MessageReceived is declared as

public delegate void MessageReceived<T>( T message );

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.RemoveMessageListener<T>( string selector, MessageReceived<T> listener );

where MessageReceived is declared as

public delegate void MessageReceived<T>( T message );

Removing all message listeners for a selector

// channel is received as a result of the Subscribe() method
channel.RemoveMessageListeners<T>( string selector );

Remove all listeners for a channel

// channel is received as a result of the Subscribe() method
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
IChannel channel = Backendless.Messaging.Subscribe( "MyChannel" );

// declare a listener
MessageReceived<string> messageListener = ( message ) =>
{
  Console.WriteLine( $"received message {message}");
};

// add the listener to the channel
channel.AddMessageListener( messageListener );

// the following call removes the listener
channel.RemoveMessageListener( messageListener );