Hot vs Cold Observables
Asked Answered
A

2

11

In RxJS hot observables are observables which use an external producer, but a cold observable use a local producer (see e.g. RxJS Hot vs Cold Observable by Ben Lesh).

Angular HttpClient.post uses cold observables to send data and repeats whenever you make a call.

Is there any way in Angular to know whether a specific method uses a hot or cold observable?

Aerophagia answered 6/9, 2017 at 15:50 Comment(2)
Possible duplicate of Difference between Cold or Hot http requests?Lateshalatest
I don't think so and I believe it doesn't really matter in practise to be honest. Just follow the common sense :). For example as you said HttpClient.post is cold but ActivatedRoute.params is a hot Observable which makes sense (it's an instance of Subject deep inside, that needs to exist even if you're not subscribed to it).Tripper
N
13

No. Documentation is the safest bet. Also, I disagree with @martin's comment, it absolutely does matter. You need to be careful with cold observables to avoid resubscribing and reissuing expensive operations (e.g. by using multicasting or saving off the result to a subject).

You also have to rely on the documentation to know when/how an observable completes. For example, you don't need to ever worry about unsubscribing from HttpClient.post because you know it will complete. However, if you're using some kind of wrapper around HttpClient which serves requests via a cached Subject you might not complete anymore. Every component will generate a new subscription and, after the component is destroyed, that subscription will be a reference from the Subject to the component so the component won't be garbage collected and you will end up with a memory leak.

There is no way to programmatically know what kind of Observable you've subscribed to, whether it will complete or not.

In general this is managed both by being smart about completing your observables and by using tools like takeUntil or Subscription to clean up subscriptions to long running non-completing observables or expensive observable workloads.

*EDIT: Actually, to clarify, you need to be careful with all observables, not just cold observables. Hot observables can generate expensive workloads too.

*EDIT2: Update example removing ActivatedRoute as those observables are completed when the component is destroyed.

Nationwide answered 6/9, 2017 at 20:1 Comment(4)
ActivatedRoute observables do complete when the component is destroyed, so you don't have to manually unsubscribe. angular.io/guide/router#observable-parammap-and-component-reuseVesper
@kevin: The article does not state that ActivatedRoute.params ever completes. The entire activated route will be simply garbage collected together with the component, when the component dies. If you kept reference to the injected ActivatedRoute in some long living object, you will emd up with memory leak. The subscription to ActivatedRoute. params will keep reference to the phantom component, although no new values will be emmitedHamfurd
@Hamfurd Correct, I misspoke. But the answer here says that we need to manage any subscriptions to the observable returned from ActivatedRoute.params. I was pointing out that that is incorrect, as stated in the linked docs. Introducing memory leaks by a long-running object holding on to a reference to ActivatedRoute is a completely different issue and has nothing to do with the management of observable subscriptions.Vesper
Ah, this clarification helps. You're right. I'll think of a new example. @KevinBabcockNationwide
B
1

An Observable can have 2 behaviors:

a) when a subscriber subscribe to it, the subscriber receive a set of data. For receiving new data, you need to subscribe again to it.

b) when a subscriber subscribe to it, the subscriber receive data continuously (when data stream change). For receiving new data, you don't need to subscribe again to the observable.

In the case a) we speak about a COLD Observervable;

In the case b) we speak about a HOT Observervable;

check this article

Burner answered 17/1, 2021 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.