Subjects & Multicasting#
A Subject is both an Observable and an Observer: you can push values into it with next(), and many subscribers can listen to it at the same time. Subjects are the standard way to multicast in RxJS, and they power most state-sharing patterns in Angular services.
Pick the right Subject#
| Type | New subscriber receives | Typical Angular use |
|---|---|---|
Subject |
Only values emitted after subscribing | Event buses, one-off notifications |
BehaviorSubject |
The current value, then updates | State that always has a value (auth status, selected item) |
ReplaySubject |
The last N values, then updates | Late subscribers that need recent history |
AsyncSubject |
Only the final value, at completion | One-shot results shared with all consumers (rare) |
Not sure which one fits? See the Subject vs BehaviorSubject vs ReplaySubject comparison.
Sharing a stream without a Subject#
Operators can multicast an existing Observable for you:
share: one shared subscription to the source, no replay for late subscribers.shareReplay: one shared subscription plus a replay buffer, commonly used to cache HTTP results.
These are the tools behind "make this HTTP call once and let every component use the result", one of the most common Angular interview scenarios.