Multicasting in RxPHP
In Reactive Extensions, multicasting means sharing a single subscription among multiple observers via an instance of a Subject
class. All multicasting operators are internally based on the general multicast()
operator that implements their most common functionality. Of course, we're not limited to only using the Subject
class and we'll use ReplaySubject
and BehaviorSubject
as well.
Multicasting is common to all Rx implementations, so knowledge of how it works inside is generally useful.
The multicast() operator and ConnectableObservable
The multicast()
operator returns ConnectableObservable
or MulticastObservable
based on what arguments we pass. We'll first have a look at how it works with ConnectableObservable
, because this should be very familiar to us.
A typical use case could look like the following example:
// multicast_01.php $observable = Rx\Observable::defer(function() { printf("Observable::defer\n"); return Observable::range(1...