1
votes

According to the docs on Subject, it's a special case of Observable that lets all the observers share a common execution path. The example shows that each observer gets the same emitted values after it's subscribed to the source.

I'm not entirely clear on how it differs from a case of a plan Observable emitting values. Every one of the multiple subscribers will receive each next'ed value. The values that have been emitted prior to respective subscription aren't delivered (unless we have pipe'ed in some shareReply'ing of those explicitly).

What's the actual difference making Subject a special case of Observable? I'm missing the obvious, possibly.

3
You can subscribe from an observable but can't push values into it, while Subject can do both - Imtiaz Shakil Siddique
This question has answers to all of your questions: stackoverflow.com/q/52014334/9172805 - Hope

3 Answers

5
votes

An observable, by definition is a data producer.

A Subject on the other hand can act as both – a data producer and a data consumer.

This implies two things.

  1. A subject can be subscribed to, just like an observable.
  2. A subject can subscribe to other observables.

That being said, there is one critical difference between a subject and an observable.

All subscribers to a subject share the same execution of the subject. i.e. when a subject produces data, all of its subscribers will receive the same data. This behavior is different from observables, where each subscription causes an independent execution of the observable.

Example:

  // Here a subject is acting like a data producer
  const subject = new Subject();
  const subjObserv = subject.asObservable();
  subjObserv.subscribe((data: number) => console.log("subect A " + data));

  for (let i = 1; i <= 5; i++) subject.next(i);

  subjObserv.subscribe((data: number) => console.log("subect B " + data));
  subject.next(6);


  // simple observer
  const plainObs = Observable.of([1, 2, 3, 4, 5]);
  plainObs.subscribe(data => console.log("plain onservable A " + data));
  plainObs.subscribe(data => console.log("plain onservable B " + data));

Output:

subect A 1
subect A 2
subect A 3
subect A 4
subect A 5
subect A 6
subect B 6
plain onservable A 1,2,3,4,5
plain onservable B 1,2,3,4,5

As you can notice we get the output as many times as we subscribe plainObs but for the subjObserv we get output that are emitted after subscription.

1
votes

"...lets all the observers share a common execution path": Check this out.

The way I understand the difference between an Observable and a Subject is that you can .next() values into a Subject, but you can't do that with an Observable.

0
votes

This is how we use Subject, Using subject(.next) you can push new value like in below:

  var source = new Subject();
    source.map(x => ...).filter(x => ...).subscribe(x => ...)
    source.next('newValue1')
    source.next('newValue2')

To create Subject we dont need any observer.

We can create new Observable with an Observer like in below:

var observable = Observable.create(observer => { 
    observer.next('newValue1'); 
    observer.next('newValue2'); 
    ... 
});
observable.map(x => ...).filter(x => ...).subscribe(x => ...)

To create observable you need at least one observer and it(Observable) informs to only observer.here you cant push new value, it will read only whatever the response will get from sources.

Conclusion: I mean Observable is like only reading values but subject is like read and writing values.