CHAPTER 20
Beginner
Observables and Subjects
Updated: May 18, 2026
5 min read
# CHAPTER 20
Observables and Subjects
1. Chapter Introduction
While regular Observables are passive (they only emit when something subscribes), Subjects are both an Observable AND an Observer. They can multicast values to multiple subscribers and be triggered programmatically. Understanding the four types of Subjects and theasync pipe is fundamental for building real Angular applications.
2. Learning Objectives
-
Differentiate between
ObservableandSubject.
-
Use
Subject,BehaviorSubject, andReplaySubjectappropriately.
-
Use the
asyncpipe to manage subscriptions in templates.
- Understand hot vs cold Observables.
- Implement real data streams.
3. Subject
ASubject is a multicast Observable. Multiple subscribers receive the same emissions:
typescript
4. BehaviorSubject
BehaviorSubject requires an initial value and always emits the current value to new subscribers:
typescript
Ideal for: Current user, cart count, theme preference, loading state — any value where new subscribers need the current state immediately.
5. ReplaySubject
ReplaySubject buffers N previous values and replays them to new subscribers:
typescript
Ideal for: Chat history, notification logs, browser history.
6. Subject Comparison
| Feature | Subject | BehaviorSubject | ReplaySubject |
|---|---|---|---|
| Initial value | None | Required | None |
| Late subscribers get | Nothing | Current value | Last N values |
| Use case | Events/clicks | Current state | History/log |
.getValue() | No | Yes | No |
7. The async Pipe
The async pipe is Angular's built-in solution for subscribing to Observables in templates. It automatically:
- 1. Subscribes to the Observable.
- 2. Updates the view when new values arrive.
- 3. Unsubscribes automatically when the component is destroyed (prevents memory leaks!).
typescript
html
8. Hot vs Cold Observables
-
Cold Observable: Each subscriber gets its own independent stream. Example:
http.get()— each subscriber triggers a separate HTTP request.
-
Hot Observable: Multiple subscribers share the same stream. Example:
Subject, mouse events — subscribers share the same source.
typescript
9. Common Mistakes
-
Using
SubjectwhenBehaviorSubjectis needed: If a component needs to know the current cart count immediately on creation, a regularSubjectwon't provide it. UseBehaviorSubject.
-
Exposing
BehaviorSubjectdirectly: Never expose theBehaviorSubjectitself from a service. Expose it as an Observable (.asObservable()) to prevent external components from calling.next().
10. MCQs with Answers
Question 1
What is the key difference between an Observable and a Subject?
Question 2
What does BehaviorSubject emit to a late subscriber?
Question 3
What initial setup is required for BehaviorSubject that Subject does not need?
Question 4
How do you retrieve a BehaviorSubject's current value without subscribing?
Question 5
What does ReplaySubject(3) buffer and emit to new subscribers?
Question 6
What three things does the async pipe automatically handle?
Question 7
What is a "Cold Observable"?
Question 8
What RxJS operator converts a Cold Observable to a Hot one and caches the result?
Question 9
Why should you expose a BehaviorSubject from a service as .asObservable() instead of directly?
Question 10
What is the *ngIf="data$ | async as data" pattern useful for?
11. Interview Questions
-
Q: What is the difference between
Subject,BehaviorSubject, andReplaySubject? Give a real-world use case for each.
-
Q: Why is the
asyncpipe preferred over manual subscriptions inngOnInit?
12. Summary
RxJS Subjects are the reactive communication backbone of Angular.BehaviorSubject is the go-to tool for shared application state. The async pipe elegantly solves subscription management in templates, preventing the most common Angular memory leak. Together, they enable building truly reactive, event-driven interfaces.