Search code examples
getstream-io

How to remove a listener


I set the listener when a new message arrives on the channel

conversation.on('message.new', async (event) => {
// code
});

When removing a participant from a channel, I need the message.new listener to be deleted. I call off() for listener.

conversation.on('member.removed', async (event) => {
   conversation.off('message.new', async (event) => {});
});

but the first listener is still working.


Solution

  • You need to save your listener to remove it later because it will be checked for equality. In your snippet, you're passing a new function so it's not removed.

    const yourHandler = async (event) => {
      // code
    }
    
    // later
    
    conversation.off(eventType, yourHandler);