Does RxJS `share` operator really makes an Observable hot? - rxjs

The documentation of a share operator states that:
As long as there is at least one Subscriber this Observable will be subscribed and emitting data. ... Because the Observable is multicasting it makes the stream hot
I thought that hot observable is the one that starts emitting data without a need to be subscribed to?
Am I wrong?

You've misunderstood what hot and cold observables are.
Cold Observable
In RxJS an observable is cold (also known as unicasted), when there is one source per subscriber. When subscribing to a cold source, the pipeline gets recreated for each subscriber. Note the following: if your pipeline is computation heavy, then you'd be doing that computation everytime a new subscriber subscribes to that source.
Hot Observable
In RxJS an observable is hot (also known as multicasting), when there is one source for all subscribers. When subscribing to a hot source, the pipeline doesn't get recreated for each subscriber. Hot sources are useful for when you don't want to compute the pipeline for every subscriber. The pipeline will be computed once, and then the answer will be sent to all subscribers.
StackBlitz Example showing this in code.

Yes, hot observables emit data even when no subscriber is active just like a websocket or mouse events. They are independent of subscribers.
Cold observables emit data only when subscribed to it, e.g. without subscribing to an obervable wrapping a http request, your http request will never happen.
Using share, your cold observable gets hot, as no new http request will happen but the old response will be remitted.

Related

Does toPromise() unsubscribe from the Observable?

I have not been able to find any normative text to answer this question. I have been using this code pattern (this is in TypeScript in an Angular application):
observeSomethingFun$: Observable<Fun>;
...
async loadsOfFun() {
const fun = await this.observeSomethingFun$.toPromise();
// I now have fun
}
In general, Observables need to be unsubscribed from. This happens automatically when the Angular async pipe is used but what happens in this case? Does toPromise unsubscribe after emitting one value?
If not, how do I unsubscribe manually?
Update:
It turns out #Will Taylor's answer below is correct but my question needs some clarification.
In my case the Observable emits a never-ending stream, unlike for example Angular's HttpClient Observables that complete after emitting one value. So
in my case I would never get past the await statement according to Taylor's answer.
RxJS makes this easy to fix. The correct statement turns out to be:
const fun = await this.observeSomethingFun$.pipe(first()).toPromise();
The RxJS first operator will receive the first value and unsubscribe from the source Observable. it will then send out that value to the toPromise operator
and then complete.
No need to unsubscribe.
Which is convenient, as there is no way to unsubscribe from a promise.
If the Observable completes - the promise will resolve with the last value emitted and all subscribers will automatically be unsubscribed at this point.
If the Observable errors - the promise will reject and all subscribers will automatically be unsubscribed at this point.
However, there are a couple of edge cases with toPromise in which the behavior is not completely clear from the docs.
If the Observable emits one or more values but does not complete or error, the promise will neither resolve or reject. In this case, the promise would hang around in memory, which is worth considering when working with toPromise.
If the Observable completes but does not emit a value, the promise will resolve with undefined.
First of all, thank you for this question and answer, I wrongly assumed toPromise() knew what it was doing in all scenarios and would unsubscribe when that observable completes (even if it is an observable stream)
So I will just say that it doesn't hurt to pipe all of your observables before using .toPromise()
I just went through a big ordeal of stepping through our app for memory leaks and found the above answer by Will to be good. The elaboration on the actual question was exactly the same issue I was running into.
We are stepping through each observable in the app right now and we use either
pipe(take(1)) which is equivalent to pipe(first()).
or we use pipe(takeUntil(this.destroyed)) where this.destroyed.next(true) is called when we destroy our particular component or service.
We use take() to keep our verbiage consistent so we can search for take or takeUntil across various components.
Long story short, yeah you might take a very slight performance hit piping your observables at each instance, but I highly recommend doing so in order to prevent any unwanted app-wide memory leak hunts. Then maybe if you have the time you can step through each one and see where .toPromise() actually unsubscribes correctly for you.

How many "temperatures" are there for a Rx Observable?

All over the Rx.Net literature there are references to what is commonly know as the temperature of an observable.
There are cold observables (like the ones created by Observable.Interval() and similar factory methods), which will create side effects every time that a new Subscription is created.
On the other side of the spectrum there are hot observables (like Subject<T>) which will onboard new subscriptions as they come.
There are also warm observables, like the ones returned by RefCount() which will execute the initialisation every time one subscription is created, but only if there was no other active subscription. The behaviour of these warm observables is explained here by Dave Sexton:
Alternatively, you can call Publish then RefCount to get an IObservable that is shared among multiple consecutive observers. Note that this isn't truly a hot observable - it's more like a warm observable. RefCount makes a single subscription to the underlying observable while there's at least one observer of your query. When your query has no more observers, changing the reference count to 0, the underlying subscription is disposed. If another observer subscribes to your query later, moving the reference count from 0 to 1 again, then RefCount makes a new subscription to the underlying observable, causing subscription side-effects to occur again.
Are there any other temperatures that one should be aware of? Is it possible to obtain programmatically the temperature of an Observable?
Easy question first:
Is it possible to obtain programmatically the temperature of an Observable?
No. Best you can do is subscribe and see what happens.
The observable 'contract' specifies that when you subscribe to an observable you get zero or more OnNext messages, optionally followed by either one OnCompleted or one OnError message. The contract doesn't specify anything about how multiple or earlier/later subscribers are treated, which is what observable 'temperature' is mostly concerned with.
Are there any other temperatures that one should be aware of?
I wouldn't even think of it in such concrete or discrete terms as you have specified.
I think of it in terms of on-subscribe effects: The coldest of observables have all their effects happen on subscribe (like Observable.Return(42)). The hottest of observables have no effects happening on subscribe (new Subject<int>()). In between those two poles is a continuum.
Observable.Interval(TimeSpan.FromMilliseconds(100)) for example will emit a new number every 100 milliseconds. That example, unlike Observable.Return(42), could be mostly 'warmed-over' via .Publish().RefCount(): The first subscriber starts the numbers, but the second subscriber will see the only the latest numbers, not starting from 0. However, if instead of .Publish() you did .Replay(2).RefCount(), then you have some on-subscribe effects going on. Do the Publish and Replay observables have the same 'temperature'?
TL;DR: Don't focus on the classifications that much. Understand the difference between the two and know that some observables have colder properties and some have warmer ones.

How to cache a reactive publisher like Observable?

I'm trying to understand what happens if I cache the result of a method returning a cold Observable? The flow has not been materialized yet, so what does the cache actually contain? I tried to find out using Hazelcast and Spring Boot but couldn't get the cache working.
Edit:
When I say cache not working, I am speaking based on what I see from Hazelcast Management Center. Depending on the cache config (I tried many things), either the cache shows up but no entries, or the cache doesn't show up at all.
Example:
#javax.cache.annotation.CacheResult
Observable<Integer> random() {
// Do I get a new number every time?
return Observable.just(new Random().nextInt());
}
From rx-java wiki (source here):
A cold Observable emits a particular sequence of items, but can begin
emitting this sequence when its Observer finds it to be convenient,
and at whatever rate the Observer desires, without disrupting the
integrity of the sequence. For example if you convert a static
Iterable into an Observable, that Observable will emit the same
sequence of items no matter when it is later subscribed to or how
frequently those items are observed. Examples of items emitted by a
cold Observable might include the results of a database query, file
retrieval, or web request.
With cold Observable, like your example, the request is done at subscribe time, for each subscriber. Even without cache, if you subscribe twice to the same Observable, the request will occur twice. The Observable is not bound to a specific stream. Observable is just a contract describing how to access data.
Caching the result of a method returning an Observable is I think somewhat similar to storing the result to a local property; you just avoid to recreate the Observable object again later. But just the 'getter', not the data.
rx-java give some tools to achieve caching by it's own. You could have a look to Subject or ConnectableObservable.

Why Observable was subscribed more then once

Observable only can be subscribed by one observer . Subject allows values to be multicasted to many Observers but in this example why foo was subscribed twice? Thank you!
enter image description here
Well foo is subscribed twice because you subscribe to it twice. It is a typical confusion for people starting with Rxjs. Subscribing any number of times to certain type of observables (called cold observables), will lead you to repeating the exact same process, which is what is happening here. To get a better understanding of the matter, you can refer to the question which has been dealt here : Hot and Cold observables : are there 'hot' and 'cold' operators?

RxJava in trading engine

I'll write in pseudocode to avoid unnecessary boilerplate. I'm new to Rx but i really want to use it instead of Features and similar stuff...
this is a simplified model of trading engine
we have a which provides all the streams of events (observables) that one might be interested in like
market.updateStream // stream of orderbook update events
we have so called s those will subscribe to requried streams and "react" when any change occures like this
market.updateStream.subscribe(bot1.marketUpdateAction)
market.updateStream.subscribe(bot2.marketUpdateAction)
we might have MANY bots that will subscribe to ONE market and each of those will either start calculating or ignore the change event.
now that we have N bots reacting to 1 event, we need someone to compare their calculations and decide which one is the most profitable also if some bot is slowing down and is exceeding limit of time we are ready to wait, we skip it and proceed to comparision and execution... for that we have a botController which is subscribed to all bots events so that it knows when a bot decided to react to an event... like this
bot1.calculationStream.subscribe(botController.botActivityAction)
bot will in its turn emit 2 different events (calculationStarted and calculationEnded which contains actual result)
when a bot emits event that its started to calculate (this happens if market event is in bot interest so not all bots will emit start event) botController shall do the following, start counting time with the very first bot started event, and await for all bots that emit similar event, if registered bots finish early, comparision starts immidiately...
Sorry if the question is too abstract but i dont really see how do i implement botControllers behaviour with RxJava... Any toughts are appreciated... there are so many rx transformations that i dont really know what can i use there.
UPDATE
Suppose our controller is subscribed to N bots events and each bot can emit 2 events (STARTED, COMPLETED)...
now when controller gets first STARTED event its starts the countdown T, while T time is not expired it will accept new events from bots, when T is expired or all bots return COMPLETED event it does some calculation and returns a single result...
the part i dont understand : rx as far as i know is handling each event in isolation thus the safety from tipical concurrency problems, now that i have several events that are tied to each other i dont see how can i do this using rx... I just need some guidance on this.
I don't fully understand your problem, but here are some design ideas to show you how to think the "Rx-way":
I wouldn't subscribe bots, but rather, they should be a map or flatMap on the update stream, so that they transform the stream of updates into a stream of their answers.
I'd make a BotAnswer class with 4 subclasses: Result, NotInterested, Timeout, BotError.
Then for each bot:
Observable<BotAnswer> bot1Answers = market.updateStream.flatMap(event ->
Observable.just(event).map({ if (event is interesting)
return new Result(doBotCalculations())
else
return new NotInterested()
})
.timeout(T, TimeUnit.SECONDS)
.onErrorResumeNext(
error -> if (error instanceof TimeoutException)
return Observable.just(new Timeout())
else
return Observable.just(new BotError(error))
)
)
And the controller would do a zip on all bot answers:
Observable.zip(bot1Ansers, bot2Answers, ... (a1, a2, ...) -> {
// compare the answers a1, a2, ... do some calculations, return result
})
Here is what i've come up with...
each bot subsribes for market updates,
when market is updated, bot starts computing,
when its finished its emiting completion event,
if its getting termination event from supervisor its stoping computation, if its getting another market update its terminating current execution and starts over...
supervisor subsribes for market updates too,
when its getting one its expecting all bots computation results (no result is also a result :),
if some bot is taking too long its ingnoring him by sending termination event (and counts that bot as returning no result),
as soon as it gets all the bots results its aware of it starts comparing them and emitting the final value to its subscriber,
if it gets another market update while executing it will terminate its computation and start over (waiting for bots)...
Regarding implementation, Bot will manage to handle proper concurrent access to its methods like (start, terminate), as well as Supervisor, eventually those objects ensure no unsafe operation can be done with the object by concurrent execution, using traditional synchronization, but main flow of what triggers what and what happens around is controlled by Rx...
Note that timeout is controlled by supervisor in this case...
Do i miss something here? maybe i'm doing something wrong if i have to use traditional synchronization in conjunction with rx?

Resources