Book Image

Learning RxJava

By : Thomas Nield
Book Image

Learning RxJava

By: Thomas Nield

Overview of this book

RxJava is a library for composing asynchronous and event-based programs using Observable sequences for the JVM, allowing developers to build robust applications in less time. Learning RxJava addresses all the fundamentals of reactive programming to help readers write reactive code, as well as teach them an effective approach to designing and implementing reactive libraries and applications. Starting with a brief introduction to reactive programming concepts, there is an overview of Observables and Observers, the core components of RxJava, and how to combine different streams of data and events together. You will also learn simpler ways to achieve concurrency and remain highly performant, with no need for synchronization. Later on, we will leverage backpressure and other strategies to cope with rapidly-producing sources to prevent bottlenecks in your application. After covering custom operators, testing, and debugging, the book dives into hands-on examples using RxJava on Android as well as Kotlin.
Table of Contents (21 chapters)
Title Page
About the Author
About the Reviewers
Customer Feedback


In this chapter, we covered multicasting using ConnectableObservable and Subject. The biggest takeaway is that Observable operators result in separate streams of events for each Observer that subscribes. If you want to consolidate these multiple streams into a single stream to prevent redundant work, the best way is to call publish() on an Observable to yield ConnectableObservable. You can then manually call connect() to fire emissions once your Observers are set up or automatically trigger a connection using autoConnect() or refCount().

Mutlicasting also enables replaying and caching, so tardy Observers can receive missed emissions. Subjects provide a means to multicast and cache emissions as well, but you should only utilize them if existing operators cannot achieve what you want.

In the next chapter, we will start working with concurrency. This is where RxJava truly shines and is often the selling point of reactive programming.