0% found this document useful (0 votes)
122 views2 pages

Behavior Subject Vs Observable

The document discusses the differences between BehaviorSubject and Observable in RxJs. A BehaviorSubject is a type of Observable that has an initial value and always returns the last emitted value on subscription. An Observable only emits values and does not store previous values. A BehaviorSubject is useful in Angular services to ensure components receive the last updated data.

Uploaded by

ashish877
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

Behavior Subject Vs Observable

The document discusses the differences between BehaviorSubject and Observable in RxJs. A BehaviorSubject is a type of Observable that has an initial value and always returns the last emitted value on subscription. An Observable only emits values and does not store previous values. A BehaviorSubject is useful in Angular services to ensure components receive the last updated data.

Uploaded by

ashish877
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

BehaviorSubject vs Observable?

I'm looking into Angular RxJs patterns and I don't understand the difference
between a BehaviorSubject and an Observable.

From my understanding, a BehaviorSubject is a value that can change over time (can
be subscribed to and subscribers can
receive updated results). This seems to be the exact same purpose of an Observable.

When would you use an Observable vs a BehaviorSubject? Are there benefits to using
a BehaviorSubject over an Observable
or vice versa?

BehaviorSubject is a type of subject, a subject is a special type of observable so


you can subscribe to messages like
any other observable. The unique features of BehaviorSubject are:

It needs an initial value as it must always return a value on subscription even if


it hasn't received a next()
Upon subscription, it returns the last value of the subject. A regular observable
only triggers when it receives an onnext
at any point, you can retrieve the last value of the subject in a non-observable
code using the getValue() method.
Unique features of a subject compared to an observable are:

It is an observer in addition to being an observable so you can also send values to


a subject in addition to subscribing
to it.
In addition, you can get an observable from behavior subject using the
asObservable() method on BehaviorSubject.

Observable is a Generic, and BehaviorSubject is technically a sub-type of


Observable because BehaviorSubject
is an observable with specific qualities.

Example with BehaviorSubject:

// Behavior Subject

// a is an initial value. if there is a subscription


// after this, it would get "a" value immediately
let bSubject = new BehaviorSubject("a");

bSubject.next("b");

bSubject.subscribe(value => {
console.log("Subscription got", value); // Subscription got b,
// ^ This would not happen
// for a generic observable
// or generic subject by default
});

bSubject.next("c"); // Subscription got c


bSubject.next("d"); // Subscription got d
Example 2 with regular subject:

// Regular Subject

let subject = new Subject();


subject.next("b");

subject.subscribe(value => {
console.log("Subscription got", value); // Subscription wont get
// anything at this point
});

subject.next("c"); // Subscription got c


subject.next("d"); // Subscription got d
An observable can be created from both Subject and BehaviorSubject using
subject.asObservable().

The only difference being you can't send values to an observable using next()
method.

In Angular services, I would use BehaviorSubject for a data service as an angular


service often
initializes before component and behavior subject ensures that the component
consuming the service
receives the last updated data even if there are no new updates since the
component's subscription to this data.

You might also like