Chapter 3: What happened when several observers subscribe to the same Observable? Hot Observable Versus Cold Observable

android2ee (Seguy Mathias)
4 min readDec 2, 2020

Cold Observable always emits the same set of data. You should think at it as a book. When you start reading the book, every readers will read the same letters in the same order.

Hot Observable is more live emitting of events. You should think at it as a show. When you come in the room, you join the show at this exact moment, you have lost previous jokes and listen to the current moment.
So we can think at it like:

  • cold observable are emitting data
  • hot observable are emitting event

This concept drives us to the simple question: What happened when several observers subscribe to the same observable. The other question that will follow is related to Threads.

To analyse this concepts, we create two observers: A simple cold observable that emits the list of the days

A second one, more hot style, is a lottery like. Every second a number is picked in 0..1_000, if this number is more than 800, we win and the event is emitted. The loto is over when 5 winners are found.

Now let’s create two observers that will observe those observables.

2 Observers subscribe() to a cold Observable in the same Thread

This will show that:

  • the same set of data is always emitted to each observers.
  • the observers will runs sequentially (first one has to finish before the second one starts) So the code is the following to test this behavior:

The result is clear:

Conclusion

The conclusion are the following:
The subscribe() is blocking in onNext and loop in it until onComplete is called or onError.
The subscribe() is creating a new instance of the observables and rerun it.

2 Observers subscribe() to a hot Observable in the same Thread

For starting to understand what happens, let’s just make it simple: subscribe twice to the hot observable and see what happens.

We obtain the following output:

Conclusion

The first remark is that both observers run sequentially and they don’t receive the same set of data.
The second remark is that hot or cold is a vision of the spirit, because in both cases, the important fact to have in mind, is that subscribe is cloning the Observable and run again its creation method.
The conclusion is the same:
The subscribe() is blocking in onNext and loop in it until onComplete is called or onError.
The subscribe() is creating a new instance of the observables and rerun it. So if it emits events or random events, none observers will receives the same data set.

3 Observers subscribe() to a hot Observable in the different Threads

For starting to understand what happens, let’s just make it simple: subscribe twice to the hot observable and run the Observable in another thread and see what happens.
To switch Thread we will use subscribeOn, we will see in another chapter.

The output is :

hotObserable[Thread]=RxComputationThreadPool-2value is 925
Second Observer value is 925[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 957
First Observer value is 957[Thread] RxComputationThreadPool-1
hotObserable[Thread]=RxComputationThreadPool-2value is 875
Second Observer value is 875[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 995
First Observer value is 995[Thread] RxComputationThreadPool-1
hotObserable[Thread]=RxComputationThreadPool-2value is 992
Second Observer value is 992[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-2value is 911
Second Observer value is 911[Thread] RxComputationThreadPool-2
hotObserable[Thread]=RxComputationThreadPool-1value is 896
First Observer value is 896[Thread] RxComputationThreadPool-1

Conclusion

Two remarks:

  • Both observers receieved different events
  • Both observers receive events in random order, it’s not alternating first/second.

The conclusion is that both observers run in parallel and they still don’t receive the same set of data.
So it’s not a question of timing, subscribe duplicates the observables and create a new instance each time.

ConnectableObservable is the solution to receive from Hot observer the same events

ConnectableObserver is a way to change a “hot observables to a cold one”.
For real, it will wait all the observers to subscribed to it before starting the emission of events. That way all the observers will receive the same set of data.

To change a normal observable to a ConnectableObservable, we just have to add two lines of code:
After the creation of the Observable, we need to call publish.

When the observers have been subscribing to the observable, you call connect on it, it will launch the emission.

Using this pattern, we will receive in both observers the same dataset, sequentially:

Conclusion

Only one instance of the Observable is created.
The onNext method runs the one after the other, in a sequential non blocking way.
Calling connect() is blocking the Thread until the emission is over.

More on the topic

Previous chapter: Chapter 2: Observer

Next chapter:

This chapter is a small part of a biggest training project, you can find on Github. You’ll have the main branch with the answers/responses/unit tests and documentation. You could start with the “questions” branch to only have the questions.

You’ll have the full table of content of the articles on Medium.

You can download the book on Android2ee

Warning: Those articles has been created only because I am stuck in a Rx Android project and I need to prepare the ramp up for those who will replace me.

My conclusion : Choose Kotlin and suspending function instead of Rx. PLEASE, save your project from RxHell !!!

Write Medium in Markdown? Try Markdium!

--

--

android2ee (Seguy Mathias)

Android2ee alias Mathias Seguy. Android expert gives you some news on the Android world. mathias.seguy@android2ee.com