Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Hot and Cold observables?

I watched the video and I know the general principles - hot happens even when nobody is subscribed, cold happens "on demand". Also, Publish() converts cold to hot and Defer() converts hot to cold.

But still, I feel I am missing the details. Here are some questions I'd like to have answered:

  • Can you give a comprehensive definition for these terms?
  • Does it ever make sense to call Publish on a hot observable or Defer on a cold?
  • What are the aspects of Hot/Cold conversions - do you lose messages, for example?
  • Are there differences between hot and cold definitions for IObservable and IEnumerable?
  • What are the general principles you should take into account when programming for cold or hot?
  • Any other tips on hot/cold observables?
like image 266
Sergey Aldoukhov Avatar asked Mar 26 '10 05:03

Sergey Aldoukhov


People also ask

Is http cold observable?

An HTTP call to an endpoint is an example of a cold Observable. It will call the endpoint when you subscribe and get the value from the backend. No values are missed in case of a cold Observable. They also re-do everything every time you subscribe.

Which of the following is an example of a hot observable?

A typical example of a hot observable are mousemove events. The mouse moves happen regardless if someone is listening or not. When we start listening for them, we only get future events. Cold Observables on the other hand are the lazy ones.

Is subject hot or cold?

The Subject itself is hot/shared.

What is the difference between observable and subject?

While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. Every Subject is an Observable.


2 Answers

From: Anton Moiseev's Book “Angular Development with Typescript, Second Edition.” :

Hot and cold observables

There are two types of observables: hot and cold. The main difference is that a cold observable creates a data producer for each subscriber, whereas a hot observable creates a data producer first, and each subscriber gets the data from one producer, starting from the moment of subscription.

Let’s compare watching a movie on Netflix to going into a movie theater. Think of yourself as an observer. Anyone who decides to watch Mission: Impossible on Netflix will get the entire movie, regardless of when they hit the play button. Netflix creates a new producer to stream a movie just for you. This is a cold observable.

If you go to a movie theater and the showtime is 4 p.m., the producer is created at 4 p.m., and the streaming begins. If some people (subscribers) are late to the show, they miss the beginning of the movie and can only watch it starting from the moment of arrival. This is a hot observable.

A cold observable starts producing data when some code invokes a subscribe() function on it. For example, your app may declare an observable providing a URL on the server to get certain products. The request will be made only when you subscribe to it. If another script makes the same request to the server, it’ll get the same set of data.

A hot observable produces data even if no subscribers are interested in the data. For example, an accelerometer in your smartphone produces data about the position of your device, even if no app subscribes to this data. A server can produce the latest stock prices even if no user is interested in this stock.

like image 184
H S Progr Avatar answered Oct 02 '22 05:10

H S Progr


Hot observables are ones that are pushing event when you are not subscribed to the observable. Like mouse moves, or Timer ticks or anything like that. Cold observables are ones that start pushing only when you subscribe, and they start over if you subscribe again.

like image 39
Richard Anthony Freeman-Hein Avatar answered Oct 02 '22 04:10

Richard Anthony Freeman-Hein