SlideShare a Scribd company logo
Reactive Programming
Buğra Uluyurt
Programming Types
Hardest part while learning Reactive
Programming => THINKING IN REACTIVE
Programming Types
- Imperative (Stateful)
- Declarative - Functional (Stateless)
- Reactive
Programming Types
IMPERATIVE
Imperative programming is a programming paradigm that uses statements that change a
program’s state.
Programming Types
DECLARATIVE
Declarative programming is a programming paradigm … that expresses the logic of a
computation without describing its control flow.
Programming Types
REACTIVE
- Wikipedia => it is like a description in a CS book which is generic and theoretical
- Reactive manifesto => it is so generic for a developer with dirty hands
- Stackoverlow => Look at stackoverflow and never look back to reactive
programming again. Morale killing explanations, hard and most advanced stuff
without explaining its core.
Reactive programming is programming with asynchronous data streams
Nothing new just made easier!
It is like dealing with event streams such as =>
- Event buses
- Click events
Streams are cheap, anything can be a stream:
- Variables,
- User inputs
- Caches etc.
YOU CAN LISTEN THESE STREAMS AND REACT ACCORDINGLY
Can use my regular mapping, filtering?
YES
RX Library provides amazing toolbox of functions to combine,
create and filter those streams
RX uses functional magic too! =>
- A stream can be used as an input to another one
- Multiple streams can be used as inputs to another stream
- Streams can be merged
- Streams can be filtered
- Data values in a particular stream can be mapped from one stream to another
What is at the center of Reactive
Programming
STREAMS
Clicking on a button stream
Streams
A stream is a sequence of ongoing events
ordered in time.
It can emit 3 things =>
- Value (of some type)
- Error
- Completed signal
Consider that the “completed” takes place, for instance, when the current window or
view containing that button is closed
Streams
We capture these emitted values
asynchronously => How? =>
- A function which will invoke after the value is emitted
- A function which will invoke after an error is emitted
- A function which will invoke after completed is emitted
Listening a stream is called => SUBSCRIPTION / SUBSCRIBING
Sometimes last 2 can be omitted. Only value emission is taken into account.
• Functions we are defining are called OBSERVERS.
• Stream is being observed by observers. Therefore it is the subject. That’s
why we call it OBSERVABLE
Streams
--a---b-c---d---X---|->
a, b, c, d are emitted values
X is an error
| is the 'completed' signal
---> is the timeline
Streams
clickStream: ---c----c--c----c------c-->
vvvvv map(c becomes 1) vvvv
---1----1--1----1------1-->
vvvvvvvvv scan(+) vvvvvvvvv
counterStream: ---1----2--3----4------5-->
Each stream has many functions attached to it, such as map, filter, scan, etc. When you
call one of these functions, such as clickStream.map(f), it returns a new stream based
on the click stream.
- Original stream is not modified. It is called immutability.
- Non modified streams allows us to chain functions =>
clickStream.map(mapFn).scan(addFn)
Stream example
• Lets make a stream of “double click” events. Or lets make “triple click”
events!
• Imagine how we would do that in a traditional imperative and stateful
way.
REACTIVE PROGRAMMING filters the implementation to 4 lines of code =>
- Grey boxes are functions
transforming one stream into
another.
- We can then subscribe ("listen")
to it to react accordingly how we
wish
* We can apply the same
operations on different kinds of
streams, on a stream of API
responses
Why should we consider Reactive Programming
• Raises the level of abstraction
Focus on business logic, don’t think about implementation details
Thinking in RP with examples =>
Rx library family is available for => (.NET, Java, Scala, Clojure, JavaScript, Ruby, Python, C++,
Objective-C/Cocoa, Groovy, etc)
http://reactivex.io/languages.html
Through our presentation we will use RxJS
Implementing “Who to follow”
suggestions box
RX Example
1. Doing the request
2. Getting the request
3. Rendering the response
--a------|->
Where a is the string 'https://api.github.com/users'
RX Example
• To create such stream with a single value is
simple in RX. Official terminology for a stream
is => Observable.
RX Example
RX Example
Rx.Observable.create => Creates your own custom stream by explicitly
informing each observer (or in other words, a "subscriber") about data
events (onNext()) or errors (onError()). What we did was just wrap that
jQuery Ajax Promise.
PROMISE IS AN OBSERVABLE => YES! OBSERVABLE IS PROMISE ++
RX Example
In RX we can easily convert Promise to an observable by using
Rx.Observable.fromPromise(promise)
What we have created is stream of streams (metastream)
RX Example
• What we just wanted was a simple stream of users (an Array each time). A
. Not a stream of streams.
requestStream: --a-----b--c------------|->
responseStream: -----A--------B-----C---|->
(lowercase is a request, uppercase is
its response)
RX Example
RX Example
Refresh Button
But something broke => A request doesn’t happen anymore on startup
SOLUTION => Merging requestOnRefreshStream with startupRequestStream
RX Example
stream A: ---a--------e-----o----->
stream B: -----B---C-----D-------->
vvvvvvvvv merge vvvvvvvvv
---a-B---C--e--D--o----->
RX Example
There is a cleaner way of writing it =>
Even cleaner
RX Example
Even more cleaner
Look at startWith from rxjs docs
RX Example
We want ‘on Refresh’ => ‘Clear suggestions’
Modelling Suggestions
RX Example
When rendering, we interpret null as “no-data”, hence hiding its UI element
RX Example
We can empty suggestions during startup
RX Example
Closing suggestions and using cached responses
This DOES NOT WORK
RX Example
Let’s think about it
In RX there is combineLatest operator
RX Example

More Related Content

What's hot

An eclipse client server architecture with asynchronous messaging
An eclipse client server architecture with asynchronous messagingAn eclipse client server architecture with asynchronous messaging
An eclipse client server architecture with asynchronous messaging
Thomas Kratz
 

What's hot (20)

Project Reactor By Example
Project Reactor By ExampleProject Reactor By Example
Project Reactor By Example
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
Demystifying Reactive Programming
Demystifying Reactive ProgrammingDemystifying Reactive Programming
Demystifying Reactive Programming
 
Declarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive ProgrammingDeclarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive Programming
 
Reactive Programming In Java Using: Project Reactor
Reactive Programming In Java Using: Project ReactorReactive Programming In Java Using: Project Reactor
Reactive Programming In Java Using: Project Reactor
 
Understanding Reactive Programming
Understanding Reactive ProgrammingUnderstanding Reactive Programming
Understanding Reactive Programming
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
 
stigbot_beta
stigbot_betastigbot_beta
stigbot_beta
 
Take a Look at Akka+Java (English version)
Take a Look at Akka+Java (English version)Take a Look at Akka+Java (English version)
Take a Look at Akka+Java (English version)
 
Introduction to Akka Streams
Introduction to Akka StreamsIntroduction to Akka Streams
Introduction to Akka Streams
 
Quick Tour On Zeppelin
Quick Tour On ZeppelinQuick Tour On Zeppelin
Quick Tour On Zeppelin
 
An eclipse client server architecture with asynchronous messaging
An eclipse client server architecture with asynchronous messagingAn eclipse client server architecture with asynchronous messaging
An eclipse client server architecture with asynchronous messaging
 
Architectural Patterns - Interactive and Event Handling Patterns
Architectural Patterns  - Interactive and Event Handling PatternsArchitectural Patterns  - Interactive and Event Handling Patterns
Architectural Patterns - Interactive and Event Handling Patterns
 
Conjunctive queries
Conjunctive queriesConjunctive queries
Conjunctive queries
 
Mini-Training: Let's have a rest
Mini-Training: Let's have a restMini-Training: Let's have a rest
Mini-Training: Let's have a rest
 
Introduction to Functional Reactive Programming
Introduction to Functional Reactive ProgrammingIntroduction to Functional Reactive Programming
Introduction to Functional Reactive Programming
 
Mule esb usecase
Mule esb usecaseMule esb usecase
Mule esb usecase
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
CQRS
CQRSCQRS
CQRS
 

Similar to Reactive programming

Reactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka StreamsReactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka Streams
Konrad Malawski
 
rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
Alexander Mostovenko
 

Similar to Reactive programming (20)

Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
 
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwiftSwift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
 
Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJS
 
Reactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka StreamsReactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka Streams
 
DZone_RC_RxJS
DZone_RC_RxJSDZone_RC_RxJS
DZone_RC_RxJS
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
 
IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018IPT Reactive Java IoT Demo - BGOUG 2018
IPT Reactive Java IoT Demo - BGOUG 2018
 
Guide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFluxGuide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFlux
 
How to debug slow lambda response times
How to debug slow lambda response timesHow to debug slow lambda response times
How to debug slow lambda response times
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
 
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Springone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and ReactorSpringone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and Reactor
 

Recently uploaded

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 

Reactive programming

  • 2. Programming Types Hardest part while learning Reactive Programming => THINKING IN REACTIVE Programming Types - Imperative (Stateful) - Declarative - Functional (Stateless) - Reactive
  • 3. Programming Types IMPERATIVE Imperative programming is a programming paradigm that uses statements that change a program’s state.
  • 4. Programming Types DECLARATIVE Declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.
  • 5. Programming Types REACTIVE - Wikipedia => it is like a description in a CS book which is generic and theoretical - Reactive manifesto => it is so generic for a developer with dirty hands - Stackoverlow => Look at stackoverflow and never look back to reactive programming again. Morale killing explanations, hard and most advanced stuff without explaining its core. Reactive programming is programming with asynchronous data streams Nothing new just made easier! It is like dealing with event streams such as => - Event buses - Click events Streams are cheap, anything can be a stream: - Variables, - User inputs - Caches etc. YOU CAN LISTEN THESE STREAMS AND REACT ACCORDINGLY
  • 6. Can use my regular mapping, filtering? YES RX Library provides amazing toolbox of functions to combine, create and filter those streams RX uses functional magic too! => - A stream can be used as an input to another one - Multiple streams can be used as inputs to another stream - Streams can be merged - Streams can be filtered - Data values in a particular stream can be mapped from one stream to another
  • 7. What is at the center of Reactive Programming STREAMS Clicking on a button stream
  • 8. Streams A stream is a sequence of ongoing events ordered in time. It can emit 3 things => - Value (of some type) - Error - Completed signal Consider that the “completed” takes place, for instance, when the current window or view containing that button is closed
  • 9. Streams We capture these emitted values asynchronously => How? => - A function which will invoke after the value is emitted - A function which will invoke after an error is emitted - A function which will invoke after completed is emitted Listening a stream is called => SUBSCRIPTION / SUBSCRIBING Sometimes last 2 can be omitted. Only value emission is taken into account. • Functions we are defining are called OBSERVERS. • Stream is being observed by observers. Therefore it is the subject. That’s why we call it OBSERVABLE
  • 10. Streams --a---b-c---d---X---|-> a, b, c, d are emitted values X is an error | is the 'completed' signal ---> is the timeline
  • 11. Streams clickStream: ---c----c--c----c------c--> vvvvv map(c becomes 1) vvvv ---1----1--1----1------1--> vvvvvvvvv scan(+) vvvvvvvvv counterStream: ---1----2--3----4------5--> Each stream has many functions attached to it, such as map, filter, scan, etc. When you call one of these functions, such as clickStream.map(f), it returns a new stream based on the click stream. - Original stream is not modified. It is called immutability. - Non modified streams allows us to chain functions => clickStream.map(mapFn).scan(addFn)
  • 12. Stream example • Lets make a stream of “double click” events. Or lets make “triple click” events! • Imagine how we would do that in a traditional imperative and stateful way. REACTIVE PROGRAMMING filters the implementation to 4 lines of code =>
  • 13. - Grey boxes are functions transforming one stream into another. - We can then subscribe ("listen") to it to react accordingly how we wish * We can apply the same operations on different kinds of streams, on a stream of API responses
  • 14. Why should we consider Reactive Programming • Raises the level of abstraction Focus on business logic, don’t think about implementation details Thinking in RP with examples => Rx library family is available for => (.NET, Java, Scala, Clojure, JavaScript, Ruby, Python, C++, Objective-C/Cocoa, Groovy, etc) http://reactivex.io/languages.html Through our presentation we will use RxJS
  • 15. Implementing “Who to follow” suggestions box
  • 16. RX Example 1. Doing the request 2. Getting the request 3. Rendering the response --a------|-> Where a is the string 'https://api.github.com/users'
  • 17. RX Example • To create such stream with a single value is simple in RX. Official terminology for a stream is => Observable.
  • 19. RX Example Rx.Observable.create => Creates your own custom stream by explicitly informing each observer (or in other words, a "subscriber") about data events (onNext()) or errors (onError()). What we did was just wrap that jQuery Ajax Promise. PROMISE IS AN OBSERVABLE => YES! OBSERVABLE IS PROMISE ++
  • 20. RX Example In RX we can easily convert Promise to an observable by using Rx.Observable.fromPromise(promise) What we have created is stream of streams (metastream)
  • 21.
  • 22. RX Example • What we just wanted was a simple stream of users (an Array each time). A . Not a stream of streams. requestStream: --a-----b--c------------|-> responseStream: -----A--------B-----C---|-> (lowercase is a request, uppercase is its response)
  • 24. RX Example Refresh Button But something broke => A request doesn’t happen anymore on startup SOLUTION => Merging requestOnRefreshStream with startupRequestStream
  • 25. RX Example stream A: ---a--------e-----o-----> stream B: -----B---C-----D--------> vvvvvvvvv merge vvvvvvvvv ---a-B---C--e--D--o----->
  • 26. RX Example There is a cleaner way of writing it => Even cleaner
  • 27. RX Example Even more cleaner Look at startWith from rxjs docs
  • 28. RX Example We want ‘on Refresh’ => ‘Clear suggestions’ Modelling Suggestions
  • 29. RX Example When rendering, we interpret null as “no-data”, hence hiding its UI element
  • 30. RX Example We can empty suggestions during startup
  • 31. RX Example Closing suggestions and using cached responses This DOES NOT WORK
  • 32. RX Example Let’s think about it In RX there is combineLatest operator

Editor's Notes

  1. The map(f) function replaces (into the new stream) each emitted value according to a function f you provide. In our case, we mapped to the number 1 on each click. The scan(g) function aggregates all previous values on the stream, producing value x = g(accumulated, current), where g was simply the add function in this example. Then, counterStream emits the total number of clicks whenever a click happens.