Why do we call RxJS Subject as Multicast and Observable as Unicast?
Asked Answered
C

2

16

I'm going through RxJS from sometime and i can't seem to understand why do we call RxJS Subject as Multicast and Observable as Unicast.

I understood that Subject is capable of emitting data using next()and like plain Observable, we can subscribe to them as well.

I found this from RxJS official website

A Subject is like an Observable, but can multicast to many Observers. 

Is this mean like a plain Observable cannot have multiple Observers?

Cortney answered 13/7, 2019 at 15:48 Comment(3)
Yes. A Subject emits the same event to many subscribers. A cold observable generates a new stream of events for every subscriber. rxjs-dev.firebaseapp.com/guide/subjectCarmarthenshire
A subject can also be returned as an Observable with the asObservable method, you then get a multicast observable reflecting the Subject, but without next exposed for external use. So yes an observable can multicast. Both subjects and observables can have multiple observers, but multicast can emit multiple times to its observers, while unicast will emit once to its observers.Ssw
Also note that you can make a unicast observable multicast, just using operators like share(), shareReplay() and others.Brodench
M
27

Consider this code:

const myObservable = new Observable<number>(subscriber => {
    const rdn = Math.floor(Math.random() * 200) + 1;
    subscriber.next(rdn);
});
myObservable
    .subscribe(a => console.log('Subscription A', a));
myObservable
    .subscribe(a => console.log('Subscription B', a));
    
// Result: 
// Subscription A 137
// Subscription B 8

Observable is unicast because each observer has its own instance of the data producer. On the other hand Observable is multicast if each observer receives notifications from the same producer, like this:

const rdn = Math.floor(Math.random() * 200) + 1;
const myObservable = new Observable<number>(subscriber => {
  subscriber.next(rdn);
});
myObservable
  .subscribe(a => console.log('Subscription A', a));
myObservable
  .subscribe(a => console.log('Subscription B', a));

// Result:
// Subscription A 149
// Subscription B 149

This last code snippet is not considering some treatments like errors and completions.

Subject is multicast because of this:

Internally to the Subject, subscribe does not invoke a new execution that delivers values. It simply registers the given Observer in a list of Observers, similarly to how addListener usually works in other libraries and languages.

Take some time to read this.

Mutton answered 19/9, 2019 at 18:19 Comment(4)
Great Explaination, much better than angular docs, thanks!Rewire
Can I say, Observable are like OTT where everyone (subscriber) gets their own version of content produced by OTT platform (producer). And Subjects are like Live telecast where everyone (subscribers) get to see the same content by broadcaster (producer).Gama
Hi, I tried the same example as yours using rxjs ‘of’ operator. In that case it just behaves like multicast. All the subscribers got the same math.random() value.Insightful
@Insightful I guess that using of operation is the same as the 2nd example, that's why you got a multicast behavior.Olag
M
2

its simple an observable is an unicaste means when we subscribe to it ,it just create a new execution that deliver values while subject are multicaste means when we subscribe it ,it does not invoke a new execution that deliver values

Monadelphous answered 22/12, 2022 at 14:50 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Spongioblast

© 2022 - 2024 — McMap. All rights reserved.