ably-realtime

Difference between ably realtime Channel.off method and Channel.unsubscribe


The Ably realtime javascript documentation here does not describe what the Channel.off method does. I'm wondering what that method does and how it's different than Channel.unsubscribe


Solution

  • Relevant bit of the docs is here. The Channel object is an EventEmitter, a pattern where an object can emit named events that you can add listeners for (which is a well-known pattern in javascript, eg see nodejs's built-in EventEmitter class docs here).

    In the case of a Channel object, it emits an event whenever the channel state changes. Channel.off() removes all state change listeners . (Or Channel.off(<state>) removes listeners for specific states, or Channel.off(<listener>)/Channel.off(<state>, <listener>) removes only a specific listener function).

    The difference from Channel.[un]subscribe(...) is that that adds [/removes] listeners for messages published on the channel, as opposed to channel state changes.

    The documentation for channels#off exists in the page source but appears to not be showing up on that page apparently due to a rendering bug.