Search code examples
javascriptangulargetstream-io

GetStream.io Chat API - Stop Listening for channel Events in Stream


I'm confused by the documentation on how to stop listening for channel events in JS using the Stream Chat API. As per the docs:

// remove the handler for all "message.new" events on the channel
channel.off("message.new", myChannelEventHandler);

What is myChannelEventHandler?

My channel.on looks like this, from the example Angular application:

this.channel.on('message.new', event => {
    this.messages = [...this.messages, event.message];
});

This is the only time myChannelEventHandler ever referenced in the docs, any guidance would be appreciated.


Solution

  • Thanks to Taplar, I've gotten this working. Solution is as simple as this:

    Define a message event handler, and create a property for the Angular component class:

    messageEventHandler = event => {
        this.messages = [...this.messages, event.message];
    };
    

    Use event handler for channel on:

    this.channel.on('message.new', this.messageEventHandler)
    

    Use the same event handler for channel off:

    this.channel.off('message.new', this.messageEventHandler)