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

10

156

I was going through this blog and reading about Observables and couldn't figure out the difference between the Observable and a Subject.

Woodie answered 28/11, 2017 at 17:50 Comment(2)
An Observible is an array/value that can be manipulated and immediately reflected. A Subject is an EventEmitter that does just that: Emits an event. You can then manipulate multiple observers of different types based on the event.Abstracted
reactivex.io/documentation/observable.html and reactivex.io/documentation/subject.htmlDafodil
S
263

In stream programming there are two main interfaces: Observable and Observer.

Observable is for the consumer, it can be transformed and subscribed:

observable.map(x => ...).filter(x => ...).subscribe(x => ...)

Observer is the interface which is used to feed an observable source:

observer.next(newItem)

We can create new Observable with an Observer:

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

Or, we can use a Subject which implements both the Observable and the Observer interfaces:

var source = new Subject();
source.map(x => ...).filter(x => ...).subscribe(x => ...)
source.next('first')
source.next('second')
Scenarist answered 28/11, 2017 at 18:58 Comment(3)
Perfect explanation. +1 For the code examples, specially creating observable using observer.Authors
I think, In Observable observable.filter and observable.map is not a function and same with Subject.Fortson
Better to suffix the Observable variable with the suffix "$" for better coding practice.Drying
P
249

Observables are unicast by design and Subjects are multicast by design.

If you look at the below example, each subscription receives the different values as observables developed as unicast by design.

import {Observable} from 'rxjs';

let obs = new Observable<any>(observer=>{
   observer.next(Math.random());
})

obs.subscribe(res=>{
  console.log('subscription a :', res); //subscription a :0.2859800202682865
});

obs.subscribe(res=>{
  console.log('subscription b :', res); //subscription b :0.694302021731573
});

This could be weird if you are expecting the same values on both the subscription.

We can overcome this issue using Subjects. Subjects is similar to event-emitter and it does not invoke for each subscription. Consider the below example.

import {Subject} from 'rxjs';

let obs = new Subject();

obs.subscribe(res=>{
  console.log('subscription a :', res); // subscription a : 0.91767565496093
});

obs.subscribe(res=>{
  console.log('subscription b :', res);// subscription b : 0.91767565496093
});

obs.next(Math.random());

Both of the subscriptions got the same output value!

Prolongation answered 30/3, 2020 at 16:13 Comment(3)
Really good idea to demonstrate with a random value, much better than solely referring to unicast/multicast.Happening
Observable.create is now deprecated in favor of new Observable(), With that now syntax you cannot call .next() on it. What is the new syntax for this?Hauge
@FiniteLooper You can use something like new Observable<string>((subscriber)=>{ subscriber.next('Hello World'); })Erikerika
A
79

Observables

  1. They are cold: Code gets executed when they have at least a single observer.

  2. Creates copy of data: Observable creates copy of data for each observer.

  3. Uni-directional: Observer can not assign value to observable(origin/master).

  4. The code will run for each observer . If its a HTTP call, it gets called for each observer.

  5. if its a service we want to share among all the components, it wont have latest result all new subscribers will still subscribe to same observable and get value from scratch

  6. Unicast means can emit values from the observable not from any other component.

Subject

  1. They are hot: code gets executed and value gets broadcast even if there is no observer.

  2. Shares data: Same data get shared between all observers.

  3. bi-directional: Observer can assign value to observable(origin/master).

  4. If are using using subject then you miss all the values that are broadcast before creation of observer. So here comes Replay Subject

  5. multicast, can cast values to multiple subscribers and can act as both subscribers and emmitter

Appaloosa answered 29/8, 2019 at 23:22 Comment(0)
O
26

I found the accepted answer slightly confusing!

An Observer isn't the interface for feeding an Observable source, it's the interface for observing an Observable source... which makes more sense from the name, right?

So, the reason that:

var observable = Observable.create(observer => { 
    observer.next('first'); 
    observer.next('second'); 
    ... 
});

works - creating an observable which emits 'first' then 'second' - is that the argument to Observable.create(...) is a subscribe function, it basically defines which Observer events will happen on a direct Observer of that Observable.

If you want to go into it a little bit further again, it's important to understand that the subscribe function isn't directly called on the Observer object when you subscribe, instead it's mediated by a Subscription object which can enforce correct observable rules, e.g. that an Observable will never emit a new value after observer.complete() has been called, even if your subscribe function looks as if it would.

REF: http://reactivex.io/rxjs/manual/overview.html#creating-observables

A Subject is both an Observable and an Observer and once again it looks just like the Observer interface is the way to 'feed' events to the Subject. But it's easier to understand the naming if you realise that a Subject is a bit like an Observable with the equivalent of the subscribe function (i.e. where you define what events will happen to things observing it) sitting there right on the object, even after it has been created. So, you call Observer methods on the Subject to define what Observer events will happen on things observing it! 😊 (And again, there are intermediate objects involved, to make sure that you can only do legal sequences of things.)

REF: http://reactivex.io/rxjs/manual/overview.html#subject

Ondometer answered 6/2, 2020 at 12:17 Comment(1)
I too was confused after reading the accepted answer and was wondering whether it's just me or anyone else is also not satisfied with it. Thanks for posting your thoughts.Stony
L
21

See rxjs document (more information and examples there): http://reactivex.io/rxjs/manual/overview.html#subject

What is a Subject? An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast.

A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners.

and code, Subject extending Observable: https://github.com/ReactiveX/rxjs/blob/master/src/internal/Subject.ts#L22

/**
 * @class Subject<T>
 */
export class Subject<T> extends Observable<T> implements SubscriptionLike {
//...
}
Linguist answered 23/7, 2018 at 23:26 Comment(0)
P
18

Briefly,

subject: you can send to it and receive from it.

Observable: you can receive from it only.

In another words, In subject you can subscribe to it and you can use it to broadcast to other subscribers any time and anywhere in code.

whilst, in observable you can subscribe to it only (you can't use it to broadcast data after it have been initialized). The only place you can broadcast data from observable is inside its constructor.

Pacifistic answered 30/8, 2021 at 9:16 Comment(1)
Good point! That's an important difference I just realized.Sailesh
N
16

Imagine if you have a stream of data coming into your application like in a websocket connection. You want a way to handle it. There is a few solution:

1. normal ajax request: This solution is not viable because it is not applicable to process push data. It is more of a pull then a push.

2. Promise: Also not good because you have to trigger them and they can only retrieve once. Also more of a pull then a push.

So in order to retrieve this data, in the old time, we do a long-polling. Which is where we set an interval function to retrieve that stream of data every 1 minute for an example. Though it works, it actually burdening resources like CPU and memory.

But now with option no 3,

3. Observable: You can subscribe and let the stream of data to come in non-stop until the function complete has been called.

Cool right ? But then there is another problem. What if you want to observe incoming data only once somewhere in your application. But you want to use that data simultaneously around your application when the data arrived. That is when and where you use Subject. You place subject.subscribe() at places you want to use throughout your application. When the data arrived, places where there is subject.subscribe() will process them simultaneously. But the observer must subscribe with the subject as its argument like this.

observer.subscribe(subject).

Example application is when you want to build a notification alert.

You cannot have multiple subscription of the same observable because chances are, each subscribers will received different input data. But with subject, all that subscribe() through subject will be retrieving the same data.

Another analogy is through magazine subscription. Each subscribers will received the magazine with their name on it. So, different subscription = different receiver name.(Normal Observable) But when you share with your friends, all of your friend would receive the same magazine with only your name on it.(Normal Observable with Subject)

This guy explain it very well with code example. You can check it out at https://javascript.tutorialhorizon.com/2017/03/23/rxjs-subject-vs-observable/

Hopefully this answer helps.

Newkirk answered 24/9, 2019 at 2:22 Comment(1)
I found using everyday ordinary thing analogy is a lot more easier to understand vs. terminology-on-terminology.Walsh
H
14

Observable can inform only one observer, while Subject can inform multiple observers.

Hutch answered 17/4, 2019 at 2:35 Comment(1)
for each subscription observable output is diffrent but if you are expecting same output for in diffrent observer recommended to use Subject!Prolongation
H
7

From another perspective, it is good to note that the subscription to an Observable re-execute the Observable function. This can lead performance issue if the data source is a service for instance.

If you want several subscribers to get the same value, you may need a Subject. For this, make sure that your subscription is set before the Subject subscribed to the data source. Otherwise, your process would be stuck.

More details here: https://javascript.tutorialhorizon.com/2017/03/23/rxjs-subject-vs-observable/

Hellfire answered 8/2, 2019 at 14:50 Comment(0)
T
6

Observable: Only the Observable knows how and when the events are triggered on the observable. i.e the next() method has to be called only inside the instantiated constructor. Also, on subscribing each time, a separate observer is created and calls next() method using particular observer inside constructor only, in the following example subscriber itself is the observer and it is subscribed when the instantiated constructor gets executed. Ex:

import { Observable } from 'rxjs';

const observable = new Observable(subscriber => {
  subscriber.next(1);
  subscriber.next(2);
  setTimeout(() => {
    subscriber.next(3);        
  }, 1000);
});

Subject: Here next() method can be used by subject anywhere outside the constructor. Also, when next() method is called before subscribing, the particular event will be missed. Hence next() method has to be called only after subscribing. Ex:

import { Subject } from 'rxjs';
 
const subject = new Subject<number>();
 

subject.next(1); // this is missed
subject.subscribe({
  next: (v) => console.log(`observerA: ${v}`)
});
subject.subscribe({
  next: (v) => console.log(`observerB: ${v}`)
});     
subject.next(2);
Trackman answered 16/2, 2021 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.