SlideShare a Scribd company logo
Reactive Programming
in PHP
Johney Park
2018.06.17
Reactive Programming
=
Observer Pattern
+ LINQ-style operators
( + Schedulers)
TL; DR
Pros:
Good for handling async events
Cons:
Difficult to understand
Too many things to learn
Some overhead
TL; DR
Definition
Reactive Programming is
programming with
asynchronous
data streams.
https://gist.github.com/
staltz/868e7e9bc2a7b8c1f754
(Where is reactive?)
What you heard when you
learn reactive programming
• Observer/Observable / Hot & Cold
• Signal / Flux / Stream / EventStream / EventSource / …
• Subject
• Scheduler
• Publish / Subscribe / Unsubscribe / Dispose / Behavior
• Producer / Consumer
• Map / Reduce / Throttle / …
Observer Pattern
Observer 1
Observable
watch & run
Observer 2
♨
(Event Source)
wrap
Observer Pattern
Observer 1
Observable
subscribe
Lazy Evaluation
Subscription 1
async call with
stream operators
Observer 2 Subscription 2
Observers don’t observe
Observable does all the work
Producer / Consumer may be better naming
Observer Pattern
Observer 1
Observable 1
Observer 2
Observer 3
Subject 1
Subject 2
Subject = Observable + Observer
broadcast
♨
(Event Source)
♨
(Event Source)
direct call
When to use RP?
• One or more event sources
• Require stream operators
• Not a perfect solution for all
async scenarios
• External API Call (MSA)
• UI Events (Click)
• Message Processing
• Abstraction over Async Process
RxPHP
• stream_select (PHP 4 >= 4.3.0, PHP 5, PHP 7)
• ReactPHP != RxPHP
• ReactPHP is event-driven, non-blocking I/O library
• EventLoop, Promise from ReactPHP
• https://github.com/ReactiveX/RxPHP
RxPHP
$source = RxObservable ::fromArray([1, 2, 3, 4]);
$observer = new RxObserverCallbackObserver(
function ($x) {
echo 'Next: ', $x, PHP_EOL;
},
function (Exception $ex) {
echo 'Error: ', $ex ->getMessage(), PHP_EOL;
},
function () {
echo 'Completed', PHP_EOL;
});
$source ->subscribe($observer);
// Next: 1
// Next: 2
// Next: 3
// Next: 4
// Completed
Very simple sample code
• Cold Observable Sample, almost
useless
• Source = Observable
• Subscribe Observer to
Observable
• Observer is consist of three
functions
• next, error, completed
RxPHP
$loop = Factory ::create();
Scheduler ::setDefaultFactory(function () use ($loop) {
return new SchedulerEventLoopScheduler($loop);
});
register_shutdown_function(function () use ($loop) {
$loop ->run();
});
Observable ::interval(1000)
->take(5)
->flatMap(function ($i) {
return Observable ::of($i + 1);
})
->subscribe(function ($e) {
echo $e, PHP_EOL;
});
// 1
// 2
// 3
// 4
// 5
Event Loop
• We need to set EventLoop when using
stream operator
• interval, throttle, etc..
• If you don’t use stream operator, you
don’t need to set EventLoop
• RxJs don’t require explicit event loop,
because Javascript has setTimeout,
setInterval, requestAnimationFrame
• There are many scheduler types
• flatMap == mergeMap, but RxPHP don’t
provide mergeMap
RxPHP
$subject = new RxSubjectSubject();
$subject ->subscribe(function($i) { echo $i . PHP_EOL;});
RxObservable ::interval(1000)
->take(5)
->flatMap(function ($i) {
return Observable ::of($i + 1);
})
->subscribe($subject);
$subject ->onNext(10);
$subject ->onNext(20);
$subject ->onNext(30);
// 10
// 20
// 30
// 1
// 2
// 3
// 4
// 5
Subject
• Subject = Observable +
Observer
• Actually Subject is extended
from Observable
• You don’t need to create custom
Observable for simple usage
RxPHP
More Samples
• https://github.com/ReactiveX/RxPHP/tree/master/demo
• https://github.com/PacktPublishing/PHP-Reactive-
Programming/
• https://www.packtpub.com/web-development/php-
reactive-programming
RxPHP
Ben Lesh’s comment
RxJS could be so simple... just a handful of types and operators.
... but if we do that, then people shoot themselves in the foot with memory
leaks and their own poorly implemented `expand` operator or whatever… ...
so then we add 60+ operators and people are like "WHOA...." haha.. Now it's
too complicated.
It's tough everywhere. Haha
If I had a nickel for every poorly implemented custom RxJS operator I've
seen… ... I'd only have about 25 cents, because we've already built almost
every tool someone could want.
But who have we scared off in the process?
The End

More Related Content

Similar to Reactive programming in PHP

Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
Jalpesh Vadgama
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PROIDEA
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
King Foo
 
Reactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NETReactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NET
EPAM
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
Pavan Chitumalla
 
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
PyData
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
Ferenc Kovács
 
"Swoole: double troubles in c", Alexandr Vronskiy
"Swoole: double troubles in c", Alexandr Vronskiy"Swoole: double troubles in c", Alexandr Vronskiy
"Swoole: double troubles in c", Alexandr Vronskiy
Fwdays
 
Node.js primer
Node.js primerNode.js primer
Node.js primer
Quhan Arunasalam
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
Bansilal Haudakari
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
Yardena Meymann
 
Airflow 101
Airflow 101Airflow 101
Airflow 101
SaarBergerbest
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
ppd1961
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
YarikS
 
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHPphp[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
Adam Englander
 
Java 8 - Lambdas and much more
Java 8 - Lambdas and much moreJava 8 - Lambdas and much more
Java 8 - Lambdas and much more
Alin Pandichi
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJS
Oswald Campesato
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje CrnjakJavantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
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
Laura Lorenz
 

Similar to Reactive programming in PHP (20)

Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go WrongJDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
 
Reactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NETReactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NET
 
Finagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at PinterestFinagle and Java Service Framework at Pinterest
Finagle and Java Service Framework at Pinterest
 
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
 
Php 5.6 From the Inside Out
Php 5.6 From the Inside OutPhp 5.6 From the Inside Out
Php 5.6 From the Inside Out
 
"Swoole: double troubles in c", Alexandr Vronskiy
"Swoole: double troubles in c", Alexandr Vronskiy"Swoole: double troubles in c", Alexandr Vronskiy
"Swoole: double troubles in c", Alexandr Vronskiy
 
Node.js primer
Node.js primerNode.js primer
Node.js primer
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
Airflow 101
Airflow 101Airflow 101
Airflow 101
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHPphp[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
 
Java 8 - Lambdas and much more
Java 8 - Lambdas and much moreJava 8 - Lambdas and much more
Java 8 - Lambdas and much more
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJS
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje CrnjakJavantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
 
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
 

Recently uploaded

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 

Recently uploaded (20)

Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 

Reactive programming in PHP

  • 2. Reactive Programming = Observer Pattern + LINQ-style operators ( + Schedulers) TL; DR
  • 3. Pros: Good for handling async events Cons: Difficult to understand Too many things to learn Some overhead TL; DR
  • 4. Definition Reactive Programming is programming with asynchronous data streams. https://gist.github.com/ staltz/868e7e9bc2a7b8c1f754 (Where is reactive?)
  • 5. What you heard when you learn reactive programming • Observer/Observable / Hot & Cold • Signal / Flux / Stream / EventStream / EventSource / … • Subject • Scheduler • Publish / Subscribe / Unsubscribe / Dispose / Behavior • Producer / Consumer • Map / Reduce / Throttle / …
  • 6. Observer Pattern Observer 1 Observable watch & run Observer 2 ♨ (Event Source) wrap
  • 7. Observer Pattern Observer 1 Observable subscribe Lazy Evaluation Subscription 1 async call with stream operators Observer 2 Subscription 2 Observers don’t observe Observable does all the work Producer / Consumer may be better naming
  • 8. Observer Pattern Observer 1 Observable 1 Observer 2 Observer 3 Subject 1 Subject 2 Subject = Observable + Observer broadcast ♨ (Event Source) ♨ (Event Source) direct call
  • 9. When to use RP? • One or more event sources • Require stream operators • Not a perfect solution for all async scenarios • External API Call (MSA) • UI Events (Click) • Message Processing • Abstraction over Async Process
  • 10. RxPHP • stream_select (PHP 4 >= 4.3.0, PHP 5, PHP 7) • ReactPHP != RxPHP • ReactPHP is event-driven, non-blocking I/O library • EventLoop, Promise from ReactPHP • https://github.com/ReactiveX/RxPHP
  • 11. RxPHP $source = RxObservable ::fromArray([1, 2, 3, 4]); $observer = new RxObserverCallbackObserver( function ($x) { echo 'Next: ', $x, PHP_EOL; }, function (Exception $ex) { echo 'Error: ', $ex ->getMessage(), PHP_EOL; }, function () { echo 'Completed', PHP_EOL; }); $source ->subscribe($observer); // Next: 1 // Next: 2 // Next: 3 // Next: 4 // Completed Very simple sample code • Cold Observable Sample, almost useless • Source = Observable • Subscribe Observer to Observable • Observer is consist of three functions • next, error, completed
  • 12. RxPHP $loop = Factory ::create(); Scheduler ::setDefaultFactory(function () use ($loop) { return new SchedulerEventLoopScheduler($loop); }); register_shutdown_function(function () use ($loop) { $loop ->run(); }); Observable ::interval(1000) ->take(5) ->flatMap(function ($i) { return Observable ::of($i + 1); }) ->subscribe(function ($e) { echo $e, PHP_EOL; }); // 1 // 2 // 3 // 4 // 5 Event Loop • We need to set EventLoop when using stream operator • interval, throttle, etc.. • If you don’t use stream operator, you don’t need to set EventLoop • RxJs don’t require explicit event loop, because Javascript has setTimeout, setInterval, requestAnimationFrame • There are many scheduler types • flatMap == mergeMap, but RxPHP don’t provide mergeMap
  • 13. RxPHP $subject = new RxSubjectSubject(); $subject ->subscribe(function($i) { echo $i . PHP_EOL;}); RxObservable ::interval(1000) ->take(5) ->flatMap(function ($i) { return Observable ::of($i + 1); }) ->subscribe($subject); $subject ->onNext(10); $subject ->onNext(20); $subject ->onNext(30); // 10 // 20 // 30 // 1 // 2 // 3 // 4 // 5 Subject • Subject = Observable + Observer • Actually Subject is extended from Observable • You don’t need to create custom Observable for simple usage
  • 14. RxPHP More Samples • https://github.com/ReactiveX/RxPHP/tree/master/demo • https://github.com/PacktPublishing/PHP-Reactive- Programming/ • https://www.packtpub.com/web-development/php- reactive-programming
  • 15. RxPHP Ben Lesh’s comment RxJS could be so simple... just a handful of types and operators. ... but if we do that, then people shoot themselves in the foot with memory leaks and their own poorly implemented `expand` operator or whatever… ... so then we add 60+ operators and people are like "WHOA...." haha.. Now it's too complicated. It's tough everywhere. Haha If I had a nickel for every poorly implemented custom RxJS operator I've seen… ... I'd only have about 25 cents, because we've already built almost every tool someone could want. But who have we scared off in the process?