Hot and Cold Observable: RxJava

If you came to this page, I am expecting you to know about observables. If you want to have a look at observables, you can see my previous post here:

Now let’s understand hot and cold observables:

Cold Observable :

Cold Observables are much like a music CD. As you know, a music CD can be replayed to each listener so that each person can hear all the tracks at any time. Similarly, cold Observables will emit the same emissions to each observer, ensuring that all Observers get all the data. Most data-driven Observables are cold, and this includes the Observable.just() and Observable.fromIterable() factories.

Let’s see by a example

Now attach two different observer to it

Let’s see the output:

observer 1 receveied: = 1
observer 1 receveied: = 2
observer 1 receveied: = 3
observer 1 receveied: = 4

Completed emitting streams

observer 2 receveied: = 1
observer 2 receveied: = 2
observer 2 receveied: = 3
observer 2 receveied: = 4

Completed emitting streams

So let’s understand what happens here :

We have two Observers subscribed to one Observable. The Observable will first emit all the emissions to the first observer and then call onComplete(). Then, it will emit all the emissions again to the second observer and call onComplete(). They both receive the same datasets by getting two separate streams each, which is typical behavior for a cold Observable.

Hot Observable :

Let’s understand the hot observable by a radio station example. Let’s say a radio station broadcasting some message, a listener (or observer) comes and starts listening to that message. As the radio station continues broadcasting, a second listener (or observer) arrives and starts listening. Here second listener will miss the previous message that was broadcasted before he came. He will only receive the later part of the message.

Similarly, hot observable will not replay the same emission again to the second observer. Here we have only one stream of data. If the second observer comes, let’s say after 3 seconds, then the second observer will miss the initial emission that was done before 3 seconds and will only listen to later emissions.

Let’s understand this by a example :

Output:

observer 1 receveied: = 0
observer 1 receveied: = 1
observer 1 receveied: = 2
observer 1 receveied: = 3
observer 2 receveied: = 3
observer 1 receveied: = 4
observer 2 receveied: = 4
observer 1 receveied: = 5
observer 2 receveied: = 5
observer 1 receveied: = 6
observer 2 receveied: = 6
observer 1 receveied: = 7
observer 2 receveied: = 7

Here what’s happening, after 3 seconds, both observer listening to the same stream in an interleaved fashion. The second observer misses the starting three emissions.

Now let’s see what publish and connect doing here.

Well publish the operator is used to convert observable to a ConnectableObservable. And ConnectableObservable is an observable source that waits to emit the items until its connect method is called.

It will take any observable, even if it is cold, and make it hot so that all emissions are played to all observers at once. To force each emission to go to all observers simultaneously is known as multicasting.

Working remotely as Android Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store