Understanding BehaviorSubject in Angular Through the Thermometer Analogy

PeterMilovcik - Sep 28 '23 - - Dev Community

When learning Angular and delving into the reactive programming paradigm, it's easy to stumble upon terms that may seem intimidating—Observable, Subject, BehaviorSubject, and so on. Today, we're going to demystify one of these terms: BehaviorSubject.

Table of Contents

  1. Introduction
  2. What is BehaviorSubject?
  3. The Thermometer Analogy
  4. Using next() Method
  5. BehaviorSubject vs Observable
  6. Conclusion

1. Introduction

Angular relies heavily on reactive programming patterns, particularly those provided by the RxJS library. Reactive programming is all about dealing with asynchronous operations and data streams. This is where BehaviorSubject comes in as a powerful construct for managing these streams.

2. What is BehaviorSubject?

A BehaviorSubject is a type of subject, a particular kind of observable in RxJS. What makes it unique is that it stores the "current" value. This means that when you subscribe to it, you immediately get the latest emitted value—or a default value if none has been emitted yet.

import { BehaviorSubject } from 'rxjs';

const temperature$ = new BehaviorSubject<number>(20);
Enter fullscreen mode Exit fullscreen mode

In the code above, temperature$ is a BehaviorSubject that starts with an initial value of 20. The $ at the end is a naming convention often used for Observables.

3. The Thermometer Analogy

Think of BehaviorSubject as a digital thermometer in a room. Various departments in an organization—say, Maintenance, Operations, and HR—need to know the room's temperature.

Initialization

When you set up the thermometer, you determine that the initial temperature is 20°C. This initial value is crucial for any department that decides to monitor the temperature.

Subscriptions

Maintenance and Operations departments 'subscribe' to the thermometer. From that point on, they get updated readings whenever the temperature changes.

temperature$.subscribe((temp) => {
  console.log(`Maintenance department: Current temperature ${temp}°C`);
});

temperature$.subscribe((temp) => {
  console.log(`Operations department: Current temperature ${temp}°C`);
});
Enter fullscreen mode Exit fullscreen mode

New Subscription

Now, HR also decides to subscribe. The moment they do, they receive the current temperature, not the initial one.

4. Using next() Method

To change the thermometer's reading, you use the next() method. This action updates the current value and informs all subscribers about this change.

temperature$.next(22);
Enter fullscreen mode Exit fullscreen mode

This will log:

Maintenance department: Current temperature 22°C
Operations department: Current temperature 22°C
Enter fullscreen mode Exit fullscreen mode

The next() function effectively pushes the new value into the BehaviorSubject, updating all active subscribers.

5. BehaviorSubject vs Observable

You might ask, "How is this different from an Observable?" The crucial difference lies in the initial value and the immediate availability of the current value. With a standard Observable, new subscribers would have to wait for a new value to be emitted. They don't get the latest value right when they subscribe, as with BehaviorSubject.

6. Conclusion

Understanding BehaviorSubject can significantly impact how you manage state and data flow in Angular applications. The ability to emit, listen, and react to new data points in different parts of your application is not just powerful; it also aligns closely with Angular's reactive ethos.

With this newfound knowledge, you're one step closer to mastering Angular's reactive landscape. Happy coding!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .