what is diference between observable created from of or from and those created from subjects, or behaviour subjects?

Viewed 84

what is diference between observable created from of or from and those created from subjects, or behaviour subjects?

This is a general question regarding what's the difference:

of([1, 2, 3]).subscribe(x => console.log(x));

from([1, 2, 3]).subscribe(x => console.log(x));

vs


const subject = new BehaviorSubject();

// subscriber 1
subject.subscribe((data) => {
    console.log('Subscriber A:', data);
});

subject.next(Math.random());
subject.next(Math.random());

The one thing I know is that with subjects we have control over the values emitted where as with observables we dont.

But consider this, ain't piping observables the same as controlling the observables. So what makes the difference?

I have gone through different posts on SO regarding this topic, but nothing is convincing enough!

What is the difference between a Observable and a Subject in rxjs?

It is said that subjects are used for multi casting, ie for delivering same values to all subscribers. So aint' observables also doin the same or are they delivering them differently.

3 Answers

See the answer is simple! With the subjects you can push new values to the stream such the subscribers receive them, Where as with of and from you are stuck with initial values, unless there is again a source that is firing values from behind such as another observable, promise of event a ui event.

ain't piping observables the same as controlling the observables? So what makes the difference?

Well, yes and no. You can use the pipeable operators to control the behavior and transform the output of the observable stream, but... you cannot create a new emission.

When an observable is created, the emission behavior is pre-determined. This is true for observables created by the rxjs creation functions of, from, interval, etc; as well as when they are created "manually" like the following example:

const observable$ = new Observable(
  observer => {
    observer.next(1);
    observer.next(2);
    observer.next(3);
    observer.complete();
});

observable$ will emit 1, 2, 3, and that's it. There is no way for this observable to emit any more values.

Subjects give you a way to emit a value at any point in time. It's an Observable and Observer all-in-one. The emission of a subject is controlled after the subject is created.

what is difference between observable created from of or from and those created from subjects, or behavior subjects?

One fundamental difference in the observable behavior between subjects and observables and subjects is that Observables are unicast while Subjects are multicast. This answer explains that point well.

"But consider this, ain't piping observables the same as controlling the observables. So what makes the difference?" Piping creates a new observable. You don't get control over the original observable.

A subject is an observer and an observable. The main difference between an observable and a subject is that an observable doesn't have to be an observer but a subject has to be an observer.

of and from create observables that aren't observers. Subjects are observers.

Related