0
0
Angularframework~10 mins

Subject types (Subject, BehaviorSubject, ReplaySubject) in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subject types (Subject, BehaviorSubject, ReplaySubject)
Create Subject
Subscribe Observers
Emit Values
Subject: Sends to current subscribers only
BehaviorSubject: Sends last value + new values
ReplaySubject: Sends last N values + new values
This flow shows how different Subject types handle subscriptions and emitted values in Angular.
Execution Sample
Angular
import { Subject } from 'rxjs';
const subject = new Subject<number>();
subject.subscribe(v => console.log('Sub1:', v));
subject.next(1);
subject.subscribe(v => console.log('Sub2:', v));
subject.next(2);
This code creates a Subject, subscribes two observers at different times, and emits values to show how Subject sends values only to current subscribers.
Execution Table
StepActionSubject StateSubscribersOutput
1Create SubjectNo values emittedNoneNone
2Subscribe Sub1No values emittedSub1None
3Emit value 1Value 1 emittedSub1Sub1: 1
4Subscribe Sub2Value 1 emittedSub1, Sub2None
5Emit value 2Value 2 emittedSub1, Sub2Sub1: 2, Sub2: 2
💡 No more emissions; Subject completes or unsubscribes.
Variable Tracker
VariableStartAfter Step 3After Step 5
subjectValueundefined12
subscribers012
Key Moments - 3 Insights
Why does Sub2 not receive the value 1 when it subscribes after it was emitted?
Because Subject only sends values to subscribers active at the time of emission, as shown in execution_table step 4 where Sub2 subscribes but no output occurs.
How would BehaviorSubject behave differently at step 4?
BehaviorSubject sends the last emitted value immediately to new subscribers, so Sub2 would receive value 1 upon subscribing, unlike Subject.
What does ReplaySubject do differently compared to BehaviorSubject?
ReplaySubject can send multiple past values (not just the last one) to new subscribers, depending on its buffer size.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what output occurs at step 3?
ASub1: 1
BSub2: 1
CSub1: 2
DNo output
💡 Hint
Check the Output column at step 3 in execution_table.
At which step does Sub2 start receiving values?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look at Subscribers and Output columns for Sub2 in execution_table.
If we replace Subject with BehaviorSubject, what changes at step 4?
ASub2 receives no values
BSub2 receives value 1 immediately
CSub1 stops receiving values
DNo change
💡 Hint
Recall BehaviorSubject sends last emitted value to new subscribers.
Concept Snapshot
Subject types in Angular:
- Subject: sends values only to current subscribers
- BehaviorSubject: sends last emitted value immediately to new subscribers
- ReplaySubject: sends last N emitted values to new subscribers
Use Subjects to multicast values to multiple observers
Choose type based on how past values should be replayed
Full Transcript
This lesson shows how Angular's Subject types work. A Subject sends values only to subscribers active when the value is emitted. BehaviorSubject remembers the last value and sends it immediately to new subscribers. ReplaySubject can remember multiple past values and send them to new subscribers. The example code creates a Subject, subscribes one observer, emits a value, then subscribes a second observer who does not get the previous value. When a new value is emitted, both observers receive it. This helps understand how these Subjects behave differently when subscribers join at different times.