Question - What is the difference between Cold and Hot Observables in RxJS?
Answer -
In simple words, the concept of cold and hot Observable can be defined as the following:
When the data is produced by the Observable itself, t is called the cold Observable. When the data is produced outside the Observable, it is called hot Observable.
Let's see the differences between Cold Observables and Hot Observables:
Cold Observables | Hot Observables |
We can call an Observable "cold" when the data is produced inside the Observable. | We call the Observable "hot" when the data is produced outside the Observable. |
Cold observables start to run upon subscription. | Hot observables produce values even before a subscription is made. |
The Cold observable sequence only starts pushing values to observers when subscribe is called. | Hot observables such as mouse move events, stock pickers or WebSocket connections are already produced in values even before the subscription is active. |
The cold Observable starts running upon subscription. | The hot Observable produces values before subscriptions. |
The cold Observable sequence starts pushing values. | In cold Observable, the data producer is outside the Observable. |
In cold Observable, the data is produced inside the Observable so, we cannot share the data between multiple subscribers. Two Observables that subscribe at more or less the same may receive two different values. We call this behavior "unicasting." | As we know that the data is produced outside the Observable in hot Observable, so it can share data between multiple subscribers in hot Observable. This behavior is "multicasting." |