2

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.

mx_code
  • 2,441
  • 1
  • 12
  • 37

3 Answers3

2

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.

Speed Code
  • 112
  • 1
  • 7
  • So how about this? ```const observable = new Observable(subscriber => { subscriber.next(1); subscriber.next(2); subscriber.next(3); setTimeout(() => { subscriber.next(4); subscriber.complete(); }, 1000); });``` – mx_code Mar 19 '21 at 00:30
  • This is also an observable right? and not a subjects! – mx_code Mar 19 '21 at 00:31
  • That's what I'm saying. You don't have access to the factory creating the stream, while you are using from, of or even new observable(). But while ussing a subject you have access to that factory and you can push new value to the sream. I hope that's clear. – Speed Code Mar 19 '21 at 00:52
  • May be someone else will help you with this. – Speed Code Mar 19 '21 at 00:52
  • Ok. Then can you comment on this? 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. – mx_code Mar 19 '21 at 00:53
2

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.

BizzyBob
  • 12,309
  • 4
  • 27
  • 51
  • Wow. Nice reply. Can you please comment on this also? https://stackoverflow.com/questions/66700745/what-is-the-difference-between-new-observable-and-of-or-from?noredirect=1#comment117908984_66700745 – mx_code Mar 19 '21 at 02:19
  • done! hope it helps :-) – BizzyBob Mar 19 '21 at 02:25
1

"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.

  • Wow. Nice reply. Can you please comment on this also? https://stackoverflow.com/questions/66700745/what-is-the-difference-between-new-observable-and-of-or-from?noredirect=1#comment117908984_66700745 – mx_code Mar 19 '21 at 02:19