SlideShare a Scribd company logo
RxJS and Reactive Programming
Ben Lesh
Senior UI Engineer
Netflix Edge Tools & Insights
@benlesh
Observables
Observables
• TC39 proposal to add them to ES2016 (ES7)
• Angular 2 uses them and supports them in a
first class way
• ReactJS will use them and support them in a
first class way
• (Ask me about Ember after :P)
Front End Development
Mostly synchronous or
asynchronous?
We tend to think synchronously
We write code in blocks that are read top to
bottom, left to right.
If this, then this, else this…
But What Are We Doing With Code?
• Handling User Input
• Animations
• AJAX/JSONP
• Web Sockets/SSE
• Updating DOM
Most of these things are ASYNC!
• Handling User Input
• Animations
• AJAX/JSONP
• Web Sockets/SSE
• Updating DOM
Async in JavaScript
usually looks like this
A function that gets called 0 to N times over time,
generally being passed a value
Asynchronous values
Can be represented as collections!
Collections today
We have Iterables
Iterable
Observable
is basically an Iterable turned inside-out
RxJS Observables
RxJS Observables (shorter)
RxJS Observables (with error handling)
In fact, this should look familiar…
But Observables handle more than one value, so
we need to know when they’re done
Observables
Any number of values over any
amount of time
SO WHAT?
We can represent values over time as
a collection… big deal.
Set operations!
• Map
• Reduce
• Filter
• Concat
• Merge
• Zip
• FlatMap
• Take
• Skip
Plus async set operations!
• Map
• Reduce
• Filter
• Concat
• Merge
• Zip
• FlatMap
• Take
• Skip
• Buffer
• Window
• CombineLatest
• Scan
• FlatMapLatest
• WithLatestFrom
Confused Yet? Code Example!
Observables for collections?
I have arrays and iterables!
Arrays already have map, filter,
reduce
Demo day at Netflix!
We hooked it up to prod data and…
So what happened?
Too much array map, filter, reduce!
Array functional programming issues
• Iterating over arrays at each step
• Allocating new arrays at each step
• GC’ing those arrays
Array filter, map, reduce
RxJS Observables to the rescue!
Stream Processing is nice
Observable Stream Processing
• Data is only iterated over once
• No intermediary arrays created
• Less GC
Observable filter, map, reduce
Problem solved
Less array allocation, less GC, less
iterating over arrays
RxJS Observables
Are about to solve another problem
Web Socket Connectivity Issues
• If you walked between buildings you lost the
socket connection
• If you closed your lid for too long, you lost the
socket connection
• If the network hiccupped or went down, you lost
the socket connection
• If the server has an issue or restarts, you lose the
socket connection
• If you’re using 4G and your singal gets too low,
you lose the socket connection
Basically you had to stay very, very still..
Multiplexing Web Sockets
• Connect to the socket
• Send a subscription message for each data
stream you care about
• Group the incoming messages by data stream
• Send an unsubscribe message when you’re
done with a stream
• If all streams are disconnected, close the
socket.
Reconnecting Multiplexed
Web Sockets Is A Pain
• Reconnect the socket
• Resend all subscriptions for previously
subscribed streams
• If you’re offline, wait for an online event,
and reconnect
• Add a delay and retry again (exponential)
• What if the user switches views and needs
different data streams during this?
Observables Are Lazy
• Execute code upon subscription to set up the
underlying data stream
• Execute code upon disposal to teardown the
underlying data stream
So you can do things like
• Setup a web socket on subscription
• Close the web socket on disposal
• Send an AJAX request
• Abort the AJAX request on disposal
• Setup an event listener
• Automatically remove event listeners on
disposal
This is nice…
• Setup a web socket on subscription
• Close the web socket on disposal
Observables can be
retried or repeated
Simply by subscribing
more than once!
Even better!
RxJS has operators:
retry, retryWhen and repeat
Code Example…
Application Development
Is about data flow
How does your data
flow through your app?
• What events cause variables to be produced
• How are those variables used and for what?
• Are you sure you’re containing side-effects?
RxJS makes this easier to reason about
(with practice)
Thinking in streams
• Imagine every variable (not consts) is a stream
• To get it, you need to figure out where it
comes from
• It could come from other variables, or it could
come from an event
• Figure out your side effects, these are the exit
points for your data flow
Thinking in streams
var c = a + b;
// do something with c
Thinking in streams
var cStream = Observable.combineLatest(aStream, bStream,
(a, b) => a + b);
cStream.subscribe(c => {
// do something with c
});
Data Flow Entry and Exit
Common entry points
• User input
• Network I/O
• Properties set
• Events handled
Common exit points
• State
• Persistence
• View Renders
• Emitting events
RxJS entry and exit
observable sources for entries
do() and subscribe() for exits
The idea is to keep
your data flow contained
And control those side effects
Recap
• Any number of values, any amount of time
• Are lazy, don’t do anything until you subscribe
• Embody the set up and tear down of their
underlying data sources
• Can retry/repeat
• Use do() and subscribe() for side-effects
• Observables were designed from the
beginning to be cancellable
Resources
RxJS 2 on GitHub:
https://github.com/Reactive-Extensions/RxJS
Talk Examples:
https://github.com/blesh/rx-talk-may-2015
RxSocketSubject:
https://github.com/blesh/RxSocketSubject
RxJS 3 (coming soon):
https://github.com/ReactiveX/RxJS
Observable Spec:
https://github.com/jhusain/observable-spec
Async Generator Proposal:
https://github.com/jhusain/asyncgenerator
Who to follow
@headinthebox – Erik Meijer (Rx)
@jhusain – Jafar Husain (TC39 Observable)
@mattpodwysocki – Matt Podwysocki (RxJS)
@jeffbcross – Jeff Cross (Angular)
@victorsavkin – Victor Savkin (Angular)
@benlesh – (me)
@benjchristensen – Ben Christensen (RxJava)
Questions? Comments? Corrections?
@benlesh
blesh@netflix.com
ben@benlesh.com
http://github.com/blesh

More Related Content

PDF
SF Front End Developers - Ember + D3
PPTX
The Road To Reactive with RxJava JEEConf 2016
PPTX
Reactive Programming and RxJS
PPTX
RxJS Animations Talk - 2017
PPTX
Advanced RxJS: Animations
PPTX
Top 10 RxJs Operators in Angular
PDF
Non Blocking I/O for Everyone with RxJava
PPTX
RxJava Applied
SF Front End Developers - Ember + D3
The Road To Reactive with RxJava JEEConf 2016
Reactive Programming and RxJS
RxJS Animations Talk - 2017
Advanced RxJS: Animations
Top 10 RxJs Operators in Angular
Non Blocking I/O for Everyone with RxJava
RxJava Applied

What's hot (20)

PDF
Javascript Promises/Q Library
PDF
My Gentle Introduction to RxJS
ODP
Scala Future & Promises
PPTX
Reactive Programming on Android - RxAndroid - RxJava
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
PDF
Introduction to Asynchronous scala
PPTX
Intro to Functional Programming with RxJava
PDF
rx-java-presentation
PDF
Intro to React
PPTX
Rethinking Best Practices
PPTX
Reactive Extensions for JavaScript
PDF
React Development with the MERN Stack
PPTX
Avoiding Callback Hell with Async.js
PPTX
Introduction to es6
PDF
Angular & RXJS: examples and use cases
PDF
Angular Weekend
PDF
RxJava on Android
PDF
RxJava for Android - GDG DevFest Ukraine 2015
PPTX
Reactive Java (33rd Degree)
PDF
The dark side of Akka and the remedy
Javascript Promises/Q Library
My Gentle Introduction to RxJS
Scala Future & Promises
Reactive Programming on Android - RxAndroid - RxJava
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Introduction to Asynchronous scala
Intro to Functional Programming with RxJava
rx-java-presentation
Intro to React
Rethinking Best Practices
Reactive Extensions for JavaScript
React Development with the MERN Stack
Avoiding Callback Hell with Async.js
Introduction to es6
Angular & RXJS: examples and use cases
Angular Weekend
RxJava on Android
RxJava for Android - GDG DevFest Ukraine 2015
Reactive Java (33rd Degree)
The dark side of Akka and the remedy
Ad

Viewers also liked (17)

PPTX
Functional Reactive Programming with RxJS
PDF
Cascadia.js: Don't Cross the Streams
PDF
Functional Reactive Programming With RxSwift
PDF
[1B4]안드로이드 동시성_프로그래밍
PPTX
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
PDF
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
PDF
FalsyValues. Dmitry Soshnikov - ECMAScript 6
PDF
혁신적인 웹컴포넌트 라이브러리 - Polymer
PDF
Module, AMD, RequireJS
PDF
System webpack-jspm
PDF
Angular2 ecosystem
PDF
Compose Async with RxJS
PDF
Closure, Higher-order function in Swift
PPTX
React in Native Apps - Meetup React - 20150409
PDF
맛만 보자 액터 모델이란
PDF
React JS and why it's awesome
PDF
LetSwift RxSwift 시작하기
Functional Reactive Programming with RxJS
Cascadia.js: Don't Cross the Streams
Functional Reactive Programming With RxSwift
[1B4]안드로이드 동시성_프로그래밍
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
FalsyValues. Dmitry Soshnikov - ECMAScript 6
혁신적인 웹컴포넌트 라이브러리 - Polymer
Module, AMD, RequireJS
System webpack-jspm
Angular2 ecosystem
Compose Async with RxJS
Closure, Higher-order function in Swift
React in Native Apps - Meetup React - 20150409
맛만 보자 액터 모델이란
React JS and why it's awesome
LetSwift RxSwift 시작하기
Ad

Similar to RxJS and Reactive Programming - Modern Web UI - May 2015 (20)

PPTX
RxJS In-Depth - AngularConnect 2015
PPTX
Luis Atencio on RxJS
PDF
Rxjs vienna
PPTX
Rxjs marble-testing
PDF
DZone_RC_RxJS
PPTX
Rxjs ngvikings
PDF
Observables in Angular
PPTX
Functional Reactive Programming (FRP): Working with RxJS
PPTX
Angular2 rxjs
PDF
RxJS - The Reactive extensions for JavaScript
PPTX
Rxjs swetugg
PPTX
PPTX
Introduction to RxJS
PDF
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
PDF
rx.js make async programming simpler
PDF
Rxjs kyivjs 2015
PDF
Reactive programming and RxJS
PPTX
Rx – reactive extensions
PDF
RxJava@DAUG
PDF
RxJS101 - What you need to know to get started with RxJS tomorrow
RxJS In-Depth - AngularConnect 2015
Luis Atencio on RxJS
Rxjs vienna
Rxjs marble-testing
DZone_RC_RxJS
Rxjs ngvikings
Observables in Angular
Functional Reactive Programming (FRP): Working with RxJS
Angular2 rxjs
RxJS - The Reactive extensions for JavaScript
Rxjs swetugg
Introduction to RxJS
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
rx.js make async programming simpler
Rxjs kyivjs 2015
Reactive programming and RxJS
Rx – reactive extensions
RxJava@DAUG
RxJS101 - What you need to know to get started with RxJS tomorrow

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
A comparative analysis of optical character recognition models for extracting...
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm

RxJS and Reactive Programming - Modern Web UI - May 2015

  • 1. RxJS and Reactive Programming Ben Lesh Senior UI Engineer Netflix Edge Tools & Insights @benlesh
  • 3. Observables • TC39 proposal to add them to ES2016 (ES7) • Angular 2 uses them and supports them in a first class way • ReactJS will use them and support them in a first class way • (Ask me about Ember after :P)
  • 4. Front End Development Mostly synchronous or asynchronous?
  • 5. We tend to think synchronously We write code in blocks that are read top to bottom, left to right. If this, then this, else this…
  • 6. But What Are We Doing With Code? • Handling User Input • Animations • AJAX/JSONP • Web Sockets/SSE • Updating DOM
  • 7. Most of these things are ASYNC! • Handling User Input • Animations • AJAX/JSONP • Web Sockets/SSE • Updating DOM
  • 8. Async in JavaScript usually looks like this A function that gets called 0 to N times over time, generally being passed a value
  • 9. Asynchronous values Can be represented as collections!
  • 12. Observable is basically an Iterable turned inside-out
  • 15. RxJS Observables (with error handling)
  • 16. In fact, this should look familiar…
  • 17. But Observables handle more than one value, so we need to know when they’re done
  • 18. Observables Any number of values over any amount of time
  • 19. SO WHAT? We can represent values over time as a collection… big deal.
  • 20. Set operations! • Map • Reduce • Filter • Concat • Merge • Zip • FlatMap • Take • Skip
  • 21. Plus async set operations! • Map • Reduce • Filter • Concat • Merge • Zip • FlatMap • Take • Skip • Buffer • Window • CombineLatest • Scan • FlatMapLatest • WithLatestFrom
  • 22. Confused Yet? Code Example!
  • 23. Observables for collections? I have arrays and iterables! Arrays already have map, filter, reduce
  • 24. Demo day at Netflix!
  • 25. We hooked it up to prod data and…
  • 26. So what happened? Too much array map, filter, reduce!
  • 27. Array functional programming issues • Iterating over arrays at each step • Allocating new arrays at each step • GC’ing those arrays
  • 29. RxJS Observables to the rescue! Stream Processing is nice
  • 30. Observable Stream Processing • Data is only iterated over once • No intermediary arrays created • Less GC
  • 32. Problem solved Less array allocation, less GC, less iterating over arrays
  • 33. RxJS Observables Are about to solve another problem
  • 34. Web Socket Connectivity Issues • If you walked between buildings you lost the socket connection • If you closed your lid for too long, you lost the socket connection • If the network hiccupped or went down, you lost the socket connection • If the server has an issue or restarts, you lose the socket connection • If you’re using 4G and your singal gets too low, you lose the socket connection
  • 35. Basically you had to stay very, very still..
  • 36. Multiplexing Web Sockets • Connect to the socket • Send a subscription message for each data stream you care about • Group the incoming messages by data stream • Send an unsubscribe message when you’re done with a stream • If all streams are disconnected, close the socket.
  • 37. Reconnecting Multiplexed Web Sockets Is A Pain • Reconnect the socket • Resend all subscriptions for previously subscribed streams • If you’re offline, wait for an online event, and reconnect • Add a delay and retry again (exponential) • What if the user switches views and needs different data streams during this?
  • 38. Observables Are Lazy • Execute code upon subscription to set up the underlying data stream • Execute code upon disposal to teardown the underlying data stream
  • 39. So you can do things like • Setup a web socket on subscription • Close the web socket on disposal • Send an AJAX request • Abort the AJAX request on disposal • Setup an event listener • Automatically remove event listeners on disposal
  • 40. This is nice… • Setup a web socket on subscription • Close the web socket on disposal
  • 41. Observables can be retried or repeated Simply by subscribing more than once!
  • 42. Even better! RxJS has operators: retry, retryWhen and repeat
  • 45. How does your data flow through your app? • What events cause variables to be produced • How are those variables used and for what? • Are you sure you’re containing side-effects?
  • 46. RxJS makes this easier to reason about (with practice)
  • 47. Thinking in streams • Imagine every variable (not consts) is a stream • To get it, you need to figure out where it comes from • It could come from other variables, or it could come from an event • Figure out your side effects, these are the exit points for your data flow
  • 48. Thinking in streams var c = a + b; // do something with c
  • 49. Thinking in streams var cStream = Observable.combineLatest(aStream, bStream, (a, b) => a + b); cStream.subscribe(c => { // do something with c });
  • 50. Data Flow Entry and Exit Common entry points • User input • Network I/O • Properties set • Events handled Common exit points • State • Persistence • View Renders • Emitting events
  • 51. RxJS entry and exit observable sources for entries do() and subscribe() for exits
  • 52. The idea is to keep your data flow contained And control those side effects
  • 53. Recap • Any number of values, any amount of time • Are lazy, don’t do anything until you subscribe • Embody the set up and tear down of their underlying data sources • Can retry/repeat • Use do() and subscribe() for side-effects • Observables were designed from the beginning to be cancellable
  • 54. Resources RxJS 2 on GitHub: https://github.com/Reactive-Extensions/RxJS Talk Examples: https://github.com/blesh/rx-talk-may-2015 RxSocketSubject: https://github.com/blesh/RxSocketSubject RxJS 3 (coming soon): https://github.com/ReactiveX/RxJS Observable Spec: https://github.com/jhusain/observable-spec Async Generator Proposal: https://github.com/jhusain/asyncgenerator
  • 55. Who to follow @headinthebox – Erik Meijer (Rx) @jhusain – Jafar Husain (TC39 Observable) @mattpodwysocki – Matt Podwysocki (RxJS) @jeffbcross – Jeff Cross (Angular) @victorsavkin – Victor Savkin (Angular) @benlesh – (me) @benjchristensen – Ben Christensen (RxJava)

Editor's Notes

  • #8: If you’re in a SPA, view readiness is also Async!
  • #19: Zero to Infinite things over Zero to infinite time