What is the difference between Cold and Hot Observables


🔥vs🥶

🔥 Allegory HOT

Image you are watching the news on TV but you've started watching in the middle of the show. You don't know what was reported before and you will not know what happened if you do not watch the show from the beginning.

If someone else has started watching the news after you, he will not have all the information you have, only from the point onwards where he started watching.

The news broadcast on TV is HOT 🔥. 

This means there is one source which emits information and you can subscribe to it. But you will only get the information from the time you've subscribed.

🥶 Allegory COLD

Imagine you want to create a bank account. You go to the bank and they will tell you:
  • Bring your ID
  • Fill in the form
  • Sign it
  • Login to you mobile app
  • etc.
The next person who wants to create an account will get probably the same information in the same order.

Creating a bank account is COLD 🥶

This means that each time you want to get the information from the source, the source will create a new process specially for you so will get all the information from the beginning.

🥶🪄🔥Making Cold Observables Hot

This is a use case which is often needed. An Http endpoint is a COLD source. Assume you have multiple subscribers to the result of an Http call. Because this source is COLD each subscriber will call the Http endpoint, but you just need to call it once. This is where you turn the COLD source to a HOT one so that each subscriber can use its result without calling the endpoint again.

But because HOT observables only emit data from the point when you subscribe to it, you can use "replay" operators which cache the already emitted infos and replay it for each subscriber.

✌️Conclusion

So the main difference is where the source of the information is produced. 
When using HOT observables you generally subscribe to sources that already exists. 
When using COLD observabled the source is produced each time you subscribe to it.


If want to dig a little deeper here is a great article from Ben Lesh.

Comments

Popular posts from this blog

What is Base, Local & Remote in Git merge

Asynchronous Nunjucks with Filters and Extensions

Debug Azure Function locally with https on a custom domain