SlideShare a Scribd company logo
Functional Reactive 
Programming in JS 
Mario Zupan 
@mzupzup 
Stefan Mayer 
@stefanmayer13
Who are we? 
@stefanmayer13 @mzupzup 
2
Motivation 
■ Technology stack re-evaluation 
■ Lessons learned 
■ Functional Programming 
■ QCon NYC 
[1] 
3
What is Functional Reactive 
Programming? 
4
Reactive Manifesto 
[2] 
5
Reactive Manifesto 
? ? 
? ? [2] 
6
Functional Programming 
■ Evaluation of mathematical functions 
■ Avoid mutable state 
■ Referential transparency 
■ Avoid side-effects 
■ Reusable functions over reusable object 
■ Function composition over object 
composition 
[1] 
7
Functional Programming 
■ map 
■ filter 
■ mergeAll 
■ reduce 
■ zip 
[3] 
[4] 8
var data = [1, 2, 3, 4, 5]; 
var numbers = data.map(function (nr) { 
return nr + 1; 
}); 
// numbers = [2, 3, 4, 5, 6] 
9
var data = [1, 2, 3, 4, 5, 6, 7]; 
var numbers = data.filter(function (nr) { 
return nr % 2 === 0; 
}); 
// numbers = [2, 4, 6] 
10
var data = [1, 2, 3, 4, 5, 6, 7]; 
var numbers = data.map(function (nr) { 
return nr + 1; 
}).filter(function (nr) { 
return nr % 2 === 0; 
}); 
// numbers = [2, 4, 6, 8] 
11
var data = [[1, 2], [3, 4], 5, [6], 7, 8]; 
var numbers = data.mergeAll(); 
// numbers = [1, 2, 3, 4, 5, 6, 7, 8] 
12
var data = [{ 
numbers: [1, 2] 
}, { 
numbers: [3, 4] 
}]; 
var numbersFlatMap = data.flatMap(function (object) { 
return object.numbers; 
}); 
// numbersMap = [[1, 2], [3, 4]] 
// numbersFlatMap = [1, 2, 3, 4] 
13
var data = [1, 2, 3, 4]; 
var sum = data.reduce(function(acc, value) { 
return acc + value; 
}); 
// sum = 10 
14
var data = [5, 7, 3, 4]; 
var min = data.reduce(function(acc, value) { 
return acc < value ? acc : value; 
}); 
// min = 3 
15
var array1 = [1, 2, 3]; 
var array2 = [4, 5, 6]; 
var array = Array.zip(array1, array2, 
function(left, right) { 
return [left, right]; 
}); 
// array = [[1, 4], [2, 5], [3, 6]] 
16
Reactive Programming 
■ Asynchronous data streams 
■ Everything is a stream 
● click events 
● user inputs 
● data from a server 
■ streams rock! 
17
Reactive Programming 
[5] 
18
F + R + P 
■ Powerful composition and aggregation of 
streams 
■ Good fit for concurrent and event-driven 
systems 
■ Declarative 
■ Easy to test 
[6] 
19
Observables 
■ Stream of data over time 
■ Hot vs cold observables 
■ Asynchronous 
■ Lazy 
■ queryable, bufferable, pausable
 
■ more than 120 operations 
[7] 
20
Observable Creation 
Rx.Observable.fromArray([1, 2, 3]); 
Rx.Observable.fromEvent(input, 'click'); 
Rx.Observable.fromEvent(eventEmitter, 'data', fn); 
Rx.Observable.fromCallback(fs.exists); 
Rx.Observable.fromNodeCallback(fs.exists); 
Rx.Observable.fromPromise(somePromise); 
Rx.Observable.fromIterable(function*() {yield 20}); 
21
Observable Basics 
var range = Rx.Observable.range(1, 3); // 1, 2, 3 
var range = range.subscribe( 
function(value) {}, 
function(error) {}, 
function() {} 
); 
optional 
22
Observable Basics 
var range = Rx.Observable.range(1, 10) // 1, 2, 3 ... 
.filter(function(value) { return value % 2 === 0; }) 
.map(function(value) { return "<span>" + value + "</span>"; }) 
.takeLast(1); 
var subscription = range.subscribe( 
function(value) { console.log("last even value: " + value); }); 
// "last even value: <span>10</span>" 
23
Cold Observables 
[8] 
24
Hot Observables 
[9] 
25
Autocomplete 
● Multiple requests 
● Async results 
● Race conditions 
● State 
● ... 
[10] 
26
Autocomplete 1/2 
var keyup = Rx.Observable.fromEvent(input, 'keyup') 
.map(function (e) { 
return e.target.value; // map to text 
}) 
.filter(function (input) { 
return input.length > 2; // filter relevant values 
}) 
.debounce(250) 
27
Autocomplete 2/2 
.distinctUntilChanged() // only if changes 
.flatMapLatest(doAsyncSearch() // do async search on server 
.retry(3)) 
.takeUntil(cancelStream) // cancel stream 
.subscribe( 
function (data) { // do UI stuff }, 
function (error) { // do error handling } 
); 
28
Drag & Drop 1/2 
var mousedown = Rx.Observable.fromEvent(dragTarget, 'mousedown'); 
var mousemove = Rx.Observable.fromEvent(document, 'mousemove'); 
var mouseup = Rx.Observable.fromEvent(dragTarget, 'mouseup'); 
[11] 
29
mousedown.flatMap(function (md) { 
// get starting coordinates 
var startX = md.offsetX, startY = md.offsetY; 
return mousemove.map(function (mm) { 
// return the mouse distance from start 
return {left: mm.clientX - startX, top: mm.clientY - startY }; 
}).takeUntil(mouseup); 
}).subscribe(function (pos) { 
// do UI stuff 
}); [11] 
30
Some Cool Stuff on Observables 
.bufferWithTime(500) 
.pausable(pauser), .pausableBuffered(..) 
.repeat(3) 
.skip(1), skipUntilWithTime(..) 
.do() // for side-effects like logging 
.onErrorResumeNext(second) // resume with other obs 
.window() // project into windows 
.timestamp() // add time for each value 
.delay() 31
RxJS 
Supported 
■ IE6+ 
■ Chrome 4+ 
■ FireFox 1+ 
■ Node.js v0.4+ 
Size (minified & gzipped): 
■ all - 23,1k 
■ lite - 13,5k 
■ compat - 12,5k 
■ ES5 core - 12k 
[12] 
32
Framework Bridges 
■ AngularJS 
■ ReactJS 
■ jQuery 
■ ExtJS 
■ NodeJS 
■ Backbone 
■ ... 
33
Companies using Rx in Production 
[13] 
34
Alternatives to RxJS 
■ BaconJS 
■ Kefir 
■ (Elm) 
35
Conclusion 
■ There is a learning curve 
■ Great abstraction for async & events 
■ Improves 
● Readability 
● Reusability 
● Scalability 
■ Both on the front- and backend 
36
Learning RxJS 
■ RxKoans 
○ https://github.com/Reactive-Extensions/RxJSKoans 
■ learnRx 
○ https://github.com/jhusain/learnrx 
■ The Introduction to Reactive Programming you've been 
missing 
○ https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 
■ rxMarbles 
○ http://rxmarbles.com/ 
37
Image references 
■ 1 - http://www.ylies.fr/ 
■ 2 - http://www.reactivemanifesto.org 
■ 3 - http://reactivex.io/ 
■ 4 - https://raw.githubusercontent.com/wiki/ReactiveX/RxJava/ 
■ 5 - http://buildstuff14.sched.org/event/9ead0e99b3c1c0edddec6c7c8d526125#.VHEgq5PF-kQ 
■ 6 - http://www.cclscorp.com 
■ 7 - http://www.pharmmd.com/ 
■ 8 - http://blogs.msdn.com/ 
■ 9 - http://blogs.msdn.com/ 
■ 10 - https://www.google.at 
■ 11 - http://dockphp.com/ 
■ 12 - http://www.thechrisyates.com/ 
■ 13 - http://www.reactivex.io 
■ 14 - http://www.quickmeme.com/ 
38
Thank you 
@stefanmayer13 
@mzupzup 
[14] 
39

More Related Content

PDF
Redux Sagas - React Alicante
Ignacio MartĂ­n
 
PDF
Introduction to RxJS
Brainhub
 
PDF
RxJS Evolved
trxcllnt
 
PDF
WebAssembly Overview
Alexandr Skachkov
 
PDF
Sagas Middleware Architecture
Mateusz Bosek
 
PPTX
The redux saga begins
Daniel Franz
 
PPTX
React workshop
Imran Sayed
 
PPTX
Rxjs ppt
Christoffer Noring
 
Redux Sagas - React Alicante
Ignacio MartĂ­n
 
Introduction to RxJS
Brainhub
 
RxJS Evolved
trxcllnt
 
WebAssembly Overview
Alexandr Skachkov
 
Sagas Middleware Architecture
Mateusz Bosek
 
The redux saga begins
Daniel Franz
 
React workshop
Imran Sayed
 
Rxjs ppt
Christoffer Noring
 

What's hot (20)

PPTX
Rxjs ngvikings
Christoffer Noring
 
PDF
WebAssembly Demystified
Jay Phelps
 
PDF
[MOPCON 2022] 仄 Kotlin Multiplatform ćˆ¶éœžć…šćčłć°
Shengyou Fan
 
PDF
WebRTC multistream
mganeko
 
PPTX
Introduction to RxJS
Abul Hasan
 
PDF
C#æŹĄäž–ä»ŁéžćŒæœŸć‡Šç†æŠ‚èŠł - Task vs Reactive Extensions
Yoshifumi Kawai
 
PDF
RxJS Operators - Real World Use Cases (FULL VERSION)
Tracy Lee
 
PDF
Ngrx slides
Christoffer Noring
 
PPTX
React js programming concept
Tariqul islam
 
PPTX
React hooks
Ramy ElBasyouni
 
PPTX
Intro to React
Eric Westfall
 
PDF
Understanding react hooks
Samundra khatri
 
PDF
Quarkus - a next-generation Kubernetes Native Java framework
SVDevOps
 
PPTX
Node.js Express
Eyal Vardi
 
PPTX
React hooks
Sadhna Rana
 
PDF
안정적읞 서ëč„슀 욎영 2014.03
Changyol BAEK
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PPTX
Intro to React
Justin Reock
 
PDF
React js use contexts and useContext hook
Piyush Jamwal
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
Rxjs ngvikings
Christoffer Noring
 
WebAssembly Demystified
Jay Phelps
 
[MOPCON 2022] 仄 Kotlin Multiplatform ćˆ¶éœžć…šćčłć°
Shengyou Fan
 
WebRTC multistream
mganeko
 
Introduction to RxJS
Abul Hasan
 
C#æŹĄäž–ä»ŁéžćŒæœŸć‡Šç†æŠ‚èŠł - Task vs Reactive Extensions
Yoshifumi Kawai
 
RxJS Operators - Real World Use Cases (FULL VERSION)
Tracy Lee
 
Ngrx slides
Christoffer Noring
 
React js programming concept
Tariqul islam
 
React hooks
Ramy ElBasyouni
 
Intro to React
Eric Westfall
 
Understanding react hooks
Samundra khatri
 
Quarkus - a next-generation Kubernetes Native Java framework
SVDevOps
 
Node.js Express
Eyal Vardi
 
React hooks
Sadhna Rana
 
안정적읞 서ëč„슀 욎영 2014.03
Changyol BAEK
 
JavaScript Promises
L&T Technology Services Limited
 
Intro to React
Justin Reock
 
React js use contexts and useContext hook
Piyush Jamwal
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
Ad

Viewers also liked (16)

PPTX
NDC14 - Rx와 Functional Reactive ProgrammingìœŒëĄœ êł ì„±ëŠ„ 서ëȄ 만듀Ʞ
Jong Wook Kim
 
PDF
[1B4]á„‹á…Ąá†«á„ƒá…łá„…á…©á„‹á…”á„ƒá…ł 도ᆌᄉᅔᄉᅄᆌ_á„‘á…łá„…á…©á„€á…łá„…á…ąá„†á…”á†Œ
NAVER D2
 
PDF
Functional Reactive Programming With RxSwift
선협 읎
 
PDF
Cascadia.js: Don't Cross the Streams
mattpodwysocki
 
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
PDF
서ëȄ 개발자가 바띌 ëłž Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform
 
PDF
혁신적읞 ì›čì»ŽíŹë„ŒíŠž ëŒìŽëžŒëŸŹëŠŹ - Polymer
Jae Sung Park
 
PDF
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
PDF
Module, AMD, RequireJS
ć‰æ Œ 高
 
PDF
Angular2 ecosystem
Kamil Lelonek
 
PDF
System webpack-jspm
Jesse Warden
 
PDF
Compose Async with RxJS
Kyung Yeol Kim
 
PPTX
React in Native Apps - Meetup React - 20150409
Minko3D
 
PDF
마á†ș먼 á„‡á…©á„Œá…Ą ᄋᅹᆚᄐᅄ á„†á…©á„ƒá…Šá†Żá„‹á…”á„…á…Ąá†«
jbugkorea
 
PDF
React JS and why it's awesome
Andrew Hull
 
PDF
LetSwift RxSwift á„‰á…”á„Œá…Ąá†šá„’á…Ąá„€á…”
Wanbok Choi
 
NDC14 - Rx와 Functional Reactive ProgrammingìœŒëĄœ êł ì„±ëŠ„ 서ëȄ 만듀Ʞ
Jong Wook Kim
 
[1B4]á„‹á…Ąá†«á„ƒá…łá„…á…©á„‹á…”á„ƒá…ł 도ᆌᄉᅔᄉᅄᆌ_á„‘á…łá„…á…©á„€á…łá„…á…ąá„†á…”á†Œ
NAVER D2
 
Functional Reactive Programming With RxSwift
선협 읎
 
Cascadia.js: Don't Cross the Streams
mattpodwysocki
 
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
서ëȄ 개발자가 바띌 ëłž Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform
 
혁신적읞 ì›čì»ŽíŹë„ŒíŠž ëŒìŽëžŒëŸŹëŠŹ - Polymer
Jae Sung Park
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
Module, AMD, RequireJS
ć‰æ Œ 高
 
Angular2 ecosystem
Kamil Lelonek
 
System webpack-jspm
Jesse Warden
 
Compose Async with RxJS
Kyung Yeol Kim
 
React in Native Apps - Meetup React - 20150409
Minko3D
 
마á†ș먼 á„‡á…©á„Œá…Ą ᄋᅹᆚᄐᅄ á„†á…©á„ƒá…Šá†Żá„‹á…”á„…á…Ąá†«
jbugkorea
 
React JS and why it's awesome
Andrew Hull
 
LetSwift RxSwift á„‰á…”á„Œá…Ąá†šá„’á…Ąá„€á…”
Wanbok Choi
 
Ad

Similar to Functional Reactive Programming with RxJS (20)

PDF
Functional Reactive Programming in JavaScript
zupzup.org
 
PDF
RxJava on Android
Dustin Graham
 
PDF
ă‚ȘăƒŒăƒ—ăƒłăƒ‡ăƒŒă‚żă‚’äœżăŁăŸăƒąăƒă‚€ăƒ«ă‚ąăƒ—ăƒȘ開ç™șïŒˆćżœç”šç·šïŒ‰
Takayuki Goto
 
PDF
SF Big Analytics 20191112: How to performance-tune Spark applications in larg...
Chester Chen
 
PDF
"ĐĐ”ĐŒĐœĐŸĐłĐŸ ĐŸ Ń„ŃƒĐœĐșŃ†ĐžĐŸĐœĐ°Đ»ŃŒĐœĐŸĐŒ ĐżŃ€ĐŸĐłŃ€Đ°ĐŒĐŒĐžŃ€ĐŸĐČĐ°ĐœĐžĐ” ĐČ JavaScript" АлДĐșсДĐč ĐšĐŸĐČĐ°Đ»Đ”ĐœĐșĐŸ
Fwdays
 
PDF
How to practice functional programming in react
Netta Bondy
 
PPTX
ECMA5 and ES6 Promises
Oswald Campesato
 
PDF
Go Says WAT?
jonbodner
 
PDF
LSFMM 2019 BPF Observability
Brendan Gregg
 
PDF
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
PDF
Douglas Crockford: Serversideness
WebExpo
 
PPS
CS101- Introduction to Computing- Lecture 35
Bilal Ahmed
 
PDF
JavaScript 2016 for C# Developers
Rick Beerendonk
 
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly
 
PPTX
Workshop 1: Good practices in JavaScript
Visual Engineering
 
PDF
ECMAScript 6
WebF
 
PDF
JS Fest 2019. Anjana Vakil. Serverless Bebop
JSFestUA
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
PDF
The mighty js_function
timotheeg
 
Functional Reactive Programming in JavaScript
zupzup.org
 
RxJava on Android
Dustin Graham
 
ă‚ȘăƒŒăƒ—ăƒłăƒ‡ăƒŒă‚żă‚’äœżăŁăŸăƒąăƒă‚€ăƒ«ă‚ąăƒ—ăƒȘ開ç™șïŒˆćżœç”šç·šïŒ‰
Takayuki Goto
 
SF Big Analytics 20191112: How to performance-tune Spark applications in larg...
Chester Chen
 
"ĐĐ”ĐŒĐœĐŸĐłĐŸ ĐŸ Ń„ŃƒĐœĐșŃ†ĐžĐŸĐœĐ°Đ»ŃŒĐœĐŸĐŒ ĐżŃ€ĐŸĐłŃ€Đ°ĐŒĐŒĐžŃ€ĐŸĐČĐ°ĐœĐžĐ” ĐČ JavaScript" АлДĐșсДĐč ĐšĐŸĐČĐ°Đ»Đ”ĐœĐșĐŸ
Fwdays
 
How to practice functional programming in react
Netta Bondy
 
ECMA5 and ES6 Promises
Oswald Campesato
 
Go Says WAT?
jonbodner
 
LSFMM 2019 BPF Observability
Brendan Gregg
 
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
Douglas Crockford: Serversideness
WebExpo
 
CS101- Introduction to Computing- Lecture 35
Bilal Ahmed
 
JavaScript 2016 for C# Developers
Rick Beerendonk
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly
 
Workshop 1: Good practices in JavaScript
Visual Engineering
 
ECMAScript 6
WebF
 
JS Fest 2019. Anjana Vakil. Serverless Bebop
JSFestUA
 
JavaScript Growing Up
David Padbury
 
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
The mighty js_function
timotheeg
 

Recently uploaded (20)

PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂșnior
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Architecture of the Future (09152021)
EdwardMeyman
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Software Development Methodologies in 2025
KodekX
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂșnior
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 

Functional Reactive Programming with RxJS

  • 1. Functional Reactive Programming in JS Mario Zupan @mzupzup Stefan Mayer @stefanmayer13
  • 2. Who are we? @stefanmayer13 @mzupzup 2
  • 3. Motivation ■ Technology stack re-evaluation ■ Lessons learned ■ Functional Programming ■ QCon NYC [1] 3
  • 4. What is Functional Reactive Programming? 4
  • 6. Reactive Manifesto ? ? ? ? [2] 6
  • 7. Functional Programming ■ Evaluation of mathematical functions ■ Avoid mutable state ■ Referential transparency ■ Avoid side-effects ■ Reusable functions over reusable object ■ Function composition over object composition [1] 7
  • 8. Functional Programming ■ map ■ filter ■ mergeAll ■ reduce ■ zip [3] [4] 8
  • 9. var data = [1, 2, 3, 4, 5]; var numbers = data.map(function (nr) { return nr + 1; }); // numbers = [2, 3, 4, 5, 6] 9
  • 10. var data = [1, 2, 3, 4, 5, 6, 7]; var numbers = data.filter(function (nr) { return nr % 2 === 0; }); // numbers = [2, 4, 6] 10
  • 11. var data = [1, 2, 3, 4, 5, 6, 7]; var numbers = data.map(function (nr) { return nr + 1; }).filter(function (nr) { return nr % 2 === 0; }); // numbers = [2, 4, 6, 8] 11
  • 12. var data = [[1, 2], [3, 4], 5, [6], 7, 8]; var numbers = data.mergeAll(); // numbers = [1, 2, 3, 4, 5, 6, 7, 8] 12
  • 13. var data = [{ numbers: [1, 2] }, { numbers: [3, 4] }]; var numbersFlatMap = data.flatMap(function (object) { return object.numbers; }); // numbersMap = [[1, 2], [3, 4]] // numbersFlatMap = [1, 2, 3, 4] 13
  • 14. var data = [1, 2, 3, 4]; var sum = data.reduce(function(acc, value) { return acc + value; }); // sum = 10 14
  • 15. var data = [5, 7, 3, 4]; var min = data.reduce(function(acc, value) { return acc < value ? acc : value; }); // min = 3 15
  • 16. var array1 = [1, 2, 3]; var array2 = [4, 5, 6]; var array = Array.zip(array1, array2, function(left, right) { return [left, right]; }); // array = [[1, 4], [2, 5], [3, 6]] 16
  • 17. Reactive Programming ■ Asynchronous data streams ■ Everything is a stream ● click events ● user inputs ● data from a server ■ streams rock! 17
  • 19. F + R + P ■ Powerful composition and aggregation of streams ■ Good fit for concurrent and event-driven systems ■ Declarative ■ Easy to test [6] 19
  • 20. Observables ■ Stream of data over time ■ Hot vs cold observables ■ Asynchronous ■ Lazy ■ queryable, bufferable, pausable
 ■ more than 120 operations [7] 20
  • 21. Observable Creation Rx.Observable.fromArray([1, 2, 3]); Rx.Observable.fromEvent(input, 'click'); Rx.Observable.fromEvent(eventEmitter, 'data', fn); Rx.Observable.fromCallback(fs.exists); Rx.Observable.fromNodeCallback(fs.exists); Rx.Observable.fromPromise(somePromise); Rx.Observable.fromIterable(function*() {yield 20}); 21
  • 22. Observable Basics var range = Rx.Observable.range(1, 3); // 1, 2, 3 var range = range.subscribe( function(value) {}, function(error) {}, function() {} ); optional 22
  • 23. Observable Basics var range = Rx.Observable.range(1, 10) // 1, 2, 3 ... .filter(function(value) { return value % 2 === 0; }) .map(function(value) { return "<span>" + value + "</span>"; }) .takeLast(1); var subscription = range.subscribe( function(value) { console.log("last even value: " + value); }); // "last even value: <span>10</span>" 23
  • 26. Autocomplete ● Multiple requests ● Async results ● Race conditions ● State ● ... [10] 26
  • 27. Autocomplete 1/2 var keyup = Rx.Observable.fromEvent(input, 'keyup') .map(function (e) { return e.target.value; // map to text }) .filter(function (input) { return input.length > 2; // filter relevant values }) .debounce(250) 27
  • 28. Autocomplete 2/2 .distinctUntilChanged() // only if changes .flatMapLatest(doAsyncSearch() // do async search on server .retry(3)) .takeUntil(cancelStream) // cancel stream .subscribe( function (data) { // do UI stuff }, function (error) { // do error handling } ); 28
  • 29. Drag & Drop 1/2 var mousedown = Rx.Observable.fromEvent(dragTarget, 'mousedown'); var mousemove = Rx.Observable.fromEvent(document, 'mousemove'); var mouseup = Rx.Observable.fromEvent(dragTarget, 'mouseup'); [11] 29
  • 30. mousedown.flatMap(function (md) { // get starting coordinates var startX = md.offsetX, startY = md.offsetY; return mousemove.map(function (mm) { // return the mouse distance from start return {left: mm.clientX - startX, top: mm.clientY - startY }; }).takeUntil(mouseup); }).subscribe(function (pos) { // do UI stuff }); [11] 30
  • 31. Some Cool Stuff on Observables .bufferWithTime(500) .pausable(pauser), .pausableBuffered(..) .repeat(3) .skip(1), skipUntilWithTime(..) .do() // for side-effects like logging .onErrorResumeNext(second) // resume with other obs .window() // project into windows .timestamp() // add time for each value .delay() 31
  • 32. RxJS Supported ■ IE6+ ■ Chrome 4+ ■ FireFox 1+ ■ Node.js v0.4+ Size (minified & gzipped): ■ all - 23,1k ■ lite - 13,5k ■ compat - 12,5k ■ ES5 core - 12k [12] 32
  • 33. Framework Bridges ■ AngularJS ■ ReactJS ■ jQuery ■ ExtJS ■ NodeJS ■ Backbone ■ ... 33
  • 34. Companies using Rx in Production [13] 34
  • 35. Alternatives to RxJS ■ BaconJS ■ Kefir ■ (Elm) 35
  • 36. Conclusion ■ There is a learning curve ■ Great abstraction for async & events ■ Improves ● Readability ● Reusability ● Scalability ■ Both on the front- and backend 36
  • 37. Learning RxJS ■ RxKoans ○ https://github.com/Reactive-Extensions/RxJSKoans ■ learnRx ○ https://github.com/jhusain/learnrx ■ The Introduction to Reactive Programming you've been missing ○ https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 ■ rxMarbles ○ http://rxmarbles.com/ 37
  • 38. Image references ■ 1 - http://www.ylies.fr/ ■ 2 - http://www.reactivemanifesto.org ■ 3 - http://reactivex.io/ ■ 4 - https://raw.githubusercontent.com/wiki/ReactiveX/RxJava/ ■ 5 - http://buildstuff14.sched.org/event/9ead0e99b3c1c0edddec6c7c8d526125#.VHEgq5PF-kQ ■ 6 - http://www.cclscorp.com ■ 7 - http://www.pharmmd.com/ ■ 8 - http://blogs.msdn.com/ ■ 9 - http://blogs.msdn.com/ ■ 10 - https://www.google.at ■ 11 - http://dockphp.com/ ■ 12 - http://www.thechrisyates.com/ ■ 13 - http://www.reactivex.io ■ 14 - http://www.quickmeme.com/ 38
  • 39. Thank you @stefanmayer13 @mzupzup [14] 39

Editor's Notes

  • #8: Bevor functional reactive programming -> functional Im Gegensatz zu imperativ wird mit mathematischen funktionen gearbeitet. Instruktionen Mapping Input -> Output immer gleich unabhĂ€ngig vom ausfĂŒhrungszeitpunkt Immutable
  • #9: FP umfasst großes Gebiet
  • #10: ----- Meeting Notes (27/11/14 18:27) ----- Transformieren
  • #14: Numbers Attribut
  • #15: Bisher alle Operationen nur auf 1 Element der Collection
  • #17: Schluss
  • #27: Probleme von suggest erklĂ€ren: Bei Eingaben zuviele requests race conditions by async (spĂ€terer request kann frĂŒher zurĂŒckkommen)
  • #28: Probleme von suggest erklĂ€ren: zuviele requests, statefull, race conditions by async
  • #32: pausable nur auf hot observables