Reactive Programming in Kotlin

Book description

Learn how to implement Reactive Programming paradigms with Kotlin, and apply them to web programming with Spring Framework 5.0 and in Android Application Development.

About This Book

  • Learn how to solve blocking user experience with Reactive Programming and get deep insights into RxKotlin
  • Integrate Reactive Kotlin with Spring and build fantastic Android Apps with RxKotlin and RxAndroid
  • Build reactive architectures that reduce complexity throughout the development process and make your apps(web and Android) scalable

Who This Book Is For

This book is for Kotlin developers who would like to build fault-tolerant, scalable, and distributed systems. A basic knowledge of Kotlin is required, but no prior knowledge of reactive programming.

What You Will Learn

  • Learn about reactive programming paradigms and how reactive programming can improve your existing projects
  • Gain in-depth knowledge in RxKotlin 2.0 and the ReactiveX Framework
  • Use RxKotlin with Android
  • Create your own custom operators in RxKotlin
  • Use Spring Framework 5.0 with Kotlin
  • Use the reactor-kotlin extension
  • Build Rest APIs with Spring,Hibernate, and RxKotlin
  • Use testSubscriber to test RxKotlin applications
  • Use backpressure management and Flowables

In Detail

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.

Style and Approach

Loaded with numerous code examples and real-life projects, this book helps you delve into Reactive Programming with Kotlin, and apply it to real-world Spring-web and Android projects, thus making all your apps reactive.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. A Short Introduction to Reactive Programming
    1. What is reactive programming?
    2. Reasons to adapt functional reactive programming
    3. Reactive Manifesto
      1. Reactive Streams standard specifications
      2. Reactive Frameworks for Kotlin
    4. Getting started with RxKotlin
      1. Downloading and setting up RxKotlin
      2. Comparing the pull mechanism with the RxJava push mechanism
      3. The ReactiveEvenOdd program
    5. The ReactiveCalculator project
    6. Summary
  3. Functional Programming with Kotlin and RxKotlin
    1. Introducing functional programming
    2. Fundamentals of functional programming
      1. Lambda expressions
      2. Pure function
      3. High-order functions
      4. Inline functions
    3. Applying functional programming to the ReactiveCalculator class
    4. Coroutines
      1. Getting started with coroutines
      2. Building sequences
      3. The ReactiveCalculator class with coroutines
    5. Functional programming – monads
      1. Single monad
    6. Summary
  4. Observables, Observers, and Subjects
    1. Observables
      1. How Observable works
        1. Understanding the Observable.create method
        2. Understanding the Observable.from methods
        3. Understanding the toObservable extension function
        4. Understanding the Observable.just method
    2. Other Observable factory methods
      1. Subscribers - the Observer interface
      2. Subscribing and disposing
    3. Hot and Cold Observables
      1. Cold Observables
      2. Hot Observables
        1. Introducing the ConnectableObservable object
        2. Subjects
    4. Varieties of Subject
      1. Understanding AsyncSubject
      2. Understanding PublishSubject
      3. Understanding BehaviorSubject
      4. Understanding ReplaySubject
    5. Summary
  5. Introduction to Backpressure and Flowables
    1. Understanding backpressure
    2. Flowable
    3. When to use Flowables and Observables
      1. When to use Flowables?
      2. When to use Observables?
    4. Flowable and Subscriber
    5. Creating Flowable from scratch
    6. Creating Flowable from Observable
    7. BackpressureStrategy.MISSING and onBackpressureXXX()
      1. Operator onBackpressureBuffer()
      2. Operator onBackpressureDrop()
      3. Operator onBackpressureLatest()
    8. Generating Flowable with backpressure at source
    9. ConnectableFlowable
    10. Processor
    11. Learning Buffer, Throttle, and Window operators
      1. The buffer() operator
      2. The window() operator
      3. The throttle() operators
    12. Summary
  6. Asynchronous Data Operators and Transformations
    1. Operator
    2. The filtering/suppressing operators
      1. The debounce operator
      2. The distinct operators – distinct, distinctUntilChanged
      3. The elementAt operator
      4. Filtering emissions - filter operator
      5. The first and last operator
      6. The ignoreElements operator
    3. The transforming operators
      1. The map operator
      2. Casting emissions (cast operator)
      3. The flatMap operator
      4. The defaultIfEmpty operator
      5. The switchIfEmpty operator
      6. The startWith operator
      7. Sorting emissions (sorted operator)
      8. Accumulating data – scan operator
    4. Reducing operators
      1. Counting emissions (count operator)
      2. Accumulating emissions – reduce operator
      3. The collection operators
    5. The error handling operators
    6. The utility operators
    7. Summary
  7. More on Operators and Error Handling
    1. Combining producers (Observable/Flowable)
      1. The startWith operator
      2. Zipping emissions – zip operator
        1. The zipWith operator
      3. The combineLatest operator
      4. Merging Observables/Flowables – merge operator
      5. Concatenating producers (Observable/Flowable)
      6. Ambiguously combining producers
    2. Grouping
    3. flatMap, concatMap – In details
      1. When to use flatMap operator
      2. When to use concatMap operator
    4. Understanding switchMap operator
    5. Skipping and taking emissions
      1. Skipping emissions (skip, skipLast, skipUntil, and skipWhile)
      2. Take operators (take, takeLast, takeWhile, and takeUntil)
    6. The error handling operators
    7. onErrorReturn – return a default value on error
    8. The onErrorResumeNext operator
      1. Retrying on error
    9. An HTTP example
    10. Summary
  8. Concurrency and Parallel Processing in RxKotlin with Schedulers
    1. Introduction to concurrency
    2. Parallel execution versus concurrency
    3. What is a scheduler?
      1. Types of scheduler
        1. Schedulers.io() - I/O bound scheduler
        2. Schedulers.computation() - CPU bound schedulers
        3. Schedulers.newThread()
        4. Schedulers.single()
        5. Schedulers.trampoline()
      2. Schedulers.from
    4. How to use schedulers – subscribeOn and observeOn operators
      1. Changing thread on subscription – subscribeOn operator
      2. Observing on a different thread – observeOn operator
    5. Summary
  9. Testing RxKotlin Applications
    1. Introduction to unit testing and its importance
      1. Why is unit testing so important?
    2. Writing JUnit tests in Kotlin
      1. Testing your code
    3. Testing in RxKotlin
    4. Blocking subscribers
    5. Blocking operators
      1. Getting the first emitted item – blockingFirst()
      2. Getting the only item from single or maybe - blockingGet
      3. Getting the last Item - blockingLast
      4. Getting all emissions as iterable - blockingIterable operator
      5. Looping through all emissions - blockingForEach
    6. Introducing TestObserver and TestSubscriber
    7. Understanding TestScheduler
    8. Summary
  10. Resource Management and Extending RxKotlin
    1. Resource management
    2. Creating your own operators
    3. Composing operators with transformer
    4. Summary
  11. Introduction to Web Programming with Spring for Kotlin Developers
    1. Spring, history, and origin of Spring
      1. The origin and history of Spring
    2. Dependency injection and IoC
      1. Spring Annotation configuration
    3. Spring – AOP
    4. Introduction to Spring Boot
    5. Creating a Rest API with Spring Boot
    6. Summary
  12. REST APIs with Spring JPA and Hibernate
    1. REST API with Spring Boot, Hibernate, and JPA
    2. Reactive programming with Reactor
      1. Add Reactor to your project
      2. Understanding Flux and Mono
    3. Summary
  13. Reactive Kotlin and Android
    1. Setting up Kotlin in Android Studio
    2. Getting started with ToDoApp on Android
    3. Retrofit 2 for API calls
    4. RxKotlin with Retrofit
    5. Making Android events reactive
      1. Introducing RxBinding in Android
    6. Kotlin extensions
    7. Summary

Product information

  • Title: Reactive Programming in Kotlin
  • Author(s): Rivu Chakraborty
  • Release date: December 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788473026