Book Image

Reactive Programming in Kotlin

By : Rivu Chakraborty
Book Image

Reactive Programming in Kotlin

By: Rivu Chakraborty

Overview of this book

In today's app-driven era, when programs are asynchronous, and responsiveness is so vital, reactive programming can help you write code that's more reliable, easier to scale, and better-performing. Reactive programming is revolutionary. With this practical book, Kotlin developers will first learn how to view problems in the reactive way, and then build programs that leverage the best features of this exciting new programming paradigm. You will begin with the general concepts of Reactive programming and then gradually move on to working with asynchronous data streams. You will dive into advanced techniques such as manipulating time in data-flow, customizing operators and provider and how to use the concurrency model to control asynchronicity of code and process event handlers effectively. You will then be introduced to functional reactive programming and will learn to apply FRP in practical use cases in Kotlin. This book will also take you one step forward by introducing you to Spring 5 and Spring Boot 2 using Kotlin. By the end of the book, you will be able to build real-world applications with reactive user interfaces as well as you'll learn to implement reactive programming paradigms in Android.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Dedication
Preface

Flowable and Subscriber


Instead of Observer, Flowable uses Subscriber, which is backpressure compatible. However, if you use lambda expressions, then you will not notice any differences. So, why use Subscriber instead of Observer? Because Subscriber supports some extra operations and backpressure. For instance, it can convey how many items it wishes to receive as a message to upstream. Or rather, we can say while using Subscriber; you must specify how many items you want to receive (request) from upstream; if you don't specify it, you will not receive any emissions.

As we already mentioned, using lambda with Subscriber is similar to Observe; this implementation will automatically request an unbounded number of emissions from the upstream. As with our last code, we didn't specify how many emissions we want, but it internally requested unbounded number of emissions, and that's why we received all the items emitted.

So, let's try replacing the previous program with a Subscriber instance:

    fun...