What are the Hot and Cold observables?
Asked Answered
G

4

135

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?
Gardal answered 26/3, 2010 at 5:20 Comment(1)
See also IConnectableObservables in RxTennilletennis
D
41

I hope this helps.

Can you give a comprehensive definition for these terms?

See my blog post at: https://leecampbell.com/2010/08/19/rx-part-7-hot-and-cold-observables

Does it ever make sense to call Publish on a hot observable or Defer on a cold?

No, not that I can think of.

What are the aspects of Hot/Cold conversions - do you lose messages, for example?

It is possible to "lose" messages when the Observable is Hot, as "events" happen regardless of subscribers.

Are there differences between hot and cold definitions for IObservable and IEnumerable?

I dont really understand the question. I hope this analogy helps though. I would compare a Hot Observable to an Eagerly evaluated IEnumerable. ie a List or an Array are both Eagerly evaluated and have been populated even if no-one enuemerates over them. A yield statement that gets values from a file or a database could be lazily evaluated with the Yield keyword. While lazy can be good, it will by default, be reevaluated if a second enumerator runs over it. Comparing these to Observables, a Hot Observable might be an Event (Button click) or a feed of temperatures; these events will happen regardless of a subscription and would also be shared if multiple subscriptions were made to the same observale. Observable.Interval is a good example of a Cold observable. It will only start producing values when a subscription is made. If multiple subscriptions as made then the sequence will be re-evaluated and the "events" will occur at seperate times (depending on the time between subscriptions).

What are the general principles you should take into account when programming for cold or hot?

Refer to the link in point one. I would also recommend you look into Publsh being used in conjunction with RefCount. This allows you to have the ability to have Lazy evaluation semantics of Cold Observables but the sharing of events that Hot Observables get.

Any other tips on hot/cold observables?

Get your hands dirty and have a play with them. Once you have read about them for more than 30minutes, then time spent coding with them is far more productive to you than reading any more :)

Dardan answered 19/8, 2010 at 13:59 Comment(5)
The link to your blog is deadPhila
Link seems fine to me. Potentially you are being blocked by a corporate firewall? Alternative links are leecampbell.blogspot.co.uk/2010/08/… or the more recent introtorx.com/Content/v1.0.10621.0/….Dardan
Here there's a use case for deferring a cold observable: blog.danlew.net/2015/07/23/…Disaccord
That is an example of defering a hot observable. RxJava's Just is hot/eagerDardan
The correct link is: leecampbell.com/2010/08/19/rx-part-7-hot-and-cold-observables (in your answer it is: leecampbell.blogspot.com/2010/08/…)Fleabite
F
212

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.

Feltie answered 4/11, 2019 at 11:17 Comment(2)
So one is a factory and other a running instance? Then shouldn't it be less confusingly called "observable factory"?Birth
so can we say Hot observables are Eager loaded and Cold observables are Lazy loaded ?Enzootic
H
71

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.

Hacking answered 31/3, 2010 at 0:9 Comment(4)
I am surprised to see this posted, I was only half finished writing it, and this tells you only what you already know, sorry. Too late to finish tonight. I will update this tomorrow with some code samples.Hacking
KIds? Will check in again in 18 years. ;^D (Btw, I like the start... a good, step by step example (rather than Lee's link to a blog) would be appreciated, even in 2019!)Bolling
This answer is incorrect. Hot observables are not eager. The difference is that cold observables create one observer per subscription, whereas hot observables create only a single observer, and only when there is at least one subscriber.Metropolis
@Distortum it was correct for the time and context. Hot observables are in general things that are firing events without subscribers. I got that from the creator. So, maybe that's changed, but it was correct when posted and sufficient for the context. Feel free to extend the answers.Hacking
D
41

I hope this helps.

Can you give a comprehensive definition for these terms?

See my blog post at: https://leecampbell.com/2010/08/19/rx-part-7-hot-and-cold-observables

Does it ever make sense to call Publish on a hot observable or Defer on a cold?

No, not that I can think of.

What are the aspects of Hot/Cold conversions - do you lose messages, for example?

It is possible to "lose" messages when the Observable is Hot, as "events" happen regardless of subscribers.

Are there differences between hot and cold definitions for IObservable and IEnumerable?

I dont really understand the question. I hope this analogy helps though. I would compare a Hot Observable to an Eagerly evaluated IEnumerable. ie a List or an Array are both Eagerly evaluated and have been populated even if no-one enuemerates over them. A yield statement that gets values from a file or a database could be lazily evaluated with the Yield keyword. While lazy can be good, it will by default, be reevaluated if a second enumerator runs over it. Comparing these to Observables, a Hot Observable might be an Event (Button click) or a feed of temperatures; these events will happen regardless of a subscription and would also be shared if multiple subscriptions were made to the same observale. Observable.Interval is a good example of a Cold observable. It will only start producing values when a subscription is made. If multiple subscriptions as made then the sequence will be re-evaluated and the "events" will occur at seperate times (depending on the time between subscriptions).

What are the general principles you should take into account when programming for cold or hot?

Refer to the link in point one. I would also recommend you look into Publsh being used in conjunction with RefCount. This allows you to have the ability to have Lazy evaluation semantics of Cold Observables but the sharing of events that Hot Observables get.

Any other tips on hot/cold observables?

Get your hands dirty and have a play with them. Once you have read about them for more than 30minutes, then time spent coding with them is far more productive to you than reading any more :)

Dardan answered 19/8, 2010 at 13:59 Comment(5)
The link to your blog is deadPhila
Link seems fine to me. Potentially you are being blocked by a corporate firewall? Alternative links are leecampbell.blogspot.co.uk/2010/08/… or the more recent introtorx.com/Content/v1.0.10621.0/….Dardan
Here there's a use case for deferring a cold observable: blog.danlew.net/2015/07/23/…Disaccord
That is an example of defering a hot observable. RxJava's Just is hot/eagerDardan
The correct link is: leecampbell.com/2010/08/19/rx-part-7-hot-and-cold-observables (in your answer it is: leecampbell.blogspot.com/2010/08/…)Fleabite
G
17

Not pretending to give a comprehensive answer, I'd like to summarize in a simplest form what I have learned since the time of this question.

Hot observable is an exact match for event. In events, values usually are fed into the handler even if no subscribers are listening. All subscribers are receiving the same set of values. Because of following the "event" pattern, hot observables are easier to understand than the cold ones.

Cold observable is also like an an event, but with a twist - Cold observable's event is not a property on a shared instance, it is a property on an object that is produced from a factory each time when somebody subscribes. In addition, subscription starts the production of the values. Because of the above, multiple subscribers are isolated and each receives its own set of values.

The most common mistake RX beginners make is creating a cold observable (well, thinking they are creating a cold observable) using some state variables within a function (f.e. accumulated total) and not wrapping it into a .Defer() statement. As a result, multiple subscribers share these variables and cause side effects between them.

Gardal answered 6/6, 2011 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.