Different type of Subjects in Rxjs (used in Angular)
- Subject
- BehaviorSubject
- ReplaySubject
- AsyncSubject
Subject
- A plain Subject does not hold the current/last value.
- It emits values only to subscribers at the time of emission (not future ones).
- Used for event emitters or multicast observables.
A standard Subject behaves similarly to a regular Observable in that a new Subscriber only receives values emitted after it subscribes.
iimport { Subject } from 'rxjs';
const subject = new Subject<number>();
// Subscriber 1
subject.subscribe(data => console.log('Subscriber 1:', data));
subject.next(1); // Emit only next called
subject.next(2);
// Subscriber 2 (subscribes after some values have been emitted)
setTimeout(() => {
subject.subscribe(data => console.log('Subscriber 2:', data));
subject.next(3); // Send/Emit 3 to all subscribers so far
}, 100);
// Output:
// Subscriber 1: 1
// Subscriber 1: 2// Subscriber 1: 3 //First users get the Latest Value only
// Subscriber 2: 3 //Second user will get Latest Value only
Both User/Subscribe now see only the last/latest value
Use Case
Good for storing and accessing state, like current user or auth status.
Ideal for scenarios where you want to broadcast new events or data to multiple listeners simultaneously, and each listener only needs to know about the events happening from the moment they subscribe.
Example:
Imagine a simple chat application where new messages are broadcast to all connected users. A Subject would be suitable here, as a newly joined user only needs to see messages sent after they've logged in.
BehaviorSubject
it immediately receives the last emitted value (the current value) and then continues to receive any old/subsequent values. You need to provide an initial value when creating a BehaviorSubject. It doesn't get any of the previously emitted values.
Subscribers get the current/last value upon subscription and subsequent emissions.
import { BehaviorSubject } from 'rxjs';
const behaviorSubject = new BehaviorSubject<string>('Initial Value');
// Subscriber 1
behaviorSubject.subscribe(data => console.log('Subscriber 1:', data));
behaviorSubject.next('First Update'); // Emit only next called
// Subscriber 2 (subscribes later)
behaviorSubject.subscribe(data => console.log('Subscriber 2:', data));
behaviorSubject.next('Second Update'); // Send/Emit 'Second Update' to all subscribers so far
// Output:
// Subscriber 1: Initial Value- Show welcome message to first user who join
// Subscriber 1: First Update- Show value input value added by 1st User
// Subscriber 2: First Update //Show first the last emitted value to new user/subscriber. value imputed by previous users first.
// Subscriber 1: Second Update // First user will get latest value input by 2nd User
// Subscriber 2: Second Update // Second user will get the latest value input by him
Use Cases:
Excellent for representing state that needs to be immediately available to new subscribers. Think of it as a setting or a piece of data that always has a current state.
Example:
Managing the current user's login status in an application. New components that load need to know immediately if the user is logged in or not.