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

Async fun with ReactPHP

Async fun with ReactPHP

WyriHaximus

July 08, 2014
Tweet

More Decks by WyriHaximus

Other Decks in Programming

Transcript

  1. THE REACTPHP CORE: THE EVENT LOOP File descriptor polling One

    off timers Periodic timers Deferred execution of callbacks
  2. OUR JOURNEY STARTS WITH RATCHET w y r i m

    a p s @ w o r k e r 1 : ~ # p h p r a t c h e t . p h p
  3. FIRST THINGS FIRST: THE EVENT LOOP $ l o o

    p = \ R e a c t \ E v e n t L o o p \ F a c t o r y : : c r e a t e ( ) ; There can be only one per process!
  4. SETTING UP RATCHET THE SIMPLE WAY $ a p p

    = n e w R a t c h e t \ A p p ( ' w y r i m a p s . n e t ' , 8 0 0 1 , ' 0 . 0 . 0 . 0 ' , $ l o o p ) ; $ a p p - > r o u t e ( ' / w s ' , n e w W y r i m a p s N e t ( $ l o o p ) ; $ a p p - > r u n ( ) ;
  5. SETTING UP RATCHET THE ADVANCED WAY $ s o c

    k e t = n e w R e a c t o r ( $ l o o p ) ; $ s o c k e t - > l i s t e n ( 8 0 0 1 , ' 0 . 0 . 0 . 0 ' ) ; $ i o S e r v e r = n e w I o S e r v e r ( n e w H t t p S e r v e r ( n e w W s S e r v e r ( n e w S e s s i o n P r o v i d e r ( n e w W a m p S e r v e r ( n e w C a k e W a m p A p p S e r v e r ( . . . ) ) , n e w C a k e W a m p S e s s i o n H a n d l e r ( ) , [ ] , n e w P h p S e r i a l i z e H a n d l e r ( ) ) ) ) , $ s o c k e t , $ l o o p ) ;
  6. WE USE PHUNINNODE TO MONITOR RATCHET w y r i

    m a p s @ w o r k e r 1 : ~ # p h p p h u n i n N o d e . p h p
  7. A DIFFERENT PROCESS ANOTHER EVENT LOOP $ l o o

    p = \ R e a c t \ E v e n t L o o p \ F a c t o r y : : c r e a t e ( ) ;
  8. SOCKET SERVER $ s o c k e t =

    n e w \ R e a c t \ S o c k e t \ S e r v e r ( $ l o o p ) ; $ s o c k e t - > l i s t e n ( 1 2 3 4 5 , ' 0 . 0 . 0 . 0 ' ) ;
  9. SETTING UP PHUNINNODE $ n o d e = n

    e w \ W y r i H a x i m u s \ P h u n i n N o d e \ N o d e ( $ l o o p , $ s o c k e t ) ; $ n o d e - > a d d P l u g i n ( n e w \ W y r i H a x i m u s \ P h u n i n N o d e \ P l u g i n s \ P l u g i n s ( ) ) ; $ n o d e - > a d d P l u g i n ( n e w \ W y r i H a x i m u s \ P h u n i n N o d e \ P l u g i n s \ P l u g i n s C a t e g o r i e s ( ) ) ; $ n o d e - > a d d P l u g i n ( n e w \ W y r i H a x i m u s \ P h u n i n N o d e \ P l u g i n s \ M e m o r y U s a g e ( ) ) ; $ n o d e - > a d d P l u g i n ( n e w \ W y r i H a x i m u s \ P h u n i n N o d e \ P l u g i n s \ U p t i m e ( ) ) ;
  10. RATCHET RECEIVES AND ROUTES RPC CALL c l a s

    s C a k e W a m p A p p S e r v e r i m p l e m e n t s R a t c h e t \ W a m p \ W a m p S e r v e r I n t e r f a c e { p u b l i c f u n c t i o n o n C a l l ( C o n n $ c o n n , $ i d , $ t o p i c , a r r a y $ p a r a m s ) { / / C a l l t h e r e s p o n s i b l e e v e n t l i s t e n e r } }
  11. PROMISES $ d e f e r r e d

    = n e w \ R e a c t \ P r o m i s e \ D e f e r r e d ( ) ; $ d e f e r r e d - > p r o m i s e ( ) - > t h e n ( f u n c t i o n ( $ r e s u l t s ) u s e ( $ c o n n , $ i d ) { $ c o n n - > c a l l R e s u l t ( $ i d , $ r e s u l t s ) ; } , f u n c t i o n ( $ e r r o r U r i , $ d e s c = ' ' , $ d e t a i l s = n u l l ) u s e ( $ c o n n ) { $ c o n n - > c a l l E r r o r ( $ i d , $ e r r o r U r i , $ d e s c , $ d e t a i l s ) ; } ) ;
  12. FIRE THE EVENT $ t h i s - >

    d i s p a t c h E v e n t ( ' R a c h e t . W a m p S e r v e r . R p c . ' . $ t o p i c N a m e , [ ' p r o m i s e ' = > $ d e f e r r e d , ' t o p i c ' = > $ t o p i c , ' p a r a m s ' = > $ p a r a m s , ' w a m p S e r v e r ' = > $ t h i s , ] ) ) ;
  13. HTTPCLIENT PREREQUISITES $ d n s R e s o

    l v e r F a c t o r y = n e w \ R e a c t \ D n s \ R e s o l v e r \ F a c t o r y ( ) ; $ d n s R e s o l v e r = $ d n s R e s o l v e r F a c t o r y - > c r e a t e C a c h e d ( ' 8 . 8 . 8 . 8 ' , $ l o o p ) ;
  14. PREPARING THE HTTPCLIENT $ f a c t o r

    y = n e w \ R e a c t \ H t t p C l i e n t \ F a c t o r y ( ) ; $ c l i e n t = $ f a c t o r y - > c r e a t e ( $ l o o p , $ d n s R e s o l v e r ) ;
  15. QUERYING ELASTICSEARCH $ r e q u e s t

    = $ c l i e n t - > r e q u e s t ( ' G E T ' , ' U R L ' , [ / * H e a d e r s * / ] ) ; $ r e q u e s t - > o n ( ' h e a d e r s - w r i t t e n ' , f u n c t i o n ( $ t h a t ) u s e ( $ s e a r c h ) { $ t h a t - > w r i t e ( ' J S O N q u e r y b o d y ' ) ; } ) ; $ r e q u e s t - > o n ( ' r e s p o n s e ' , f u n c t i o n ( $ r e s p o n s e ) { $ r e s p o n s e - > o n ( ' d a t a ' , f u n c t i o n ( $ d a t a ) { / / . . . } ) ; } ) ; $ r e q u e s t - > e n d ( ) ;
  16. FULFILLING THE PROMISE $ r e q u e s

    t - > o n ( ' e n d ' , f u n c t i o n ( ) u s e ( $ p r o m i s e , & $ b u f f e r ) { $ p r o m i s e - > r e s o l v e ( $ b u f f e r ) ; } ) ;
  17. SENDING THE RESULT BACK TO THE CLIENT f u n

    c t i o n ( $ r e s u l t s ) u s e ( $ c o n n , $ i d ) { $ c o n n - > c a l l R e s u l t ( $ i d , $ r e s u l t s ) ; }
  18. ACCEPTING THE CONNECTION $ s o c k e t

    - > o n ( ' c o n n e c t i o n ' , f u n c t i o n ( C o n n e c t i o n $ c o n n ) { } ) ;
  19. HANDLING INCOMING DATA $ b u f f e r

    = ' ' ; $ c o n n - > o n ( ' d a t a ' , f u n c t i o n ( $ d a t a , $ c o n n ) u s e ( & $ b u f f e r ) { $ b u f f e r . = $ d a t a ; i f ( s t r p o s ( $ b u f f e r , P H P _ E O L ) ! = = f a l s e ) { / / Y a y l e t s l o o k s o m e t h i n g u p } } ) ;
  20. CONTACTING RATCHET OVER 0MQ $ z m q = n

    e w Z M Q C o n t e x t ( 1 ) ; $ s e r v e r C o n n e c t i o n = $ z m q - > g e t S o c k e t ( Z M Q : : S O C K E T _ R E Q ) ; $ s e r v e r C o n n e c t i o n - > c o n n e c t ( ' t c p : / / 1 2 7 . 0 . 0 . 1 : 5 5 5 5 / ' ) ; $ s e r v e r C o n n e c t i o n - > s e n d ( s e r i a l i z e ( $ c o m m a n d ) ) ;
  21. ON THE RATCHET SIDE $ c o n t e

    x t = n e w \ R e a c t \ Z M Q \ C o n t e x t ( $ l o o p ) ; $ s o c k e t = $ c o n t e x t - > g e t S o c k e t ( Z M Q : : S O C K E T _ R E P ) ; $ s o c k e t - > b i n d ( ' t c p : / / 1 2 7 . 0 . 0 . 1 : 5 5 5 5 / ' ) ; $ s o c k e t - > o n ( ' m e s s a g e ' , f u n c t i o n ( $ m e s s a g e ) u s e ( $ e v e n t , $ s o c k e t ) { $ c o m m a n d = u n s e r i a l i z e ( $ m e s s a g e ) ; $ s o c k e t - > s e n d ( s e r i a l i z e ( $ c o m m a n d - > e x e c u t e ( $ e v e n t - > s u b j e c t ( ) ) ) ) ; } ) ;
  22. BACK AT PHUNINNODE $ c o m m a n

    d - > r e s p o n s e ( u n s e r i a l i z e ( $ s e r v e r C o n n e c t i o n - > r e c v ( ) ) ) ;
  23. PASSING DATA BACK TO MUNIN $ c o n n

    - > w r i t e ( " k e y . v a l u e 1 2 3 \ n " ) ;
  24. PUBSUB $ p r e d i s - >

    s e t ( ' w y r i m a p s _ n e t : s t r e e t v i e w : g e n e r a t o r : s t e p ' , 1 2 3 ) ; $ p r e d i s - > s e t ( ' w y r i m a p s _ n e t : s t r e e t v i e w : g e n e r a t o r : s t a t u s ' , ' S o m e s t a t u s m e s s a g e ' ) ;
  25. PUBSUB $ p u b s u b P r

    e d i s - > p u b s u b ( ' _ _ k e y s p a c e @ 6 _ _ : w m n : s t r v : g e n : s ' , f u n c t i o n ( $ e v e n t ) { $ p u b s u b P r e d i s G e t - > g e t ( ' w m n : s t r v : g e n : s ' , f u n c t i o n ( $ s t a t u s , $ c l i e n t ) { $ t h i s - > b r o a d c a s t ( ' W y r i m a p s N e t . S t r v G e n . U p d a t e s . S ' , [ ' s t e p ' = > $ s t a t u s ] ) ; } ) ; } ) ;
  26. PACKAGES BUILD ON REACT RobinvdVleuten/react-nntp beaucollins/phluid-php bergie/dnode-php bixuehujin/reactphp-mysql bzikarsky/gearman-async cboden/Ratchet

    clue/buzz-react clue/icmp-react clue/psocksd clue/redis-react From: https://github.com/reactphp/react/wiki/Users
  27. PACKAGES BUILD ON REACT clue/redis-server clue/socket-react clue/socks-react clue/wol-react clue/zenity-react evan108108/react-mongrel2

    ianbarber/eep-php jolicode/php-ar-drone markwilson/publictweetstream-php mkjpryor/async From: https://github.com/reactphp/react/wiki/Users
  28. PACKAGES BUILD ON REACT mkraemer/react-inotify mkraemer/react-pcntl nrk/predis-async phergie3 recoil/recoil rithis-archive/find-your-domain

    sculpin umpirsky/wisdom wyrihaximus/phunin-node wyrihaximus/react-guzzle From: https://github.com/reactphp/react/wiki/Users
  29. $LOOP->STOP(); A talk by / Cees-Jan Kiewiet @WyriHaximus Please rate

    this talk: https://joind.in/11455 Questions?