SlideShare a Scribd company logo
1 of 56
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

What's hot

Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Libraryasync_io
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJSMattia Occhiuto
 
Scala Future & Promises
Scala Future & PromisesScala Future & Promises
Scala Future & PromisesKnoldus Inc.
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaAli Muzaffar
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014Thomas Lockney
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scalaStratio
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaIntro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaMike Nakhimovich
 
Intro to React
Intro to ReactIntro to React
Intro to ReactTroy Miles
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practicesfloydophone
 
Reactive Extensions for JavaScript
Reactive Extensions for JavaScriptReactive Extensions for JavaScript
Reactive Extensions for JavaScriptJim Wooley
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN StackTroy Miles
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jscacois
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use casesFabio Biondi
 
Angular Weekend
Angular WeekendAngular Weekend
Angular WeekendTroy Miles
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015Constantine Mars
 
The dark side of Akka and the remedy
The dark side of Akka and the remedyThe dark side of Akka and the remedy
The dark side of Akka and the remedykrivachy
 

What's hot (20)

Javascript Promises/Q Library
Javascript Promises/Q LibraryJavascript Promises/Q Library
Javascript Promises/Q Library
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
Scala Future & Promises
Scala Future & PromisesScala Future & Promises
Scala Future & Promises
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaIntro to Functional Programming with RxJava
Intro to Functional Programming with RxJava
 
rx-java-presentation
rx-java-presentationrx-java-presentation
rx-java-presentation
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
Reactive Extensions for JavaScript
Reactive Extensions for JavaScriptReactive Extensions for JavaScript
Reactive Extensions for JavaScript
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
 
Avoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.jsAvoiding Callback Hell with Async.js
Avoiding Callback Hell with Async.js
 
Introduction to es6
Introduction to es6Introduction to es6
Introduction to es6
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
Angular Weekend
Angular WeekendAngular Weekend
Angular Weekend
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 
Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
 
The dark side of Akka and the remedy
The dark side of Akka and the remedyThe dark side of Akka and the remedy
The dark side of Akka and the remedy
 

Viewers also liked

Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJSstefanmayer13
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streamsmattpodwysocki
 
Functional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwiftFunctional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwift선협 이
 
[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍NAVER D2
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기Jong Wook Kim
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015NAVER / MusicPlatform
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - Polymer혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - PolymerJae Sung Park
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS偉格 高
 
System webpack-jspm
System webpack-jspmSystem webpack-jspm
System webpack-jspmJesse Warden
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
 
Closure, Higher-order function in Swift
Closure, Higher-order function in SwiftClosure, Higher-order function in Swift
Closure, Higher-order function in SwiftSeongGyu Jo
 
React in Native Apps - Meetup React - 20150409
React in Native Apps - Meetup React - 20150409React in Native Apps - Meetup React - 20150409
React in Native Apps - Meetup React - 20150409Minko3D
 
맛만 보자 액터 모델이란
맛만 보자 액터 모델이란 맛만 보자 액터 모델이란
맛만 보자 액터 모델이란 jbugkorea
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesomeAndrew Hull
 
LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기Wanbok Choi
 

Viewers also liked (17)

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

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

Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesDavid Martínez Rego
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous ApplicationsJohan Edstrom
 
FP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleFP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleChristophe Grand
 
Hard Coding as a design approach
Hard Coding as a design approachHard Coding as a design approach
Hard Coding as a design approachOren Eini
 
Cycling for noobs
Cycling for noobsCycling for noobs
Cycling for noobsSteve Lee
 
Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16allingeek
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitterRoger Xia
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...smallerror
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...xlight
 
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...Lucidworks
 
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...confluent
 
Streamlining with rx
Streamlining with rxStreamlining with rx
Streamlining with rxAkhil Dad
 
Building GUI App with Electron and Lisp
Building GUI App with Electron and LispBuilding GUI App with Electron and Lisp
Building GUI App with Electron and Lispfukamachi
 
Microservices for java architects it-symposium-2015-09-15
Microservices for java architects it-symposium-2015-09-15Microservices for java architects it-symposium-2015-09-15
Microservices for java architects it-symposium-2015-09-15Derek Ashmore
 
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...Amazon Web Services
 
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, Europe
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, EuropePatterns of the Lambda Architecture -- 2015 April - Hadoop Summit, Europe
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, EuropeFlip Kromer
 
A Production Quality Sketching Library for the Analysis of Big Data
A Production Quality Sketching Library for the Analysis of Big DataA Production Quality Sketching Library for the Analysis of Big Data
A Production Quality Sketching Library for the Analysis of Big DataDatabricks
 

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

Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
 
FP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit HoleFP Days: Down the Clojure Rabbit Hole
FP Days: Down the Clojure Rabbit Hole
 
Hard Coding as a design approach
Hard Coding as a design approachHard Coding as a design approach
Hard Coding as a design approach
 
React introduction
React introductionReact introduction
React introduction
 
Cycling for noobs
Cycling for noobsCycling for noobs
Cycling for noobs
 
Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16Getting Deep on Orchestration - Nickoloff - DockerCon16
Getting Deep on Orchestration - Nickoloff - DockerCon16
 
Fixing twitter
Fixing twitterFixing twitter
Fixing twitter
 
Fixing_Twitter
Fixing_TwitterFixing_Twitter
Fixing_Twitter
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...Fixing Twitter  Improving The Performance And Scalability Of The Worlds Most ...
Fixing Twitter Improving The Performance And Scalability Of The Worlds Most ...
 
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
 
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...
Hard Truths About Streaming and Eventing (Dan Rosanova, Microsoft) Kafka Summ...
 
Streamlining with rx
Streamlining with rxStreamlining with rx
Streamlining with rx
 
Building GUI App with Electron and Lisp
Building GUI App with Electron and LispBuilding GUI App with Electron and Lisp
Building GUI App with Electron and Lisp
 
Microservices for java architects it-symposium-2015-09-15
Microservices for java architects it-symposium-2015-09-15Microservices for java architects it-symposium-2015-09-15
Microservices for java architects it-symposium-2015-09-15
 
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...
AWS re:Invent 2016| GAM302 | Sony PlayStation: Breaking the Bandwidth Barrier...
 
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, Europe
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, EuropePatterns of the Lambda Architecture -- 2015 April - Hadoop Summit, Europe
Patterns of the Lambda Architecture -- 2015 April - Hadoop Summit, Europe
 
A Production Quality Sketching Library for the Analysis of Big Data
A Production Quality Sketching Library for the Analysis of Big DataA Production Quality Sketching Library for the Analysis of Big Data
A Production Quality Sketching Library for the Analysis of Big Data
 
React.js at Cortex
React.js at CortexReact.js at Cortex
React.js at Cortex
 

Recently uploaded

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Recently uploaded (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

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

  1. If you’re in a SPA, view readiness is also Async!
  2. Zero to Infinite things over Zero to infinite time