Hot Observables in RxJava: Understanding the Asynchronous Data Streams
Posted By : Bipin Tiwari | 22-Feb-2023
RxJava is a popular library for reactive programming in Java. It provides a comprehensive set of tools for working with asynchronous data streams, allowing us to build efficient and scalable applications.
One of the key concepts in RxJava is the Hot Observable, which is a type of observable that starts emitting items as soon as it is created, irrespective of whether there are any subscribers or not.
What are Hot Observables?
Hot Observables are observables that start emitting items as soon as they are created, regardless of whether there are any subscribers or not. This means that the items emitted by a Hot Observable are not sent directly to its subscribers, but are instead stored in a buffer. Subscribers to the Hot Observable will receive the items from the buffer, but will not receive any items emitted after they have subscribed. In a sense, Hot observable defines events rather than the finite datasets. The events can carry data with them but its time sensitive and late subscribers will miss the already emitted data.
The main difference between Hot Observables and other observables is that Hot Observables are not connected to the lifecycle of their subscribers. This makes them perfect for cases where data is emitted independently of subscribers, such as real-time data streams, event-based systems, and push notifications.
Where to use Hot Observables :
Hot Observables are specialy useful when working with real-time data streams, where the new data is constantly being generated and they needs to be processed as soon as it becomes available. For example, a Hot Observable can be used to process incoming messages from a chat service, allowing new messages to be displayed in real-time as they are received.
Another common use case for Hot Observables is event-based systems, where the events are generated independently of any subscribers. For example, a Hot Observable can be used to process incoming mouse clicks, allowing the application to respond to user input in real-time.
Hot Observables can also be used to handle push notifications, where data is pushed to the application without the need for the application to actively poll for new data. For example, a Hot Observable can be used to process incoming push notifications, allowing the application to display notifications to the user as they are received.
Working with Hot Observables :
RxJava provides a number of operators and methods for working with Hot Observables, including publish, connect, and refCount. These operators and methods allow us to control the lifecycle of Hot Observables and allowing them to start and stop emitting items as we need.
// Here we are using publish to convert cold observable to work as hot observable, it gives a ConnectableObservable object
ConnectableObservable<String> source =
Observable.just("Alpha", "Beta", "Gamma").publish();
//Set up first observer
source.subscribe(s -> System.out.println("Observer 1: " + s));
//Set up second observer
.subscribe(i -> System.out.println("Observer 2: " + i));
//connect is then called on the ConnectableObservable object to start the emissions
Observer 1: Alpha
Observer 2: 5
Observer 1: Beta
Observer 2: 4
Observer 1: Gamma
Observer 2: 5
Here the observers are setup in advance before calling connect and thus print the output in an interleaved mannner. So rather than processing first observer processing all the emissions before second observer, each emissions goes to both of them simultaneously.
Hot Observables are a powerful tool for working with asynchronous data streams in RxJava. They allow us to build applications that can handle real-time data streams, event-based systems, and push notifications etc. By understanding to work with hot and cold observable we can take the advantage of the power of reactive programming in our applications.
Bipin is a highly skilled Android developer with extensive professional experience in creating innovative and efficient mobile applications. He possesses proficiency in programming languages such as Java and Kotlin, along with a deep understanding of Android SDK, design patterns, and best coding practices. Bipin is committed to delivering high-quality code and ensuring exceptional user experiences. With excellent problem-solving skills and the ability to work collaboratively in a team environment, he has made valuable contributions to both client and internal projects, including the development of the Blackbook Travels App and Corniz App.