Angular2: difference between a Behavior Subject and an Observable

Standard

A couple of weeks back, I was trying to understand some basic concepts about Angular2 and came across this excellent stack overflow question. Perfect explanation by Shantanu Bhadoria

“Behavior Subject 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 a behavior subject are:

  • Behavior subject 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 a 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 a observable are:

  • It is a observer in addition to being a observable so you can also send values to a subject in addition to subscribing to it.

In addition you can get a observable from behavior subject using the asobservable() method on behaviour subject.

Observable is a Generic, and Behavior subject is technically a sub-type of Observable because behavior subject is a observable with specific qualities.

Example with behavior subject:

// Behavior Subject

// a is a 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 Regular and Behavior Subjects using subject.asobservable(). Only difference being you can’t send values to a observable using next() method.

In angular2 services, I would use behavior subject for a data service as a 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”

Source: Stack Overflow (Answer By Shantanu Bhadoria)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s