SlideShare une entreprise Scribd logo
1  sur  16
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Tendances (20)

Asp.net web api
Asp.net web apiAsp.net web api
Asp.net web api
 
HTML 5 Tables and Forms
HTML 5 Tables and FormsHTML 5 Tables and Forms
HTML 5 Tables and Forms
 
Asp.net controls
Asp.net controlsAsp.net controls
Asp.net controls
 
Zipf's law
Zipf's lawZipf's law
Zipf's law
 
HTML frames and HTML forms
HTML frames and HTML formsHTML frames and HTML forms
HTML frames and HTML forms
 
Web application framework
Web application frameworkWeb application framework
Web application framework
 
Html media
Html mediaHtml media
Html media
 
Javascript - Array - Creating Array
Javascript - Array - Creating ArrayJavascript - Array - Creating Array
Javascript - Array - Creating Array
 
Html notes
Html notesHtml notes
Html notes
 
Introduction to indexing (presentation1)
Introduction to indexing (presentation1)Introduction to indexing (presentation1)
Introduction to indexing (presentation1)
 
Understanding world wide web and the internet
Understanding world wide web and the internetUnderstanding world wide web and the internet
Understanding world wide web and the internet
 
Design your first website using HTML
Design your first website using HTMLDesign your first website using HTML
Design your first website using HTML
 
HTML5 - Forms
HTML5 - FormsHTML5 - Forms
HTML5 - Forms
 
Asp net
Asp netAsp net
Asp net
 
Intro to HTML5 audio tag
Intro to HTML5 audio tagIntro to HTML5 audio tag
Intro to HTML5 audio tag
 
Internet basics
Internet basicsInternet basics
Internet basics
 
HTML and XML Difference FAQs
HTML and XML Difference FAQsHTML and XML Difference FAQs
HTML and XML Difference FAQs
 
MySQL Cheat Sheet
MySQL Cheat SheetMySQL Cheat Sheet
MySQL Cheat Sheet
 
Html table tags
Html table tagsHtml table tags
Html table tags
 
Visual Basic menu
Visual Basic menuVisual Basic menu
Visual Basic menu
 

Similaire à Reactive programming in PHP

ZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async PrimerZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async PrimerAdam Englander
 
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 WrongPROIDEA
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHPKing Foo
 
Reactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NETReactive Extensions: classic Observer in .NET
Reactive Extensions: classic Observer in .NETEPAM
 
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 PinterestPavan 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 AirflowPyData
 
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 OutFerenc 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 VronskiyFwdays
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaYardena Meymann
 
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 2ppd1961
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 SlidesYarikS
 
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 PHPAdam Englander
 
Java 8 - Lambdas and much more
Java 8 - Lambdas and much moreJava 8 - Lambdas and much more
Java 8 - Lambdas and much moreAlin Pandichi
 

Similaire à Reactive programming in PHP (20)

Introduction to reactive programming
Introduction to reactive programmingIntroduction to reactive programming
Introduction to reactive programming
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
ZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async PrimerZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async Primer
 
ReactPHP
ReactPHPReactPHP
ReactPHP
 
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
 

Dernier

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 

Dernier (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 

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?