RxJS’s Subject Class

A Subject is a type that implements both Observer and Observable types. As an Observer, it can subscribe to Observables, and as an Observable it can produce values and have Observers subscribe to it.

In some scenarios a single Subject can do the work of a combination of Observers and Observables. For example, for making a proxy object between a data source and the Subject’s listeners, we could use this:

 var​ subject = ​new​ Rx.Subject();
 var​ source = Rx.Observable.interval(300)
  .map(​function​(v) { ​return​ ​'Interval message #'​ + v; })
 var​ subscription = subject.subscribe(
 function​ onNext(x) { console.log(​'onNext: '​ + x); },

Get Reactive Programming with RxJS now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.