SlideShare a Scribd company logo
RxJS In-Depth
(Formerly a talk called
“Reactive Streams in Angular 1 and 2”)
We had a last-minute
pivot on talk content,
because Jeff Cross and
Rob Wormald wanted to
ruin my week last week.
Ben Lesh
Senior UI Engineer
Edge Developer Experience
RxJS Next
http://github.com/ReactiveX/RxJS
“RxJS Next”
is now
RxJS 5.0.0-alpha
RxJS 5 is community
driven
Spear-headed by contributors at Netflix, Google
and Microsoft. With many other contributors
under the ReactiveX community banner.
RxJS 5 Goals
• Performance
• Improved debugging
• ES7 observable spec alignment
Performance
• Decisions were guided by performance testing
• Tried out 4-5 different architectures in the
beginning
• > 120 micro performance tests
• ~ 10 macro performance tests around common
scenarios and bottlenecks
RxJS 5 operators are
4.3x faster on average
Call-stack depth and debugging
was a common complaint in older
versions of RxJS
RxJS 4
RxJS 5
Thanks in huge part to
contributions from
Paul Taylor OJ Kwon
André Staltz Jeff Cross
… really every one of our
contributors.
https://github.com/ReactiveX/RxJS/graphs/contributors
Thanks to guidance from
Matt Podwysocki Jafar Husain Erik Meijer
Ben Christensen George Campbell Aaron Tull
we’re closing in on beta
in the next month or so
What is RxJS?
• Reactive programming
• Treating events as collections
• Manipulating sets of events with ”operators”
“RxJS is LoDash or Underscore for async.”
Angular 2 is using
RxJS 5 Observables
for async
Angular has traditionally
used Promises for async
Promises
• Read-only view to a single future value
• Success and error semantics via .then()
• Not lazy. By the time you have a promise, it’s on
its way to being resolved.
• Immutable and uncancellable. Your promise will
resolve or reject, and only once.
Observable
• “Streams” or sets
• Of any number of things
• Over any amount of time
• Lazy. Observables will not generate values via an
underlying producer until they’re subscribed to.
• Can be “unsubscribed” from. This means the
underlying producer can be told to stop and even tear
down.
Both Promises and Observables
are built to solve problems
around async
(to avoid “callback hell”)
Types of async in modern
web applications
• DOM events
• Animations
• AJAX
• WebSockets
• SSE
• Alternative inputs (voice, joystick, etc)
Promises only really make
sense for one of these
• DOM events (0-N values)
• Animations (cancelable)
• AJAX (1 value)
• WebSockets (0-N values)
• SSE (0-N values)
• Alternative inputs (0-N values)
… except when they don’t
• Single page applications commonly prepare data
via AJAX for each view shown
• When the view changes, the next view probably
doesn’t care about the previous view’s data
• Fortunately, XHRs can be aborted!
• …but promise-based AJAX implementations
cannot be aborted, because promises cannot be
cancelled.*
So how do we go from
Promises to
Observables?
They have some similar
semantics
then x. (valueFn, errorFn);subscribe
They have some similar
semantics
x.subscribe(valueFn, errorFn);, completeFn
Observable also has
cancellation semantics!
let sub = x.subscribe(valueFn, errorFn, completeFn);
// ...some time later
sub.unsubscribe();
Creating Observables is
similar to creating
Promises
creating a Promise
let p = new Promise((resolve, reject) => {
doAsyncThing((err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
});
});
creating an Observable
let o = new Observable(observer => {
doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
});
if the producer’s resource
allows cancellation,
we can handle that here
let o = new Observable(observer => {
const token = doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
});
const token
return () => {
cancelAsyncThing(token);
};
when you call o.subscribe()
let o = new Observable(observer => {
const token = doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
return () => {
cancelAsyncThing(token);
};
});
observer => {
const token = doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
return () => {
};
});
when you call
subscription.unsubscribe()
let o = new Observable(observer => {
const token = doAsyncThing((err, value) => {
if (err) {
observer.error(err);
} else {
observer.next(value);
observer.complete();
}
});
return () => {
cancelAsyncThing(token);
};
});
return () => {
cancelAsyncThing(token);
};
Don’t worry... you’re
unlikely to be creating
your own Observables
Observable creation
helpers in RxJS
• Observable.of(value, value2, value3, …)
• Observable.from(promise/iterable/observable)
• Observable.fromEvent(item, eventName)
• Angular’s HTTP and Realtime Data services
• Many community-driven RxJS modules and
libraries
Error Handling in
Observables is also
similar to Promise
catch
myPromise.catch(error => {
if (error instanceof OkayError) {
return Promise.resolve(‘okay’);
} else {
throw error;
}
});
catch
myObservable.catch(error => {
if (error instanceof OkayError) {
return Observable.of(‘okay’);
} else {
throw error;
}
});
finally
(some promise impls)
myPromise.finally(() => {
console.log(‘done’);
});
finally
myObservable.finally(() => {
console.log(‘done’);
});
Observables can be “retried”
myObservable.retry(3);
myObservable.retryWhen(errors =>
errors.delay(3000));
Quick Recap on
Observable
An observable is a set of any number of things over
any amount of time.
Quick Recap on
Observable
All values are pushed to the nextHandler
observable.subscribe(nextHandler);
Quick Recap on
Observable
The errorHandler is called if the producer
experiences an error
observable.subscribe(nextHandler, errorHandler);
Quick Recap on
Observable
The completionHandler is called when the producer
completes successfully
observable.subscribe(nextHandler, errorHandler, completionHandler);
Quick Recap on
Observable
Observables are lazy. It doesn’t start producing
data until subscribe() is called.
observable.subscribe(nextHandler, errorHandler, completionHandler);
Quick Recap on
Observable
subscribe() returns a subscription, on which a
consumer can call unsubscribe() to cancel the
subscription and tear down the producer
let sub = observable.subscribe(nextHandler, errorHandler, completionHandler);
sub.unsubscribe();
What are “operators”?
They’re methods on Observable that allow you to
compose new observables.
How operators work
let result = source.myOperator();
result is an Observable, that when subscribed to,
subscribes to source, then tranforms it’s values in
some way and emits them.
How operators work
subscription.unsubscribe();
when unsubscribe() is called on that
subscription, the unsubscription logic from both
result and source are called.
How operators work
• Operators pass each value from one operator to
the next, before proceeding to the next value in
the set.*
• This is different from array operators (think map
and filter) which will process the entire array at
each step.
• *This can be changed with Schedulers.
Array filter, map, reduce
Observable filter, map, reduce
simple operators
• map()
• filter()
• reduce()
• first()
• last()
• single()
• elementAt()
• toArray()
• isEmpty()
• take()
• skip()
• startWith()
• and many more…
merging and joining operators
• merge
• mergeMap (flatMap)
• concat
• concatMap
• switch
• switchMap
• combineLatest
• withLatestFrom
• zip
• forkJoin
• expand
splitting and grouping operators
• groupBy
• window
• partition
buffering strategy operators
• buffer
• throttle
• debounce
• sample
With ES7 function bind,
you can roll your own*
(*coming soon to TypeScript?)
“hot” vs “cold” observables
• Observables are “cold” by default
• “Cold” observables create a new producer each
time a consumer subscribes to them
• “Hot” observables share a single producer with
every consumer that subscribes to them.
Multiplexed WebSocket
• Connect to a web socket server
• For each data stream
• send a subscription message to the server
• filter out responses from the server
• when done, send an unsubscription message.
• Ideally, the socket is only open while you require data
from it.
Multiplexed WebSocket
• RECONNECTION?!
• maintain a state of all of the multiplexed data
streams your views need.
• check to see when you’re able to reconnect
• reconnect the socket
• once the socket is reopened, send all of the
subscription messages again
Create an Observable
that wraps a WebSocket
It will create the WebSocket
when you subscribe()
It emits messages events
that arrive on the WebSocket
And when it unsubscribes,
it will tear down the WebSocket!
“Share” to make it “hot”…
We don’t want each subscription
creating it’s own websocket.
Create an Observable
that wraps our
Socket Observable
filter the messages
from the Socket Observable
to what you need
and subscribe
Send a message to
the server to start getting
the data over the socket
on unsubscribe,
tell the server
to stop sending data
and unsubscribe
from the socket observable
Add a retryWhen to retry the whole thing
if something goes wrong with the multiplexed observable
“share” the observable to make it “hot”,
so many consumers can share this producer.
Example Time!
Never trust anyone with
only good things to say
about their
library/framework.
What’s bad in RxJS?
• “Zalgo”.. don’t use mutable outer scoped
variables in your operators or subscriptions!
• Unbounded buffers! The zip operator in particular
is dangerous here.
• Too many operators to remember.
Twitter: @benlesh
github: blesh
ben@benlesh.com
GOOD: Comments.
BETTER: Questions.
BEST: Corrections!
Thank you!

More Related Content

PPTX
Advanced RxJS: Animations
PPTX
RxJS Animations Talk - 2017
PDF
Rethink Async With RXJS
PPTX
Async Redux Actions With RxJS - React Rally 2016
PDF
My Gentle Introduction to RxJS
PPTX
Rxjs ngvikings
PDF
RxJS101 - What you need to know to get started with RxJS tomorrow
PDF
Cascadia.js: Don't Cross the Streams
Advanced RxJS: Animations
RxJS Animations Talk - 2017
Rethink Async With RXJS
Async Redux Actions With RxJS - React Rally 2016
My Gentle Introduction to RxJS
Rxjs ngvikings
RxJS101 - What you need to know to get started with RxJS tomorrow
Cascadia.js: Don't Cross the Streams

What's hot (20)

PPTX
Intro to Functional Programming with RxJava
PDF
Introduction to RxJS
PPTX
RxJava Applied
PDF
RxJS Operators - Real World Use Cases (FULL VERSION)
PPTX
Avoiding Callback Hell with Async.js
PPTX
The Road To Reactive with RxJava JEEConf 2016
PDF
Angular and The Case for RxJS
PPTX
Connect S3 with Kafka using Akka Streams
PPTX
Luis Atencio on RxJS
PDF
ES6: The Awesome Parts
PDF
Reactive programming on Android
PDF
Practical RxJava for Android
PDF
Functional Reactive Programming / Compositional Event Systems
PDF
Practical RxJava for Android
PDF
RxJava for Android - GDG DevFest Ukraine 2015
PPTX
Functional Reactive Programming (FRP): Working with RxJS
PDF
Reactive Thinking in Java
PDF
Callbacks and control flow in Node js
PDF
Understanding Asynchronous JavaScript
ODP
Realm Mobile Database - An Introduction
Intro to Functional Programming with RxJava
Introduction to RxJS
RxJava Applied
RxJS Operators - Real World Use Cases (FULL VERSION)
Avoiding Callback Hell with Async.js
The Road To Reactive with RxJava JEEConf 2016
Angular and The Case for RxJS
Connect S3 with Kafka using Akka Streams
Luis Atencio on RxJS
ES6: The Awesome Parts
Reactive programming on Android
Practical RxJava for Android
Functional Reactive Programming / Compositional Event Systems
Practical RxJava for Android
RxJava for Android - GDG DevFest Ukraine 2015
Functional Reactive Programming (FRP): Working with RxJS
Reactive Thinking in Java
Callbacks and control flow in Node js
Understanding Asynchronous JavaScript
Realm Mobile Database - An Introduction
Ad

Viewers also liked (20)

PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
PPTX
Angular2 + rxjs
PPTX
Functional Reactive Programming with RxJS
PDF
ReactiveX-SEA
PDF
RxJS Evolved
PPTX
PPTX
Reactive Programming in Java 8 with Rx-Java
PPTX
Building modular enterprise scale angular js applications
PPTX
Observables in angular2
PDF
You will learn RxJS in 2017
PDF
Angular 2 observables
PPTX
Mini training - Reactive Extensions (Rx)
PPTX
Angular2 rxjs
PDF
FRP with Ractive and RxJS
PDF
Progressive Web Apps
PDF
Programação reativa com RxJS e Angular
PDF
Add Some Fun to Your Functional Programming With RXJS
PPTX
Reactive programming
PDF
RxJS - The Reactive extensions for JavaScript
PDF
Functional Reactive Angular 2
RxJS and Reactive Programming - Modern Web UI - May 2015
Angular2 + rxjs
Functional Reactive Programming with RxJS
ReactiveX-SEA
RxJS Evolved
Reactive Programming in Java 8 with Rx-Java
Building modular enterprise scale angular js applications
Observables in angular2
You will learn RxJS in 2017
Angular 2 observables
Mini training - Reactive Extensions (Rx)
Angular2 rxjs
FRP with Ractive and RxJS
Progressive Web Apps
Programação reativa com RxJS e Angular
Add Some Fun to Your Functional Programming With RXJS
Reactive programming
RxJS - The Reactive extensions for JavaScript
Functional Reactive Angular 2
Ad

Similar to RxJS In-Depth - AngularConnect 2015 (20)

PPTX
Introduction to RxJS
PDF
Observables in Angular
PDF
Rxjs kyivjs 2015
PPTX
Rx – reactive extensions
PDF
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
PDF
rx.js make async programming simpler
PPTX
Rxjs marble-testing
PPTX
Rxjs swetugg
PPTX
Taming Asynchrony using RxJS
PDF
Rxjs vienna
PPTX
Promises, promises, and then observables
PPTX
Angular mix chrisnoring
PDF
DZone_RC_RxJS
PDF
Reactive x
PPTX
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
PPTX
Angular observables for fun and profit
PPTX
TDC2016SP - Trilha Node.Js
PPTX
From zero to hero with the reactive extensions for java script
PPTX
From zero to hero with the reactive extensions for JavaScript
PDF
RxJava@DAUG
Introduction to RxJS
Observables in Angular
Rxjs kyivjs 2015
Rx – reactive extensions
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
rx.js make async programming simpler
Rxjs marble-testing
Rxjs swetugg
Taming Asynchrony using RxJS
Rxjs vienna
Promises, promises, and then observables
Angular mix chrisnoring
DZone_RC_RxJS
Reactive x
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Angular observables for fun and profit
TDC2016SP - Trilha Node.Js
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for JavaScript
RxJava@DAUG

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing

RxJS In-Depth - AngularConnect 2015

  • 1. RxJS In-Depth (Formerly a talk called “Reactive Streams in Angular 1 and 2”)
  • 2. We had a last-minute pivot on talk content, because Jeff Cross and Rob Wormald wanted to ruin my week last week.
  • 3. Ben Lesh Senior UI Engineer Edge Developer Experience
  • 6. RxJS 5 is community driven Spear-headed by contributors at Netflix, Google and Microsoft. With many other contributors under the ReactiveX community banner.
  • 7. RxJS 5 Goals • Performance • Improved debugging • ES7 observable spec alignment
  • 8. Performance • Decisions were guided by performance testing • Tried out 4-5 different architectures in the beginning • > 120 micro performance tests • ~ 10 macro performance tests around common scenarios and bottlenecks
  • 9. RxJS 5 operators are 4.3x faster on average
  • 10. Call-stack depth and debugging was a common complaint in older versions of RxJS
  • 13. Thanks in huge part to contributions from Paul Taylor OJ Kwon André Staltz Jeff Cross
  • 14. … really every one of our contributors. https://github.com/ReactiveX/RxJS/graphs/contributors
  • 15. Thanks to guidance from Matt Podwysocki Jafar Husain Erik Meijer Ben Christensen George Campbell Aaron Tull
  • 16. we’re closing in on beta in the next month or so
  • 17. What is RxJS? • Reactive programming • Treating events as collections • Manipulating sets of events with ”operators”
  • 18. “RxJS is LoDash or Underscore for async.”
  • 19. Angular 2 is using RxJS 5 Observables for async
  • 20. Angular has traditionally used Promises for async
  • 21. Promises • Read-only view to a single future value • Success and error semantics via .then() • Not lazy. By the time you have a promise, it’s on its way to being resolved. • Immutable and uncancellable. Your promise will resolve or reject, and only once.
  • 22. Observable • “Streams” or sets • Of any number of things • Over any amount of time • Lazy. Observables will not generate values via an underlying producer until they’re subscribed to. • Can be “unsubscribed” from. This means the underlying producer can be told to stop and even tear down.
  • 23. Both Promises and Observables are built to solve problems around async (to avoid “callback hell”)
  • 24. Types of async in modern web applications • DOM events • Animations • AJAX • WebSockets • SSE • Alternative inputs (voice, joystick, etc)
  • 25. Promises only really make sense for one of these • DOM events (0-N values) • Animations (cancelable) • AJAX (1 value) • WebSockets (0-N values) • SSE (0-N values) • Alternative inputs (0-N values)
  • 26. … except when they don’t • Single page applications commonly prepare data via AJAX for each view shown • When the view changes, the next view probably doesn’t care about the previous view’s data • Fortunately, XHRs can be aborted! • …but promise-based AJAX implementations cannot be aborted, because promises cannot be cancelled.*
  • 27. So how do we go from Promises to Observables?
  • 28. They have some similar semantics then x. (valueFn, errorFn);subscribe
  • 29. They have some similar semantics x.subscribe(valueFn, errorFn);, completeFn
  • 30. Observable also has cancellation semantics! let sub = x.subscribe(valueFn, errorFn, completeFn); // ...some time later sub.unsubscribe();
  • 31. Creating Observables is similar to creating Promises
  • 32. creating a Promise let p = new Promise((resolve, reject) => { doAsyncThing((err, value) => { if (err) { reject(err); } else { resolve(value); } }); });
  • 33. creating an Observable let o = new Observable(observer => { doAsyncThing((err, value) => { if (err) { observer.error(err); } else { observer.next(value); observer.complete(); } }); });
  • 34. if the producer’s resource allows cancellation, we can handle that here let o = new Observable(observer => { const token = doAsyncThing((err, value) => { if (err) { observer.error(err); } else { observer.next(value); observer.complete(); } }); }); const token return () => { cancelAsyncThing(token); };
  • 35. when you call o.subscribe() let o = new Observable(observer => { const token = doAsyncThing((err, value) => { if (err) { observer.error(err); } else { observer.next(value); observer.complete(); } }); return () => { cancelAsyncThing(token); }; }); observer => { const token = doAsyncThing((err, value) => { if (err) { observer.error(err); } else { observer.next(value); observer.complete(); } }); return () => { }; });
  • 36. when you call subscription.unsubscribe() let o = new Observable(observer => { const token = doAsyncThing((err, value) => { if (err) { observer.error(err); } else { observer.next(value); observer.complete(); } }); return () => { cancelAsyncThing(token); }; }); return () => { cancelAsyncThing(token); };
  • 37. Don’t worry... you’re unlikely to be creating your own Observables
  • 38. Observable creation helpers in RxJS • Observable.of(value, value2, value3, …) • Observable.from(promise/iterable/observable) • Observable.fromEvent(item, eventName) • Angular’s HTTP and Realtime Data services • Many community-driven RxJS modules and libraries
  • 39. Error Handling in Observables is also similar to Promise
  • 40. catch myPromise.catch(error => { if (error instanceof OkayError) { return Promise.resolve(‘okay’); } else { throw error; } });
  • 41. catch myObservable.catch(error => { if (error instanceof OkayError) { return Observable.of(‘okay’); } else { throw error; } });
  • 42. finally (some promise impls) myPromise.finally(() => { console.log(‘done’); });
  • 44. Observables can be “retried” myObservable.retry(3); myObservable.retryWhen(errors => errors.delay(3000));
  • 45. Quick Recap on Observable An observable is a set of any number of things over any amount of time.
  • 46. Quick Recap on Observable All values are pushed to the nextHandler observable.subscribe(nextHandler);
  • 47. Quick Recap on Observable The errorHandler is called if the producer experiences an error observable.subscribe(nextHandler, errorHandler);
  • 48. Quick Recap on Observable The completionHandler is called when the producer completes successfully observable.subscribe(nextHandler, errorHandler, completionHandler);
  • 49. Quick Recap on Observable Observables are lazy. It doesn’t start producing data until subscribe() is called. observable.subscribe(nextHandler, errorHandler, completionHandler);
  • 50. Quick Recap on Observable subscribe() returns a subscription, on which a consumer can call unsubscribe() to cancel the subscription and tear down the producer let sub = observable.subscribe(nextHandler, errorHandler, completionHandler); sub.unsubscribe();
  • 51. What are “operators”? They’re methods on Observable that allow you to compose new observables.
  • 52. How operators work let result = source.myOperator(); result is an Observable, that when subscribed to, subscribes to source, then tranforms it’s values in some way and emits them.
  • 53. How operators work subscription.unsubscribe(); when unsubscribe() is called on that subscription, the unsubscription logic from both result and source are called.
  • 54. How operators work • Operators pass each value from one operator to the next, before proceeding to the next value in the set.* • This is different from array operators (think map and filter) which will process the entire array at each step. • *This can be changed with Schedulers.
  • 57. simple operators • map() • filter() • reduce() • first() • last() • single() • elementAt() • toArray() • isEmpty() • take() • skip() • startWith() • and many more…
  • 58. merging and joining operators • merge • mergeMap (flatMap) • concat • concatMap • switch • switchMap • combineLatest • withLatestFrom • zip • forkJoin • expand
  • 59. splitting and grouping operators • groupBy • window • partition
  • 60. buffering strategy operators • buffer • throttle • debounce • sample
  • 61. With ES7 function bind, you can roll your own* (*coming soon to TypeScript?)
  • 62. “hot” vs “cold” observables • Observables are “cold” by default • “Cold” observables create a new producer each time a consumer subscribes to them • “Hot” observables share a single producer with every consumer that subscribes to them.
  • 63. Multiplexed WebSocket • Connect to a web socket server • For each data stream • send a subscription message to the server • filter out responses from the server • when done, send an unsubscription message. • Ideally, the socket is only open while you require data from it.
  • 64. Multiplexed WebSocket • RECONNECTION?! • maintain a state of all of the multiplexed data streams your views need. • check to see when you’re able to reconnect • reconnect the socket • once the socket is reopened, send all of the subscription messages again
  • 65. Create an Observable that wraps a WebSocket
  • 66. It will create the WebSocket when you subscribe()
  • 67. It emits messages events that arrive on the WebSocket
  • 68. And when it unsubscribes, it will tear down the WebSocket!
  • 69. “Share” to make it “hot”… We don’t want each subscription creating it’s own websocket.
  • 70. Create an Observable that wraps our Socket Observable
  • 71. filter the messages from the Socket Observable to what you need and subscribe
  • 72. Send a message to the server to start getting the data over the socket
  • 73. on unsubscribe, tell the server to stop sending data and unsubscribe from the socket observable
  • 74. Add a retryWhen to retry the whole thing if something goes wrong with the multiplexed observable
  • 75. “share” the observable to make it “hot”, so many consumers can share this producer.
  • 77. Never trust anyone with only good things to say about their library/framework.
  • 78. What’s bad in RxJS? • “Zalgo”.. don’t use mutable outer scoped variables in your operators or subscriptions! • Unbounded buffers! The zip operator in particular is dangerous here. • Too many operators to remember.
  • 79. Twitter: @benlesh github: blesh ben@benlesh.com GOOD: Comments. BETTER: Questions. BEST: Corrections! Thank you!

Editor's Notes

  • #14: Luis Gabriel Adrian Omelo Jinroh Rob Wormwald