Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Assíncrono com ReactPHP

Zizaco
May 07, 2015

PHP Assíncrono com ReactPHP

PHP vs Node.js

Talk Video (07/05/2015): http://youtu.be/yQxQM5Y-MpE

Leroy Merlin (Laravel)
http://zizaco.net
Luiz Fernando / Zizaco

Request Lifecycle
WebServer
PHP
File System
External Services
Cache

Laravel Request Lifecycle
WebServer
External Services
HTTP Kernel
Service Providers
autoload
Routes
Controller

Express Request Lifecycle
AppServer
Middlewares
Routes
Controller(?)

PHP Approach
PHP HTTP Server

IO Bloqueante
CPU

L1 cache reference
0.000001 ms
Main memory reference
0.0001 ms
Enviar 1 Kb por uma conexão 1 Gbps
0.010 ms
Disk seek
10 ms
Ler 1 MB do HD
20 ms
Round trip Brazil - EUA
150 ms

IO Não Bloqueante How to?
EventLoop & Reactor Pattern
EventLoop is a programming construct that waits for and dispatches events or messages in a program & The reactor design pattern is an event handling pattern ...

select | epoll
A system call to examine the status of file descriptors of open input/output channels. | A scalable I/O event notification mechanism

File-based EventLoop
The select and poll system calls allow a set of file descriptors to be monitored for a change of state, [...] by using files to trigger and listen to events, the loop will block if there are no events ready.

File-based EventLoop
The select and poll system calls allow a set of file descriptors to be monitored for a change of state, [...] by using files to trigger and listen to events, the loop will block if there are no events ready.

ReactPHP HTTP Server

Programando Event Driven
- The good
- The bad
- The ugly

Operações Bloqueantes
$result = syncAction($param1, $param2);

Operações não Bloqueantes
asyncAction($param1, $param2, Callable $callback);
Callback Hell
$promise = asyncAction($param1, $param2);
$promise->done(Callable $callback);

Packages/Libs
90% Bloqueantes
Packages/Libs não Bloqueantes
http://github.com/react/
packages for ReactPHP

ReactPHP's ChildProcess
Roda qualquer processo de forma assíncrona (EventDriven)
http://nodejs.org/api/child_process.html

Quando usar ReactPHP/Node?
Chat
APIs
Queued Input
Data Streaming
Proxy
Monitoring
Web Applications with NoSQL

Quando não usar ReactPHP/Node
Web Applications with Relational Database
Heavy Processing (CPU Intensive)

Node.js VS ReactPHP

Node.js
+ Packages / Libs
Javascript
Compatível com Front-end
+ Maduro

ReactPHP
OOP decente
PHP
Compatível com código Back-end
+ Padronizado

Remember
Don't do cargo cult programming

Trabalhe conosco (:
Backend / Frontend
Subject: [Vaga]

Fontes
ReactPHP
http://reactphp.org/
Why Use Node.js? - Tomislav Capan
http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js
Async PHP With React (Laracon) - Jeremy Mikola
https://www.youtube.com/watch?v=s6xrnYae1FU
Laravel Request Lifecycle
http://laravel.com/docs/5.0/lifecycle
Event Correlation: What you Need to Know For IT Operations Management - Michael Johnson
http://goo.gl/0bZFaP
Callback Hell - Max Ogden
http://callbackhell.com/
Understanding the node.js event loop - Mikito Takada
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Promises - Forbes Lindesay
https://www.promisejs.org/
I/O não bloqueante com select “na unha” (Perl) - Tiago Peczenyj
http://imasters.com.br/desenvolvimento/io-nao-bloqueante-com-ioselect-na-unha/
Reactor Pattern - Douglas C. Schmidt
http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf
Differences between event driven model and reactor pattern? - Jean-Paul Calderone
http://stackoverflow.com/a/9143390
Cargo Cult Programming
http://en.wikipedia.org/wiki/Cargo_cult_programming

Zizaco

May 07, 2015
Tweet

More Decks by Zizaco

Other Decks in Technology

Transcript

  1. PHP Assíncrono com
    ReactPHP
    PHP vs Node.js

    View Slide

  2. Who am I?
    ● Programador
    ● Desenvolvedor web
    ● WebDesigner wannabe
    ● Leroy Merlin (Laravel)
    ● Sempre mais
    http://zizaco.net
    Luiz Fernando / Zizaco

    View Slide

  3. Request Lifecycle
    HTTP://
    WebServer
    PHP
    External Services
    File System
    External Services
    Cache

    View Slide

  4. Laravel Request Lifecycle
    HTTP://
    WebServer
    External Services
    HTTP Kernel Service Providers
    index.php autoload
    Routes
    Controller

    View Slide

  5. View Slide

  6. Express (Node.js)
    Request Lifecycle
    HTTP://
    WebServer
    External Services
    AppServer Middlewares
    index.js require(...)
    Routes
    Controller(?)
    Opcional

    View Slide

  7. Laravel Request Lifecycle
    HTTP://
    WebServer
    External Services
    HTTP Kernel Service Providers
    index.php autoload
    Routes
    Controller

    View Slide

  8. PHP Approach Request Lifecycle
    HTTP://
    WebServer
    External Services
    AppServer Stuff
    index.php autoload
    Routes
    Controller
    Opcional

    View Slide

  9. PHP HTTP Server

    View Slide

  10. View Slide

  11. Node.js HTTP Server

    View Slide

  12. IO Bloqueante
    IO CPU IO CPU IO CPU

    View Slide

  13. IO Bloqueante
    L1 cache reference 0.000001 ms
    Main memory reference 0.0001 ms
    Enviar 1 Kb por uma conexão 1 Gbps 0.010 ms
    Disk seek 10 ms
    Ler 1 MB do HD 20 ms
    Round trip Brazil - EUA 150 ms
    IO CPU IO CPU IO CPU

    View Slide

  14. IO Não Bloqueante
    IO CPU
    IO CPU
    IO CPU

    View Slide

  15. IO Não Bloqueante How to?
    EventLoop & Reactor Pattern

    View Slide

  16. IO Não Bloqueante How to?
    EventLoop & Reactor Pattern
    "EventLoop is a programming construct
    that waits for and dispatches events or
    messages in a program & The reactor
    design pattern is an event handling
    pattern ..."

    View Slide

  17. IO Não Bloqueante How to?
    select | epoll
    "A system call to examine the status of
    file descriptors of open input/output
    channels. | A scalable I/O event
    notification mechanism"

    View Slide

  18. IO Não Bloqueante How to?
    File-based EventLoop
    "The select and poll system calls allow a
    set of file descriptors to be monitored for
    a change of state, [...] by using files to
    trigger and listen to events, the loop will
    block if there are no events ready."

    View Slide

  19. IO Não Bloqueante How to?
    File-based EventLoop
    "The select and poll system calls allow a
    set of file descriptors to be monitored for
    a change of state, [...] by using files to
    trigger and listen to events, the loop will
    block if there are no events ready."

    View Slide

  20. Node.js HTTP Server

    View Slide

  21. ReactPHP HTTP Server

    View Slide

  22. View Slide

  23. View Slide

  24. Programando
    Event Driven
    [✓] The good
    [✓] The bad
    [✓] The ugly

    View Slide

  25. Operações Bloqueantes
    $result = syncAction($param1, $param2);
    asyncAction($param1, $param2, Callable $callback);
    Operações não Bloqueantes

    View Slide

  26. Operações Bloqueantes
    $result = syncAction($param1, $param2);
    asyncAction($param1, $param2, Callable $callback);
    Operações não Bloqueantes
    Callback Hell

    View Slide

  27. Node.js Callback Hell

    View Slide

  28. Operações Bloqueantes
    Operações não Bloqueantes
    $result = syncAction($param1, $param2);
    asyncAction($param1, $param2, Callable $callback);
    $promise = asyncAction($param1, $param2);
    $promise->done(Callable $callback);

    View Slide

  29. Packages/Libs
    Bloqueantes
    Packages/Libs não
    Bloqueantes
    90% github.com/react/*
    &
    packages for
    ReactPHP

    View Slide

  30. ReactPHP's
    ChildProcess
    Roda qualquer processo de forma
    assíncrona (EventDriven)

    View Slide

  31. ReactPHP's
    ChildProcess
    Roda qualquer processo de forma
    assíncrona (EventDriven)

    View Slide

  32. ReactPHP's
    ChildProcess
    Roda qualquer processo de forma
    assíncrona (EventDriven)
    nodejs.org/api/child_process.html

    View Slide

  33. Why?

    View Slide

  34. Quando usar ReactPHP/Node?
    ● Chat
    ● APIs
    ● Queued Input
    ● Data Streaming
    ● Proxy
    ● Monitoring
    ● Web Applications with NoSQL

    View Slide

  35. Quando não usar ReactPHP/Node
    ● Web Applications with Relational Database
    ● Heavy Processing (CPU Intensive)

    View Slide

  36. Node.js ReactPHP
    VS

    View Slide

  37. Node.js ReactPHP
    ● + Packages / Libs
    ● Javascript
    ○ Compatível com
    Front-end
    ● + Maduro
    ● OOP decente
    ● PHP
    ○ Compatível com
    código Back-end
    ● + Padronizado

    View Slide

  38. Node.js ReactPHP
    ● + Packages / Libs
    ● Javascript
    ○ Compatível com
    Front-end
    ● + Maduro
    ● OOP decente
    ● PHP
    ○ Compatível com
    código Back-end
    ● + Padronizado

    View Slide

  39. Remember...

    View Slide

  40. Don't do
    cargo cult programming

    View Slide

  41. Trabalhe conosco (:
    Backend / Frontend
    [email protected]
    Subject: [Vaga]

    View Slide

  42. Fontes
    ReactPHP
    http://reactphp.org/
    Why Use Node.js? - Tomislav Capan
    http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js
    Async PHP With React (Laracon) - Jeremy Mikola
    https://www.youtube.com/watch?v=s6xrnYae1FU
    Laravel Request Lifecycle
    http://laravel.com/docs/5.0/lifecycle
    Event Correlation: What you Need to Know For IT Operations Management - Michael Johnson
    http://goo.gl/0bZFaP
    Callback Hell - Max Ogden
    http://callbackhell.com/

    View Slide

  43. Fontes
    Understanding the node.js event loop - Mikito Takada
    http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
    Promises - Forbes Lindesay
    https://www.promisejs.org/
    I/O não bloqueante com select “na unha” (Perl) - Tiago Peczenyj
    http://imasters.com.br/desenvolvimento/io-nao-bloqueante-com-ioselect-na-unha/
    Reactor Pattern - Douglas C. Schmidt
    http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf
    Differences between event driven model and reactor pattern? - Jean-Paul Calderone
    http://stackoverflow.com/a/9143390
    Cargo Cult Programming
    http://en.wikipedia.org/wiki/Cargo_cult_programming

    View Slide