In this post under RxJava, I will explain the difference between Cold Observable and Hot Observable with the help of java code which will create both Hot and Cold Observable and emits items from both of them. You can compare the output of Hot and Cold Observable to understand there difference.
In my previous post “ConnectableObservale Example” I gave the difference between Cold and Hot Observable. I will recap them here.
They are two kinds of Observables. One is called Hot and another Cold. You will see the difference between them when more than one Observer subscribes to the Observable.
Whenever an Observer subscribes to an Observable, a stream is created. The stream can be shared between all the Observers or each Observer will have their own specific stream.
In case of Cold Observable a unshared stream is created for all the subscribed Observers regardless of when they subscribed. As a result each Observer will get its own copy of the items emitted by the Observable.
In case of Hot Observable a shared stream is created for all the subscribed Observers. As a result each Observer will get single copy of the items emitted by the Observable. If the Observers have subscribed at the same, then all Observers will get all the items emitted by the Observables but if there is gap between the subscription of each Observer. Some Observers may miss some items.
For example if O is an Observable emitting items at an interval of 1 seconds between each emission and two Observers O1 and O2 subscribes to O and their is delay between their subscription of 3 seconds. O1 will receive 1, 2, 3, 4, 5, etc whereas O2 will receive 4, 5, etc.
Main Class
1 import java.util.concurrent.TimeUnit;
2
3 import io.reactivex.rxjava3.core.Observable;
4 import io.reactivex.rxjava3.disposables.Disposable;
5 import io.reactivex.rxjava3.observables.ConnectableObservable;
6
7 public class Example14 {
8 public static void main(String[] args) throws Exception {
9 Example14 example = new Example14();
10 System.out.println("-----------Cold Observable--------------");
11 example.coldObservableExample();
12 System.out.println("-----------Hot Observable-------------");
13 example.hotObservableExample();
14 }
15
16 public void coldObservableExample() throws Exception {
17 Observable<Long> observable = Observable.interval(1, TimeUnit.SECONDS);
18 Disposable disposable1 = observable.subscribe((t) -> System.out.println("Observer1: " + t));
19 Thread.sleep(10000);
20 Disposable disposable2 = observable.subscribe((t) -> System.out.println("Observer2: " + t));
21 Thread.sleep(10000);
22 disposable1.dispose();
23 disposable2.dispose();
24 }
25
26 public void hotObservableExample() throws Exception {
27 Observable<Long> observable = Observable.interval(1, TimeUnit.SECONDS);
28 ConnectableObservable<Long> connectableObservable = observable.publish();
29 connectableObservable.connect();
30 Disposable disposable1 = connectableObservable.subscribe((t) -> System.out.println("Observer1: " + t));
31 Thread.sleep(10000);
32 Disposable disposable2 = connectableObservable.subscribe((t) -> System.out.println("Observer2: " + t));
33 Thread.sleep(10000);
34 disposable1.dispose();
35 disposable2.dispose();
36 }
37 }
In the above code, first Cold Observable will generate items and them Hot Observable.
Each Observable will emit items with a gap of 1 second.
For each Observable we have created two Observers “Observer1” and “Observer2”.
In case of each Observable, Observer1 first subscribes and then Observer2 after a gap of 10 seconds.
If you see the below output, in case of Cold Observable, when Observer2 join the subscription with the Observable, it starts getting items from 0 (i.e., from the beginning). This shows that after subscription with the Cold Observable a new separate stream is created specifically for Observer2.
In case of Hot Observable, when Observer2 join the subscription with the Observable, it starts getting items from 9 and not from the beginning. This shows that after subscription with the Hot Observable, the Observer is connected to existing stream also used by Observer1. So from now on whatever Observer1 gets Observer2 will also get it.
Output
———–Cold Observable————–
Observer1: 0
Observer1: 1
Observer1: 2
Observer1: 3
Observer1: 4
Observer1: 5
Observer1: 6
Observer1: 7
Observer1: 8
Observer1: 9
Observer2: 0
Observer1: 10
Observer1: 11
Observer2: 1
Observer2: 2
Observer1: 12
Observer1: 13
Observer2: 3
Observer2: 4
Observer1: 14
Observer2: 5
Observer1: 15
Observer2: 6
Observer1: 16
Observer2: 7
Observer1: 17
Observer2: 8
Observer1: 18
Observer2: 9
———–Hot Observable————-
Observer1: 0
Observer1: 1
Observer1: 2
Observer1: 3
Observer1: 4
Observer1: 5
Observer1: 6
Observer1: 7
Observer1: 8
Observer1: 9
Observer2: 9
Observer1: 10
Observer2: 10
Observer1: 11
Observer2: 11
Observer1: 12
Observer2: 12
Observer1: 13
Observer2: 13
Observer1: 14
Observer2: 14
Observer1: 15
Observer2: 15
Observer1: 16
Observer2: 16
Observer1: 17
Observer2: 17
Observer1: 18
Observer2: 18
Observer2: 19