SlideShare a Scribd company logo
+
Reactive
Programming
With RxJava
Mike Nakhimovich
Mnakhimovich@gmail.com
Feb. 23, 2015
+
Tonight’s Agenda
■What is Reactive
■Learn By Example
■Walkthrough sample app
+ What is
Reactive?
+
Reactive Programming
■“Reactive Programming is
programming with
asynchronous data streams.”
(Andre Staltz)
■Data Stream: a sequence of
values
+
What does this mean?
■Programming Model based on the
principle of push rather than pull
■Values are emitted when ready,
not when asked for in a non-
blocking manner
■Allows for actions to be performed
in parallel, rather than in serial
■ie: shopping in person vs online
+
Functional Reactive
Programming
■Takes reactive
programming to the next
level
■Applying functions to the
data stream
■Ex. Map, Filter, Zip, Take
+ Reactive
Extensions
+
ReactiveX
■Collection of helpful functions that
let you do reactive programming
■ReactiveX exists in more than 10
languages (JavaScript, .Net,
Objective-C, etc.)
■RxJava is the Java
implementation of ReactiveX
■Ported by Netflix team
+
Building Blocks of RxJava
■ Observable: source of data stream (sender)
■ Observer: listens for emitted values
(receiver)
■ The Observer subscribes (listens) to the
Observable
■ Observers react to whatever item or
sequence of items the Observable emits
■ Many observers can subscribe to the same
observable
+
Observable Observer Pattern
■Allows for Concurrent Operations:
the observer does not need to
block while waiting for the
observable to emit values
■Observer waits to receive values
when the observable is ready to
emit them
■Based on push rather than pull
+ From Iterable
To Observable
+
Before Observables
■No easy way to perform asynchronous
operations if you needed multiple items
■Observables fill the gap as the ideal
way to access asynchronous
sequences of multiple items
+
Iterable vs. Observable
Interface
■Observable is the
asynchronous/push dual to the
synchronous pull iterable
+
Observables are…
■ Composable: Easily chained together or
combined
■ Flexible: Can be used to emit:
■ A scalar value (network result)
■ Sequence (items in a list)
■ Infinite streams (weather sensor)
■ Free from callback hell: Easy to transform
one asynchronous stream into another
+
Iterable Architecture
■ Before Reactive
1. Call a method
2. Wait for result
3. Store the return value from that method in a
variable
4. Use that variable and its new value to do
something useful
+
Observable Architecture
■ The flow goes like this:
1. Define an Observer that specifies what to
do with each emitted value
2. Call a method that returns an Observable
3. Subscribe the Observer to the Observable.
This tells the Observable that it has a
subscriber waiting to receive values when
they’re available.
+
In RxJava…
■ The Subscribe method connects an
Observer to an Observable
■ Once you Subscribe, no need to block the
thread
■ Values will come to your Observer when they
are ready
+
The Observer
+
Observer Interface
■ OnNext
■ onError
■ onCompleted
+
onNext
■ Observable calls this method whenever the
Observable emits an item.
■ This method can be called any number of
times (zero to many)
■ Always followed by onError or onComplete
(but not both)
+
onError
■ Observable calls this method to indicate
that it has failed to generate the expected
data or has encountered some other error
■ This stops the Observable and it won’t
make further calls.
■ Takes as its parameter an indication of
what caused the error
+
onComplete
■ Observable calls this method after it has
called onNext for the final time and it has not
encountered any errors.
■ A call to onComplete ends the subscription.
+
Why RxJava?
■Schedulers that make threading a
breeze
■Operators that let you transform,
combine, manipulate, and work
with the sequence of items emitted
by Observables
+
Threading Shouldn’t be Hard
+
Schedulers
■ Schedulers are used to manage and control
concurrency
■ observeOn: thread observable is executed on
■ subscribeOn: thread subscribe is executed on
api.users()
.observeOn(AndroidSchedulers.mainThr
ead())
.subscribeOn(Schedulers.io());
+
Available Schedulers
+
Let’s see some example
+
Consuming Observables
+
Consuming Observables
+
doOn Operators
■ Note: if only passing on onNext action (without a doOnError) to
the subscribe method OnErrorNotImplementedException will
be thrown if an error occurs
+
Explicitly Creating Observables
■ You can make an endless Observable by never
calling subscriber.onCompleted()
+
From async to sync
Lifesaver during testing
synchronously test asynchronous data
streams
+
Need a List?
■Be careful with long/infinite streams
+
Combining Observables
+
Map and Flatmap
+
Map
+
Map
+
Transform One Observable into
Another
1. Create on observable from a click event
2. Use flatMap to change that Observable to
another Observable
3. Subscribe to the result of the second
Observable (will emit 3 users)
+
map vs. flatMap
map flatMap
When you transform to
a value
When you transform to
an observable
+
Filtering Observables
+
Filter
+
Filter
+
Debounce/Throttle
+
Debounce/Throttle
▪ No more sending multiple requests when a
user clicks too many times.
+
Error Recovery
+
Recovering from Errors
■ Return a different value in an error case
+
Sample app
walkthrough
https://github.com/Betterment/DaggerStart
er
+
How we use Rx
■ Treat all data as immutable singleton Observable
Stores that know how to self-update and are backed
on disk
■ Since we treat data as a stream, we can return 1 or
more results for each subscription (if a cached value
is available)
■ We can cross subscribe one Store to another
■ RxJava lets us transform and combine data on any
thread and subscribe to updates for related
values/collections
+
Next to Explore
■Cold vs Hot Observables
■Subjects
■Creating Custom Operators
+
Sources/Addl Reading
■Offical Wiki
https://github.com/ReactiveX/RxJava/wiki
■Mastering Observables
http://docs.couchbase.com/developer/java-
2.0/observables.html
■Intro To Functional Reactive Programming
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
+
Questions?

More Related Content

What's hot (20)

Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Reactive Programming and RxJS
Reactive Programming and RxJS
Denis Gorbunov
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka Streams
Kevin Webber
 
Reactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive Way
Roland Kuhn
 
RxJava 2.0 介紹
RxJava 2.0 介紹
Kros Huang
 
Reactive Thinking in Java
Reactive Thinking in Java
Yakov Fain
 
RxJava@Android
RxJava@Android
Maxim Volgin
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
IndicThreads
 
Reactive Programming with Rx
Reactive Programming with Rx
C4Media
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
Async and Await on the Server
Async and Await on the Server
Doug Jones
 
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Flink Forward
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Sync with async
Sync with async
prabathsl
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015
Ben Lesh
 
RxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 
Reactive Programming and RxJS
Reactive Programming and RxJS
Denis Gorbunov
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka Streams
Kevin Webber
 
Reactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive Way
Roland Kuhn
 
RxJava 2.0 介紹
RxJava 2.0 介紹
Kros Huang
 
Reactive Thinking in Java
Reactive Thinking in Java
Yakov Fain
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
IndicThreads
 
Reactive Programming with Rx
Reactive Programming with Rx
C4Media
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
Async and Await on the Server
Async and Await on the Server
Doug Jones
 
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Flink Forward
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Sync with async
Sync with async
prabathsl
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015
Ben Lesh
 
RxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 

Similar to Intro to Functional Programming with RxJava (20)

Introduction to RxJava on Android
Introduction to RxJava on Android
Chris Arriola
 
RxJava@DAUG
RxJava@DAUG
Maxim Volgin
 
Reactive programming with RxAndroid
Reactive programming with RxAndroid
Savvycom Savvycom
 
Rxandroid
Rxandroid
Thinh Thanh
 
RxAndroid
RxAndroid
Thinh Thanh
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
Reactive programming with rx java
Reactive programming with rx java
CongTrung Vnit
 
Streamlining with rx
Streamlining with rx
Akhil Dad
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Reactive Functional Programming with Java 8 on Android N
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
RxJava 2 Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
Reactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
RxJava2 Slides
RxJava2 Slides
YarikS
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Introduction to Reactive programming
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Rxjs swetugg
Rxjs swetugg
Christoffer Noring
 
Reactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
Introduction to RxJava on Android
Introduction to RxJava on Android
Chris Arriola
 
Reactive programming with RxAndroid
Reactive programming with RxAndroid
Savvycom Savvycom
 
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava pour Android : présentation lors du GDG Android Montréal
Sidereo
 
Reactive programming with rx java
Reactive programming with rx java
CongTrung Vnit
 
Streamlining with rx
Streamlining with rx
Akhil Dad
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Reactive Functional Programming with Java 8 on Android N
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Codemotion
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
RxJava 2 Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
Reactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
RxJava2 Slides
RxJava2 Slides
YarikS
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Introduction to Reactive programming
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Reactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
Ad

More from Mike Nakhimovich (7)

meetstore5.pdf
meetstore5.pdf
Mike Nakhimovich
 
Dispatching Reactive State
Dispatching Reactive State
Mike Nakhimovich
 
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Mike Nakhimovich
 
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Mike Nakhimovich
 
Open sourcing the store
Open sourcing the store
Mike Nakhimovich
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
Dispatching Reactive State
Dispatching Reactive State
Mike Nakhimovich
 
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Mike Nakhimovich
 
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Mike Nakhimovich
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
Ad

Recently uploaded (20)

How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
UPDASP a project coordination unit ......
UPDASP a project coordination unit ......
withrj1
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
UPDASP a project coordination unit ......
UPDASP a project coordination unit ......
withrj1
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 

Intro to Functional Programming with RxJava