'Does RxJS `share` operator really makes an Observable hot?

The documentation of a share operator states that: As long as there is at least one Subscriber this Observable will be subscribed and emitting data. ... Because the Observable is multicasting it makes the stream hot

I thought that hot observable is the one that starts emitting data without a need to be subscribed to? Am I wrong?



Solution 1:[1]

You've misunderstood what hot and cold observables are.


Cold Observable

In RxJS an observable is cold (also known as unicasted), when there is one source per subscriber. When subscribing to a cold source, the pipeline gets recreated for each subscriber. Note the following: if your pipeline is computation heavy, then you'd be doing that computation everytime a new subscriber subscribes to that source.

Cold Observable


Hot Observable

In RxJS an observable is hot (also known as multicasting), when there is one source for all subscribers. When subscribing to a hot source, the pipeline doesn't get recreated for each subscriber. Hot sources are useful for when you don't want to compute the pipeline for every subscriber. The pipeline will be computed once, and then the answer will be sent to all subscribers.

Hot Observable


StackBlitz Example showing this in code.

Solution 2:[2]

Yes, hot observables emit data even when no subscriber is active just like a websocket or mouse events. They are independent of subscribers.

Cold observables emit data only when subscribed to it, e.g. without subscribing to an obervable wrapping a http request, your http request will never happen.

Using share, your cold observable gets hot, as no new http request will happen but the old response will be remitted.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Julzz