Slide 1

Slide 1 text

pushing the limits with ReactPHP why ReactPHP is awesome and why YOU should care

Slide 2

Slide 2 text

Agenda - Hello! - PHP, the web of the ‘90s? - Enter React - Core components - Examples and demo time - Conclusions 2

Slide 3

Slide 3 text

Hello! 3

Slide 4

Slide 4 text

$ whoami Christian Lück 4

Slide 5

Slide 5 text

$ whoami Christian Lueck 5

Slide 6

Slide 6 text

$ whoami Christian Lueck 6

Slide 7

Slide 7 text

$ whoami Christian Lueck @clue 7

Slide 8

Slide 8 text

$ whoami Christian Lueck @another_clue 8

Slide 9

Slide 9 text

$ whoami Christian Lueck @another_clue passionate about pushing the limits 9

Slide 10

Slide 10 text

$ whoami Christian Lueck @another_clue passionate about pushing the limits freelance software engineer 10

Slide 11

Slide 11 text

Who are you? 11 now that you know me…

Slide 12

Slide 12 text

Who are you? 12 now that you know me… - PHP developers? - architecs / engineers?

Slide 13

Slide 13 text

Who are you? 13 now that you know me… - PHP developers? - architecs / engineers? - know React?

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

The other React™ 15

Slide 16

Slide 16 text

The REAL React™ 16

Slide 17

Slide 17 text

PHP, the web of the ‘90s? 17

Slide 18

Slide 18 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle 18 Apache Client PHP MySQL

Slide 19

Slide 19 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 19 Apache Client PHP MySQL

Slide 20

Slide 20 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 20 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP

Slide 21

Slide 21 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 21 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP

Slide 22

Slide 22 text

PHP and the web of the ‘90s - traditional LAMP stack - Request-Response-Cycle - PHP is too slow? - We sure can improve this… 22 Apache Client PHP MySQL Apache Client FPM MySQL PHP PHP nginx Client FPM MySQL PHP PHP nginx Client FPM memcache PHP PHP MySQL

Slide 23

Slide 23 text

PHP may not be pretty… 23

Slide 24

Slide 24 text

PHP may not be pretty… but it gets the job done! 24

Slide 25

Slide 25 text

Knock knock! Who’s there? 25

Slide 26

Slide 26 text

Knock knock! 2017! Who’s there? 26

Slide 27

Slide 27 text

Knock knock! 2017! - Separation of concerns (Frontend↔Backend) - HTTP APIs (RESTful) - Integration with 3rd parties - Live-Data (ticker) - CLI tools Who’s there? 27

Slide 28

Slide 28 text

nodejs? 28

Slide 29

Slide 29 text

no js! 29

Slide 30

Slide 30 text

Enter React! 30

Slide 31

Slide 31 text

What is React? 31

Slide 32

Slide 32 text

What is React? non-blocking I/O 32

Slide 33

Slide 33 text

What is React? non-blocking I/O event-driven 33

Slide 34

Slide 34 text

What is React? non-blocking I/O event-driven async 34

Slide 35

Slide 35 text

100% 35

Slide 36

Slide 36 text

100% pure PHP 36

Slide 37

Slide 37 text

100% pure PHP no extensions 37

Slide 38

Slide 38 text

100% pure PHP no extensions no magic 38

Slide 39

Slide 39 text

What does that even mean?! 39

Slide 40

Slide 40 text

the idea 40

Slide 41

Slide 41 text

the idea calculations are fast 41

Slide 42

Slide 42 text

the idea calculations are fast I/O is slow 42

Slide 43

Slide 43 text

I/O is everywhere 43

Slide 44

Slide 44 text

I/O is everywhere third party HTTP APIs (RESTful, SOAP, you name it…) 44

Slide 45

Slide 45 text

I/O is everywhere third party HTTP APIs (RESTful, SOAP, you name it…) mysql, postgres 45

Slide 46

Slide 46 text

I/O is everywhere third party HTTP APIs (RESTful, SOAP, you name it…) mysql, postgres filesystem I/O (session files) 46

Slide 47

Slide 47 text

I/O is everywhere third party HTTP APIs (RESTful, SOAP, you name it…) mysql, postgres filesystem I/O (session files) redis, memcache 47

Slide 48

Slide 48 text

48 Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832 CPU vs I/O

Slide 49

Slide 49 text

I/O is slow! 49

Slide 50

Slide 50 text

I/O is slow! So why wait? 50

Slide 51

Slide 51 text

This is React 51

Slide 52

Slide 52 text

This is React 52 Start multiple I/O operations (non-blocking)

Slide 53

Slide 53 text

This is React 53 Start multiple I/O operations (non-blocking) Get notified when something happens (react)

Slide 54

Slide 54 text

This is React 54 Start multiple I/O operations (non-blocking) Get notified when something happens (react) Don’t waste time waiting

Slide 55

Slide 55 text

What React is not 55

Slide 56

Slide 56 text

What React is not - React is not black magic / vodoo - Does not make your code faster magically 56

Slide 57

Slide 57 text

What React is not - React is not black magic / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) 57

Slide 58

Slide 58 text

What React is not - React is not black magic / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) - React is not a replacement for your favorite framework - Symfony is here to stay 58

Slide 59

Slide 59 text

What React is not - React is not black magic / vodoo - Does not make your code faster magically - React is not a framework - Indepentent components (libraries) - React is not a replacement for your favorite framework - Symfony is here to stay - React is not the new buzz - React is here to stay as well (started in 2012) 59

Slide 60

Slide 60 text

React core components 60

Slide 61

Slide 61 text

React Core components - Event loop (reactor) - Streams - Promises 61

Slide 62

Slide 62 text

Event loop Consumers - THE core, low-level component 62

Slide 63

Slide 63 text

Event loop Consumers - THE core, low-level component - Create an instance - Just use the Factory - Additional extensions for bigger payloads - something inbetween… - just pass the $loop around - Start running - keeps running forever - unless stopped or done 63

Slide 64

Slide 64 text

Event loop Consumers - THE core, low-level component - Create an instance - Just use the Factory - Additional extensions for bigger payloads - something inbetween… - just pass the $loop around - Start running - keeps running forever - unless stopped or done 64 $loop = Factory::create(); // something inbetween // pass the $loop around to all components $loop->run();

Slide 65

Slide 65 text

Event loop Implementors - Reactor pattern (hence the name) 65

Slide 66

Slide 66 text

Event loop Implementors - Reactor pattern (hence the name) - start timers - once - periodic - ticks 66 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; });

Slide 67

Slide 67 text

Event loop Implementors - Reactor pattern (hence the name) - start timers - once - periodic - ticks - wait for stream resources to become - readable - writable 67 $loop->addTimer(0.5, function () { echo ‘world’; }); $loop->addTimer(0.3, function () { echo ‘hello’; }); $loop->addReadStream($stream, $fn); $loop->addWriteStream($stream, $fn);

Slide 68

Slide 68 text

Streams - Process large strings in chunks as they happen (think downloads) - Types - Readable (e.g. STDIN pipe) - Writable (e.g. STDOUT pipe) - Duplex (e.g. TCP/IP connection) 68

Slide 69

Slide 69 text

Streams - interfaces, events and listeners: 69 $dest->write(‘hello’); $source->on(‘data’, function ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; });

Slide 70

Slide 70 text

Streams - interfaces, events and listeners: 70 $dest->write(‘hello’); $source->on(‘data’, function ($data) { var_dump($data); }); $source->on(‘close’, function () { echo ‘stream closed’; }); $source->pipe($gunzip)->pipe($badwords)->pipe($dest);

Slide 71

Slide 71 text

Promises - Placeholder for a single future result - Possible states: - pending - fulfilled (successfully resolved) - rejected (Exception occured) 71

Slide 72

Slide 72 text

Promises - no more imperative code flow - instead (tell, don’t ask) 72 $a->then($fulfilled = null, $rejected = null); $a->then(‘process’); $a->then(‘process’, ‘var_dump’);

Slide 73

Slide 73 text

Examples and demo time! 73

Slide 74

Slide 74 text

Socket server 74 react/socket - THE canonical chat example - broadcast all incoming msgs

Slide 75

Slide 75 text

Socket server 75 react/socket - THE canonical chat example - broadcast all incoming msgs - run example server - connect via telnet: $ telnet ipc.clue.engineering 6001

Slide 76

Slide 76 text

76

Slide 77

Slide 77 text

HTTP client 77 clue/buzz-react - Simple HTTP requests - inspired by kriswallsmith/buzz - PSR-7 compatible

Slide 78

Slide 78 text

HTTP client 78 clue/buzz-react - Simple HTTP requests - inspired by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast…

Slide 79

Slide 79 text

HTTP client 79 clue/buzz-react - Simple HTTP requests - inspired by kriswallsmith/buzz - PSR-7 compatible - Promises and Streams - It’s fast… - benchmarks in following slides about clue/docker-react

Slide 80

Slide 80 text

Packagist API clue/packagist-api-react - get information about any Composer package - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 80

Slide 81

Slide 81 text

Packagist API clue/packagist-api-react - get information about any Composer package - simple, Promise-based - lightweight wrapper between - KnpLabs/packagist-api - clue/buzz-react 81 - see its examples $ php examples/search.php

Slide 82

Slide 82 text

Docker client clue/docker-react - Run apps in isolated containers - “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 82

Slide 83

Slide 83 text

Docker client clue/docker-react - Run apps in isolated containers - “build, ship and run, anywhere” - Controlled through HTTP API - Promises and Streams 83 - see its promise examples $ php examples/info.php - see its streaming examples $ php examples/benchmark-exec.php

Slide 84

Slide 84 text

700 MiB/s 84

Slide 85

Slide 85 text

700 MiB/s 85 dockerd maxed out PHP not

Slide 86

Slide 86 text

HTTP server react/http - Pure PHP, with no additional webserver - standard PSR-7 interfaces 86

Slide 87

Slide 87 text

HTTP server react/http - Pure PHP, with no additional webserver - standard PSR-7 interfaces - Lots of third-party integrations with traditional frameworks (symfony, slim, silex, PIMF etc.) 87

Slide 88

Slide 88 text

5k requests/s 88

Slide 89

Slide 89 text

5k requests/s 89 this is a local single core benchmark!

Slide 90

Slide 90 text

5k requests/s 90 this is a local single core benchmark! dual core i3 => 10k requests/s

Slide 91

Slide 91 text

5k requests/s 91 this is a local single core benchmark! dual core i3 => 10k requests/s 36M requests/h

Slide 92

Slide 92 text

Server sent events clue/sse-react - Server sent events (SSE) - aka. EventSource (browser API) - Streaming events to browser - limited browser support 92

Slide 93

Slide 93 text

Server sent events clue/sse-react - Server sent events (SSE) - aka. EventSource (browser API) - Streaming events to browser - limited browser support 93 - see examples connecting to initial chat $ php examples/chat-server.php - open browser: http://ipc.clue.engineering:7000/

Slide 94

Slide 94 text

94

Slide 95

Slide 95 text

Websocket server cboden/ratchet - Async WebSocket server - bidirectional data flow between browser and server - better browser support 95

Slide 96

Slide 96 text

Redis client 96 clue/redis-react - Redis is a fast in-memory DB - very simple commands - very simple protocol - pipelined, Promise-based

Slide 97

Slide 97 text

Redis client 97 clue/redis-react - Redis is a fast in-memory DB - very simple commands - very simple protocol - pipelined, Promise-based - see its examples $ php examples/incr.php

Slide 98

Slide 98 text

Redis server 98

Slide 99

Slide 99 text

Redis server 99 - Official Redis is written in C

Slide 100

Slide 100 text

Redis server 100 clue/php-redis-server - Official Redis is written in C - Reimplementation is pure PHP

Slide 101

Slide 101 text

Redis server 101 clue/php-redis-server - Official Redis is written in C - Reimplementation is pure PHP - Very simple to add commands

Slide 102

Slide 102 text

Redis server 102 clue/php-redis-server - Official Redis is written in C - Reimplementation is pure PHP - Very simple to add commands - How fast could PHP possibly be? Let’s see…

Slide 103

Slide 103 text

Redis server 103 clue/php-redis-server - Official Redis is written in C - Reimplementation is pure PHP - Very simple to add commands - How fast could PHP possibly be? Let’s see… - see its bin $ php bin/redis-server.php - test via clue/redis-react - test via official redis CLI - run official redis benchmark during talk: - official server: ~90k OP/s - PHP server: ~50k OP/s

Slide 104

Slide 104 text

50k OP/s 104

Slide 105

Slide 105 text

50k OP/s pure PHP who needs native code anyway? 105

Slide 106

Slide 106 text

Redis framework 106 clue/php-redis-framework - Development preview - Very simple to add custom commands

Slide 107

Slide 107 text

Redis framework 107 clue/php-redis-framework - Development preview - Very simple to add custom commands - run example server $ php examples/11-beer.php - connect via telnet: $ telnet IP 9000

Slide 108

Slide 108 text

108

Slide 109

Slide 109 text

UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 109

Slide 110

Slide 110 text

UPnP clue/ssdp-react - Univeral Plug and Play (UPnP) - Simple Service Discovery Protocol (SSDP) - dicover UPnP-enabled devices - printers - routers - multi media devices 110 - see its examples $ php examples/search.php

Slide 111

Slide 111 text

Zenity clue/zenity-react - PHP desktop GUI applications - very simple, Promise-based 111

Slide 112

Slide 112 text

Zenity clue/zenity-react - PHP desktop GUI applications - very simple, Promise-based 112 - see its simple examples $ php examples/01-dialog.php - see its more realistic examples $ php examples/06-menu.php $ php examples/03-progress-pulsate.php $ php examples/03-progress-random.php

Slide 113

Slide 113 text

113

Slide 114

Slide 114 text

114

Slide 115

Slide 115 text

115

Slide 116

Slide 116 text

many, MANY more third-party projects: https://github.com/reactphp/react/wiki/Users 116

Slide 117

Slide 117 text

Conclusions 117

Slide 118

Slide 118 text

Conclusions 118 - React is a real deal and here to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues)

Slide 119

Slide 119 text

Conclusions 119 - React is a real deal and here to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues) - React does not make your code faster magically

Slide 120

Slide 120 text

Conclusions 120 - React is a real deal and here to stay - React is stable and production ready - predictable releases - but possibly feature-incomplete (check open issues) - React does not make your code faster magically - Consider using React whenever you’re having to wait - Consider using React whenever you’re accessing the network

Slide 121

Slide 121 text

PHP is faster than you probably thought 121

Slide 122

Slide 122 text

PHP is more versatile than you probably thought 122

Slide 123

Slide 123 text

Need help? Want to help? - check each component’s README - check open issues 123

Slide 124

Slide 124 text

Need help? Want to help? - check each component’s README - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp 124

Slide 125

Slide 125 text

Need help? Want to help? - check each component’s README - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp - Talk to me 125

Slide 126

Slide 126 text

Need help? Want to help? - check each component’s README - check open issues - join #reactphp on irc.freenode.org - tweet @ReactPHP or #reactphp - Talk to me Did I mention I’m available? 126

Slide 127

Slide 127 text

// thank you! $loop->stop(); 127 @another_clue – https://lueck.tv/

Slide 128

Slide 128 text

Avoid blocking! - The loop must not be blocked 128

Slide 129

Slide 129 text

Avoid blocking! - The loop must not be blocked - Many functions / lib assume blocking by default - Anything >1ms should be reconsidered 129

Slide 130

Slide 130 text

Avoid blocking! - The loop must not be blocked - Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams 130

Slide 131

Slide 131 text

Avoid blocking! - The loop must not be blocked - Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams - Need a blocking function? - Fork off! - Use IPC 131

Slide 132

Slide 132 text

Avoid blocking! - The loop must not be blocked - Many functions / lib assume blocking by default - Anything >1ms should be reconsidered - Alternatives - Single result: Promises - Evented: Streams - Need a blocking function? - Fork off! - Use IPC 132 Pay attention: - PDO, mysql etc. - file system access - network access - third-party APIs

Slide 133

Slide 133 text

Integration with traditional environments 133 integrating async into sync is easy

Slide 134

Slide 134 text

Integration with traditional environments 134 integrating async into sync is easy - just run the loop until you’re done - see clue/block-react

Slide 135

Slide 135 text

Integration with traditional environments 135 integrating async into sync is easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard

Slide 136

Slide 136 text

Integration with traditional environments 136 integrating async into sync is easy - just run the loop until you’re done - see clue/block-react integrating sync into async is hard - often requires async rewrite - consider forking instead