Full answer
An Observable is a description of work. Whether the work is shared depends on where the source is created.
Cold observable: the producer is created when you subscribe. Each subscriber gets its own run. HttpClient.get() is cold: if two places subscribe, two requests are sent. of(1, 2, 3), interval(1000) and from([...]) are also cold. Think of it as a video you press play on for each viewer.
Hot observable: the producer exists outside the subscription, and values are emitted whether or not someone is listening. DOM events, a Subject, and a WebSocket connection that is already open behave this way. A late subscriber misses earlier values. Think of a live TV channel.
Why it matters:
- Two
asyncpipes on the same cold observable mean two requests. - A
tapside effect runs once per subscriber. - Late subscribers to a hot source may miss values, so you may need a replay.
Making a cold source hot (multicasting):
share(): one shared subscription to the source for all current subscribers. When the last subscriber leaves, the source is unsubscribed, and the next subscriber starts again.shareReplay({ bufferSize: 1, refCount: true }): also replays the latest value to late subscribers. UserefCount: trueso the source stops when nobody listens. WithoutrefCount, the source is never unsubscribed, which can leak.- A
SubjectorBehaviorSubjectthat you feed from the source manually. - In signal code,
toSignal(source$)subscribes once and shares the latest value with all readers.
Rule: if more than one place needs the same result, share it once.
import { Observable, share, take, timer } from 'rxjs';
let starts = 0;
const cold$ = new Observable<number>((subscriber) => {
starts++;
subscriber.next(starts);
subscriber.complete();
});
cold$.subscribe();
cold$.subscribe();
console.log('cold, two subscribers, source started', starts, 'times');
let timerStarts = 0;
const timerCold$ = new Observable<number>((subscriber) => {
timerStarts++;
const id = setTimeout(() => {
subscriber.next(1);
subscriber.complete();
}, 10);
return () => clearTimeout(id);
});
const timerShared$ = timerCold$.pipe(share());
let received = 0;
timerShared$.pipe(take(1)).subscribe(() => received++);
timerShared$.pipe(take(1)).subscribe(() => received++);
setTimeout(() => console.log('shared, source started', timerStarts, 'time, subscribers received', received), 40);RxJS 7.8. The script was run in Node 24 twice with identical output. It counts how many times the cold source starts with and without share.
Key points
- Cold: each subscriber triggers its own run (HTTP, `of`, `interval`).
- Hot: values are produced independent of subscribers (events, Subjects).
- Use `share` or `shareReplay({ bufferSize: 1, refCount: true })` to multicast.
- Two subscriptions to a cold HTTP observable send two requests.
Common mistakes
- Subscribing twice to an HTTP observable and sending two requests by accident.
- Using `shareReplay(1)` without `refCount`, so the source never stops.
- Assuming a Subject replays old values to late subscribers.
Follow-ups
- What does `refCount` do in `shareReplay`?
- Is a `Promise` hot or cold?
- How does `toSignal` relate to sharing?
Tips
- Use the HTTP example. Everyone knows it.
- Name `refCount` and the leak it avoids.
- Link to the "two async pipes" bug.
Evaluation rubric
- Defines hot and cold correctly with examples.
- Explains the practical effects.
- Knows how to multicast safely.