SlideShare a Scribd company logo
1 of 28
1
how reactive programming can help in asynchronous non
blocking environments
Make ReactiveX and Node dance
Enrico Piccinin
Rome April 14th 2018
2
Enrico Piccinin
Italia
3
ReactiveX is a library to implement the Observable pattern
ReactiveX - Observable
4
A quick definition of ReactiveX and Observable
What an Observable is
A way to model streams of
asynchronous events
What you can do
Transform the event streams via
operators – apply functional
programming style to streams of
events
Consume events via subscription
Transform functions which use
callbacks into Observables
Miri Charlie Fede
Stream
completed
time
Each event comes with its own
data
5
Subscription allows to define what to do with the events of a stream
observable1
.subscribe(
)
Miri Charlie Fede Stream
completed
observable1
data => {
// do something with data received
},
() => {
// do something when the Observable completes
},
err => {
// manage the error
},
6
Operators make Observables easy to use in a functional style
observable1
1 2 3 4 5
.map(data => data * 2)
2 4 6 8 10
8 10
.filter(data => data > 5)
6
8.take(2) 6
7
Let’s see Observables in action in a very simple use case
• Read all files in the Source directory
• Transform the content of each file
• Write a new file in Target directory
• Write a line in the log for each file saved
• Write on the console when the processing is completed
Source Dir Target Dir
transform
level_1_file
level_2_file
level_3_file
8
… a real world use case
Source Dir Target Dir
transform
This is an I/O bound process –
the nirvana of Node
~150.000 Cobol source files
Extract all sql statements
About 20 times faster than
synchronous I/O blocking logic
9
readFilesFromSourceDir(dir, cb(data, err) {
})
readFileContent(path, cb(err, content) {
transform(content)
})
writeFileToTargetDir(newContent, cb(err, data) {
})
The callback approach - the traditional Node approach
for each file
end-for
writeLog(newFile, cb(err, data) {
})
Am I done?
10
What happens in an asynchronous non-blocking world
SourceDir read
File read File written
File read
File read File written Log written
time
File read
Log written
File written Log written
Done
File written Log written
file1
file2
file3
file4
Dir Read
Same pattern Log Written
11
Maybe we can see this from a slightly different perspective
Log written
Log written
Functions &
Streams of events
12
For this reactive model we need some Observables
Functions & Event streams
Done
File
written
Log
written
File read
SourceDir read
Observables required
Emits when the content of a file is read –
completes when all files are read
Emits when a new file is written –
completes when all files are written
Emits when a line in the log is written –
completes when all logs lines are written
Emits when the directory is read and then
completes
13
We need to start from what node gives us: “callback based”
functions
fs.readdir(path[, options], callback)Read Directory
fs.readFile(path[, options], callback)Read File
fs.writeFile(file, data[, options], callback)Write File
fs.write(fd, string[, position[, encoding]], callback)Append line
readFilesFromSourceDir(dir, cb(data, err) {
})
readFileContent(path, cb(err, content) {
transform(content)
})
writeFileToTargetDir(newContent, cb(err, data) {
})
for each file
end-for
writeLog(newFile, cb(err, data) {
})
Am I done?
14
We can create Observables from “callback based” functions
readDirObs(dir)
File Names
readDirObs = Observable.bindNodeCallback(fs.readdir)
Emits the Array of File Names when the directory is read and then completes
15
Observables can be created also from Arrays
names = [ ”Gina”, “Fede”, “Maria”, ”Carl”, “Rob” ]
namesObs = Observable.from( names )
namesObs
Gina Fede Maria Carl Rob
Emits sequentially as many events as the number of Objects in the Array,
each event carrying as content data the object itself, and then completes
16
Now we have all the tools we need to build our solution
We can create Observables from
callback based functions
We can create Observables from
Arrays
We can transform Observables
into other Observables via
operators
readFilesFromSourceDir(dir, cb(data, err) {
})
readFileContent(path, cb(err, content) {
transform(content)
})
writeFileToTargetDir(newContent, cb(err, data) {
})
for each file
end-for
writeLog(newFile, cb(err, data) {
})
Am I done?
[1, 2, 3, … ]
observable1
observable2 = operator( observable1 )
observable2
17
.switchMap(fileName =>
writeLogObs(fileName))
.switchMap(content =>
writeFileObs(content))
The first thing to do is to build the function that processes one file
content
switch
fileName
newContent = transform(content)
writeFileObs(newContent)
switch
fileName
writeLogObs(fileName)
readFileObs(fileName)
18
We have created a function which returns an Observable which emits
once a file has completed its processing
function transformFile(name):
Observable<string> {
return
readFileObs(name)
.map(data => transform(data))
.switchMap(data => writeFileObs(data))
.switchMap(file => writeLogObs(file))
}
File
written
Log
written
File read
19
.subscribe(
data => console.log(data),
err => console.error(err),
() => console.log(“DONE”))
Back now to the entire journey: it all starts from the first Observable
fileName
Observable.from(fileNames)
fileName fileName
transformFile(fileName)
merge
.mergeMap(fileName =>
transformFile(fileName))
switch
.switchMap(content =>
Observable.from(fileNames))
fileNames
readDirObs(dirName)
Done
20
How all pieces of code look like
function transformFile(name): Observable<string> {
return
readFileObs(name)
.map(data => transform(data))
.switchMap(data => writeFileObs(data))
.switchMap(file => writeLogObs(file))
}
readDirObs(dir: string)
.switchMap(files => Observable.from(files))
.mergeMap(file => transformFile(file))
.subscribe(
file => console.log(file + ‘ logged’),
err => { console.error(err) },
() => console.log(‘I am done’)
)
File
written
Log
written
File read
Done
21
Demo – add a line number to each Canto of Divina Commedia
22
How many files do I start reading before closing the first one?
File Names
switch
content
File name
Observable.from(files)
File name File name File name
How many
files do we
open ?
23
Let’s make our use case more real – concurrency is limited
Source Dir Target Dir
transform
level_1_file
level_2_file
level_3_file
Limit concurrency
readFilesFromSourceDir(dir, cb(data, err) {
})
readFileContent(path, cb(err, content) {
transform(content)
})
writeFileToTargetDir(newContent, cb(err, data) {
})
for each file
end-for
writeLog(newFile, cb(err, data) {
})
24
Add a limit to the concurrency of mergeMap
SourceDir read readDirObs(dirName)
File nameswitch
.switchMap(data =>
Observable.from(data))
File name File name
File processed File processed File processed
transformFile(file)
Done
.subscribe(
data => console.log(data),
err => console.error(err),
() => console.log(“DONE”))
merge
.mergeMap(file =>
transformFile(file), nnn)
level of concurrency
25
So what? Are Observables bringing any benefit here vs Promises?
File
written
Log
written
File read
1
2
3
4
Observables
1
2
3
4
Promises
NOT ALWAYS
26
But if we look at the big Picture? What if we have real streams of
events to deal with?
Done
Observables
Promises
A
B
C
1
3
2
27
and the world is full of “asynchronous event streams”
Serveless
functions
Databases Realtime Database
HTTP
Websockets
28
Some details on the topic discussed may be found here
https://medium.freecodecamp.org/rxjs-and-node-8f4e0acebc7c
The code of the samples shown in the demo can be downloaded from here
https://github.com/codemotion-2018-rome-rxjs-node/rxjs-node-fs
Thank you

More Related Content

What's hot

java copy file program
java copy file programjava copy file program
java copy file programGlen Pais
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowLaura Lorenz
 
Using akka streams to access s3 objects
Using akka streams to access s3 objectsUsing akka streams to access s3 objects
Using akka streams to access s3 objectsMikhail Girkin
 
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015Till Rohrmann
 
How to Integrate Internet of Things with Webserver with
How to Integrate Internet of Things with Webserver with How to Integrate Internet of Things with Webserver with
How to Integrate Internet of Things with Webserver with Ionela
 
Openpicus Flyport interfaces the cloud services
Openpicus Flyport interfaces the cloud servicesOpenpicus Flyport interfaces the cloud services
Openpicus Flyport interfaces the cloud servicesIonela
 
Apache Flink @ NYC Flink Meetup
Apache Flink @ NYC Flink MeetupApache Flink @ NYC Flink Meetup
Apache Flink @ NYC Flink MeetupStephan Ewen
 

What's hot (10)

java copy file program
java copy file programjava copy file program
java copy file program
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with AirflowHow I learned to time travel, or, data pipelining and scheduling with Airflow
How I learned to time travel, or, data pipelining and scheduling with Airflow
 
Git basic
Git basicGit basic
Git basic
 
Using akka streams to access s3 objects
Using akka streams to access s3 objectsUsing akka streams to access s3 objects
Using akka streams to access s3 objects
 
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015
Streaming Data Flow with Apache Flink @ Paris Flink Meetup 2015
 
How to Integrate Internet of Things with Webserver with
How to Integrate Internet of Things with Webserver with How to Integrate Internet of Things with Webserver with
How to Integrate Internet of Things with Webserver with
 
Openpicus Flyport interfaces the cloud services
Openpicus Flyport interfaces the cloud servicesOpenpicus Flyport interfaces the cloud services
Openpicus Flyport interfaces the cloud services
 
Apache Flink @ NYC Flink Meetup
Apache Flink @ NYC Flink MeetupApache Flink @ NYC Flink Meetup
Apache Flink @ NYC Flink Meetup
 
File io
File io File io
File io
 
Dsfdssdfsdf
DsfdssdfsdfDsfdssdfsdf
Dsfdssdfsdf
 

Similar to Codemotion Rome 2018 - RxJs and Node

fileop report
fileop reportfileop report
fileop reportJason Lu
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and outputallenbailey
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and EnhancementsGagan Agrawal
 
2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin CoroutinesEamonn Boyle
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingAnalyticsConf
 
Programming language for the cloud infrastructure
Programming language for the cloud infrastructureProgramming language for the cloud infrastructure
Programming language for the cloud infrastructureYaroslav Muravskyi
 
Airflow tutorials hands_on
Airflow tutorials hands_onAirflow tutorials hands_on
Airflow tutorials hands_onpko89403
 
Jaffle: managing processes and log messages of multiple applications in devel...
Jaffle: managing processes and log messages of multiple applicationsin devel...Jaffle: managing processes and log messages of multiple applicationsin devel...
Jaffle: managing processes and log messages of multiple applications in devel...Masaki Yatsu
 
Hadoop Introduction
Hadoop IntroductionHadoop Introduction
Hadoop IntroductionSNEHAL MASNE
 
OPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONOPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONSUMIT KUMAR
 
The Ring programming language version 1.5.2 book - Part 25 of 181
The Ring programming language version 1.5.2 book - Part 25 of 181The Ring programming language version 1.5.2 book - Part 25 of 181
The Ring programming language version 1.5.2 book - Part 25 of 181Mahmoud Samir Fayed
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphiMax Kleiner
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_iNico Ludwig
 

Similar to Codemotion Rome 2018 - RxJs and Node (20)

fileop report
fileop reportfileop report
fileop report
 
3.1 file input and output
3.1   file input and output3.1   file input and output
3.1 file input and output
 
Tibco business works
Tibco business worksTibco business works
Tibco business works
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and Enhancements
 
2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines2019-01-29 - Demystifying Kotlin Coroutines
2019-01-29 - Demystifying Kotlin Coroutines
 
Wiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programmingWiesław Kałkus: C# functional programming
Wiesław Kałkus: C# functional programming
 
Programming language for the cloud infrastructure
Programming language for the cloud infrastructureProgramming language for the cloud infrastructure
Programming language for the cloud infrastructure
 
Airflow tutorials hands_on
Airflow tutorials hands_onAirflow tutorials hands_on
Airflow tutorials hands_on
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Jaffle: managing processes and log messages of multiple applications in devel...
Jaffle: managing processes and log messages of multiple applicationsin devel...Jaffle: managing processes and log messages of multiple applicationsin devel...
Jaffle: managing processes and log messages of multiple applications in devel...
 
Hadoop Introduction
Hadoop IntroductionHadoop Introduction
Hadoop Introduction
 
OPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATIONOPEN TEXT ADMINISTRATION
OPEN TEXT ADMINISTRATION
 
The Ring programming language version 1.5.2 book - Part 25 of 181
The Ring programming language version 1.5.2 book - Part 25 of 181The Ring programming language version 1.5.2 book - Part 25 of 181
The Ring programming language version 1.5.2 book - Part 25 of 181
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphi
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 

Recently uploaded

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 

Recently uploaded (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Codemotion Rome 2018 - RxJs and Node

  • 1. 1 how reactive programming can help in asynchronous non blocking environments Make ReactiveX and Node dance Enrico Piccinin Rome April 14th 2018
  • 3. 3 ReactiveX is a library to implement the Observable pattern ReactiveX - Observable
  • 4. 4 A quick definition of ReactiveX and Observable What an Observable is A way to model streams of asynchronous events What you can do Transform the event streams via operators – apply functional programming style to streams of events Consume events via subscription Transform functions which use callbacks into Observables Miri Charlie Fede Stream completed time Each event comes with its own data
  • 5. 5 Subscription allows to define what to do with the events of a stream observable1 .subscribe( ) Miri Charlie Fede Stream completed observable1 data => { // do something with data received }, () => { // do something when the Observable completes }, err => { // manage the error },
  • 6. 6 Operators make Observables easy to use in a functional style observable1 1 2 3 4 5 .map(data => data * 2) 2 4 6 8 10 8 10 .filter(data => data > 5) 6 8.take(2) 6
  • 7. 7 Let’s see Observables in action in a very simple use case • Read all files in the Source directory • Transform the content of each file • Write a new file in Target directory • Write a line in the log for each file saved • Write on the console when the processing is completed Source Dir Target Dir transform level_1_file level_2_file level_3_file
  • 8. 8 … a real world use case Source Dir Target Dir transform This is an I/O bound process – the nirvana of Node ~150.000 Cobol source files Extract all sql statements About 20 times faster than synchronous I/O blocking logic
  • 9. 9 readFilesFromSourceDir(dir, cb(data, err) { }) readFileContent(path, cb(err, content) { transform(content) }) writeFileToTargetDir(newContent, cb(err, data) { }) The callback approach - the traditional Node approach for each file end-for writeLog(newFile, cb(err, data) { }) Am I done?
  • 10. 10 What happens in an asynchronous non-blocking world SourceDir read File read File written File read File read File written Log written time File read Log written File written Log written Done File written Log written file1 file2 file3 file4 Dir Read Same pattern Log Written
  • 11. 11 Maybe we can see this from a slightly different perspective Log written Log written Functions & Streams of events
  • 12. 12 For this reactive model we need some Observables Functions & Event streams Done File written Log written File read SourceDir read Observables required Emits when the content of a file is read – completes when all files are read Emits when a new file is written – completes when all files are written Emits when a line in the log is written – completes when all logs lines are written Emits when the directory is read and then completes
  • 13. 13 We need to start from what node gives us: “callback based” functions fs.readdir(path[, options], callback)Read Directory fs.readFile(path[, options], callback)Read File fs.writeFile(file, data[, options], callback)Write File fs.write(fd, string[, position[, encoding]], callback)Append line readFilesFromSourceDir(dir, cb(data, err) { }) readFileContent(path, cb(err, content) { transform(content) }) writeFileToTargetDir(newContent, cb(err, data) { }) for each file end-for writeLog(newFile, cb(err, data) { }) Am I done?
  • 14. 14 We can create Observables from “callback based” functions readDirObs(dir) File Names readDirObs = Observable.bindNodeCallback(fs.readdir) Emits the Array of File Names when the directory is read and then completes
  • 15. 15 Observables can be created also from Arrays names = [ ”Gina”, “Fede”, “Maria”, ”Carl”, “Rob” ] namesObs = Observable.from( names ) namesObs Gina Fede Maria Carl Rob Emits sequentially as many events as the number of Objects in the Array, each event carrying as content data the object itself, and then completes
  • 16. 16 Now we have all the tools we need to build our solution We can create Observables from callback based functions We can create Observables from Arrays We can transform Observables into other Observables via operators readFilesFromSourceDir(dir, cb(data, err) { }) readFileContent(path, cb(err, content) { transform(content) }) writeFileToTargetDir(newContent, cb(err, data) { }) for each file end-for writeLog(newFile, cb(err, data) { }) Am I done? [1, 2, 3, … ] observable1 observable2 = operator( observable1 ) observable2
  • 17. 17 .switchMap(fileName => writeLogObs(fileName)) .switchMap(content => writeFileObs(content)) The first thing to do is to build the function that processes one file content switch fileName newContent = transform(content) writeFileObs(newContent) switch fileName writeLogObs(fileName) readFileObs(fileName)
  • 18. 18 We have created a function which returns an Observable which emits once a file has completed its processing function transformFile(name): Observable<string> { return readFileObs(name) .map(data => transform(data)) .switchMap(data => writeFileObs(data)) .switchMap(file => writeLogObs(file)) } File written Log written File read
  • 19. 19 .subscribe( data => console.log(data), err => console.error(err), () => console.log(“DONE”)) Back now to the entire journey: it all starts from the first Observable fileName Observable.from(fileNames) fileName fileName transformFile(fileName) merge .mergeMap(fileName => transformFile(fileName)) switch .switchMap(content => Observable.from(fileNames)) fileNames readDirObs(dirName) Done
  • 20. 20 How all pieces of code look like function transformFile(name): Observable<string> { return readFileObs(name) .map(data => transform(data)) .switchMap(data => writeFileObs(data)) .switchMap(file => writeLogObs(file)) } readDirObs(dir: string) .switchMap(files => Observable.from(files)) .mergeMap(file => transformFile(file)) .subscribe( file => console.log(file + ‘ logged’), err => { console.error(err) }, () => console.log(‘I am done’) ) File written Log written File read Done
  • 21. 21 Demo – add a line number to each Canto of Divina Commedia
  • 22. 22 How many files do I start reading before closing the first one? File Names switch content File name Observable.from(files) File name File name File name How many files do we open ?
  • 23. 23 Let’s make our use case more real – concurrency is limited Source Dir Target Dir transform level_1_file level_2_file level_3_file Limit concurrency readFilesFromSourceDir(dir, cb(data, err) { }) readFileContent(path, cb(err, content) { transform(content) }) writeFileToTargetDir(newContent, cb(err, data) { }) for each file end-for writeLog(newFile, cb(err, data) { })
  • 24. 24 Add a limit to the concurrency of mergeMap SourceDir read readDirObs(dirName) File nameswitch .switchMap(data => Observable.from(data)) File name File name File processed File processed File processed transformFile(file) Done .subscribe( data => console.log(data), err => console.error(err), () => console.log(“DONE”)) merge .mergeMap(file => transformFile(file), nnn) level of concurrency
  • 25. 25 So what? Are Observables bringing any benefit here vs Promises? File written Log written File read 1 2 3 4 Observables 1 2 3 4 Promises NOT ALWAYS
  • 26. 26 But if we look at the big Picture? What if we have real streams of events to deal with? Done Observables Promises A B C 1 3 2
  • 27. 27 and the world is full of “asynchronous event streams” Serveless functions Databases Realtime Database HTTP Websockets
  • 28. 28 Some details on the topic discussed may be found here https://medium.freecodecamp.org/rxjs-and-node-8f4e0acebc7c The code of the samples shown in the demo can be downloaded from here https://github.com/codemotion-2018-rome-rxjs-node/rxjs-node-fs Thank you