Hot vs Cold Observables
You'll learn: The crucial difference between cold observables (which create a new data source for each subscriber) and hot observables (which share one data source among all subscribers).
What Makes an Observable Cold?
A cold observable creates a fresh execution of its producer logic for every subscriber. Each subscription gets its own independent stream. Think of it like a movie on Netflix—every viewer who presses play starts the movie from the beginning, completely independently.
When you subscribe to a cold observable, the data production starts because you subscribed. Unsubscribe, and that particular production stops. Two subscribers? Two separate productions running in parallel.
const cold$ = new Observable(observer => {
console.log('Producer started');
observer.next(Math.random());
});
cold$.subscribe(val => console.log('Sub A:', val));
// Producer started
// Sub A: 0.234...
cold$.subscribe(val => console.log('Sub B:', val));
// Producer started (again!)
// Sub B: 0.891... (different value)
What Makes an Observable Hot?
A hot observable shares a single execution among all subscribers. The producer exists independently of subscriptions—it's already running. Think of it like live TV: everyone watching sees the same broadcast at the same moment. Join late? You've missed what aired earlier.
Hot observables are typically created by converting cold ones (using operators like share()) or wrapping external event sources like DOM events or WebSocket connections.
The Key Distinction
Cold observables are unicast (one producer per subscriber). Hot observables are multicast (one producer, many subscribers). Most observables you create with new Observable() are cold by default.
Key Takeaway: Cold observables restart their data production for each subscriber (like a Netflix movie), while hot observables share a single ongoing stream across all subscribers (like live TV).