Identify last subscriber for a branch

Product, Libraries & Version

IOCD 9.7

Goal

I’m looking for a way to detect when the last subscriber for a specific branch is gone and no one is listening to that branch anymore.

Is the simplest way to keep a count of subscribers?

When handling the subscriptionRemoved event, check if streamSubscription.stream.branch(key) returns undefined.
If it does, that means there are no more subscribers left on that branch. Something like:

function onSubscriptionRemoved(streamSubscription) {
  const key = streamSubscription.arguments.symbol;
  const branch = streamSubscription.stream.branch(key);   
  if (branch === undefined) {                              // last sub is gone
    //console.warn(`Branch is gone".`);
  }