title: How to monitor number of RXJS subscriptions?
body: <p>I'm using an <code>Observable</code> to provide event subscription interface for clients from a global resource, and I need to manage that resource according to the number of active subscriptions:</p>
  
  <ul>
  <li>Allocate global resource when the number of subscriptions becomes greater than 0</li>
  <li>Release global resource when the number of subscriptions becomes 0</li>
  <li>Adjust the resource usage strategy based on the number of subscriptions</li>
  </ul>
  
  <p>What is the proper way in RXJS to monitor the number of active subscriptions?</p>
  
  <hr>
  
  <p>How to implement the following within RXJS syntax? - </p>
  
  <pre><code>const myEvent: Observable&lt;any&gt; = new Observable();
  
  myEvent.onSubscription((newCount: number, prevCount: number) =&gt; {
     if(newCount === 0) {
        // release global resource
     } else {
        // allocate global resource, if not yet allocated
     }
     // for a scalable resource usage / load,
     // re-configure it, based on newCount
  });
  </code></pre>
  
  <p>I wouldn't expect a guaranteed notification on each change, hence <code>newCount</code> + <code>prevCount</code> params.</p>
  
  <p><strong>UPDATE-1</strong></p>
  
  <p>This is not a duplicate to <a href="https://stackoverflow.com/questions/39315707/rxjs-number-of-observable-subscriptions">this</a>, because I need to be notified when the number of subscriptions changes, and not just to get the counter at some point.</p>
  
  <p><strong>UPDATE-2</strong></p>
  
  <p>Without any answer so far, I quickly came up with a <a href="https://gist.github.com/vitaly-t/fcaf95c51e2a3a8d160e41b9173854ae" rel="noreferrer">very ugly and limited work-around</a>, through complete incapsulation, and specifically for type <code>Subject</code>. Hoping very much to find a proper solution.</p>
  
  <p><strong>UPDATE-3</strong></p>
  
  <p>After a few answers, I'm still not sure how to implement what I'm trying, which is the following:</p>
  
  <pre><code>class CustomType {
  
  }
  
  class CountedObservable&lt;T&gt; extends Observable&lt;T&gt; {
  
      private message: string; // random property
  
      public onCount; // magical Observable that needs to be implemented
  
      constructor(message: string) {
          // super(); ???
          this.message = message;
      }
  
      // random method
      public getMessage() {
          return this.message;
      }
  }
  
  const a = new CountedObservable&lt;CustomType&gt;('hello'); // can create directly
  
  const msg = a.getMessage(); // can call methods
  
  a.subscribe((data: CustomType) =&gt; {
      // handle subscriptions here;
  });
  
  // need that magic onCount implemented, so I can do this:
  a.onCount.subscribe((newCount: number, prevCont: number) =&gt; {
      // manage some external resources
  });
  </code></pre>
  
  <p>How to implement such class <code>CountedObservable</code> above, which would let me subscribe to itself, as well as its <code>onCount</code> property to monitor the number of its clients/subscriptions?</p>
  
  <p><strong>UPDATE-4</strong></p>
  
  <p>All suggested solutions seemed overly complex, and even though I accepted one of the answers, I ended up with a <a href="https://github.com/vitaly-t/sub-events" rel="noreferrer">completely custom solution one of my own</a>.</p>