getting started with ReactPHP
pushing real-time data to the browser
@another_clue
Slide 2
Slide 2 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
2
Slide 3
Slide 3 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
3
Slide 4
Slide 4 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
4
Slide 5
Slide 5 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
5
Slide 6
Slide 6 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
6
Slide 7
Slide 7 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
7
Slide 8
Slide 8 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
- Streaming HTTP
8
Slide 9
Slide 9 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
- Streaming HTTP
- Chat bots
9
Slide 10
Slide 10 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
- Streaming HTTP
- Chat bots
- interactive CLI tools
10
Slide 11
Slide 11 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
- Streaming HTTP
- Chat bots
- interactive CLI tools
- desktop GUI
11
Slide 12
Slide 12 text
@another_clue
Agenda
12
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- real time data
- Streaming HTTP
- Chat bots
- interactive CLI tools
- desktop GUI
- Conclusions
Slide 13
Slide 13 text
@another_clue
really?
13
Slide 14
Slide 14 text
@another_clue
this is a getting started guide
14
Slide 15
Slide 15 text
@another_clue
pushing real-time data
to the browser
15
Slide 16
Slide 16 text
@another_clue
I encourage feedback,
questions + suggestions
16
Slide 17
Slide 17 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- conclusions
17
Slide 18
Slide 18 text
@another_clue
Agenda
- Hello!
- Introduction to async PHP with ReactPHP
- event loops
- streaming data
- sockets
- promises
- conclusions
18
examples and demo time!
Slide 19
Slide 19 text
@another_clue
Hello!
19
Slide 20
Slide 20 text
@another_clue
$ whoami
20
Slide 21
Slide 21 text
@another_clue
$ whoami
Christian Lück
21
Slide 22
Slide 22 text
@another_clue
$ whoami
Christian Lück
22
Slide 23
Slide 23 text
@another_clue
$ whoami
Christian Lueck
23
Slide 24
Slide 24 text
@another_clue
$ whoami
Christian Lueck
24
Slide 25
Slide 25 text
@another_clue
$ whoami
Christian Lueck
@clue
25
Slide 26
Slide 26 text
@another_clue
$ whoami
Christian Lueck
@another_clue
26
Slide 27
Slide 27 text
@another_clue
$ whoami
Christian Lueck
@another_clue
passionate about pushing the limits
27
Slide 28
Slide 28 text
@another_clue
$ whoami
Christian Lueck
@another_clue
passionate about pushing the limits
freelance software engineer 28
Slide 29
Slide 29 text
@another_clue
Who are you?
29
now that you know me…
Slide 30
Slide 30 text
@another_clue
Who are you?
30
now that you know me…
- PHP developers?
- architecs / engineers?
Slide 31
Slide 31 text
@another_clue
Who are you?
31
now that you know me…
- PHP developers?
- architecs / engineers?
- know React?
Slide 32
Slide 32 text
@another_clue
PHP, the web of the ‘90s?
32
Slide 33
Slide 33 text
@another_clue
PHP and the web of the ‘90s
33
Slide 34
Slide 34 text
@another_clue
PHP and the web of the ‘90s
34
Apache
Client PHP MySQL
Slide 35
Slide 35 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
35
Apache
Client PHP MySQL
Slide 36
Slide 36 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
Request-Response-Cycle
36
Apache
Client PHP MySQL
Slide 37
Slide 37 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
Request-Response-Cycle
PHP is too slow? 37
Apache
Client PHP MySQL
Slide 38
Slide 38 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
Request-Response-Cycle
PHP is too slow? 38
Apache
Client MySQL
FPM
PHP
PHP
Slide 39
Slide 39 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
Request-Response-Cycle
PHP is too slow? 39
nginx
Client MySQL
FPM
PHP
PHP
Slide 40
Slide 40 text
@another_clue
PHP and the web of the ‘90s
traditional LAMP stack
Request-Response-Cycle
PHP is too slow? 40
nginx
Client
FPM
PHP
PHP memcache
MySQL
Slide 41
Slide 41 text
@another_clue
PHP may not be pretty…
41
Slide 42
Slide 42 text
@another_clue
PHP may not be pretty…
but it gets the job done!
42
@another_clue
PHP
56
gets the job done
widespread usage
Slide 57
Slide 57 text
@another_clue
PHP
57
gets the job done
widespread usage
if PHP can do it…
Slide 58
Slide 58 text
@another_clue
Enter React!
58
Slide 59
Slide 59 text
@another_clue 59
Slide 60
Slide 60 text
@another_clue
The other React™
60
Slide 61
Slide 61 text
@another_clue
The REAL React™
61
Slide 62
Slide 62 text
@another_clue 62
Slide 63
Slide 63 text
@another_clue
What is React?
63
Slide 64
Slide 64 text
@another_clue
What is React?
non-blocking I/O
64
Slide 65
Slide 65 text
@another_clue
What is React?
non-blocking I/O
event-driven
65
Slide 66
Slide 66 text
@another_clue
What is React?
non-blocking I/O
event-driven
async
66
Slide 67
Slide 67 text
@another_clue
100%
67
Slide 68
Slide 68 text
@another_clue
100%
pure PHP
68
Slide 69
Slide 69 text
@another_clue
100%
pure PHP
no extensions
69
Slide 70
Slide 70 text
@another_clue
100%
pure PHP
no extensions
no magic
70
Slide 71
Slide 71 text
@another_clue
What does that even mean?!
71
Slide 72
Slide 72 text
@another_clue
the idea
72
Slide 73
Slide 73 text
@another_clue
calculations are fast
73
the idea
Slide 74
Slide 74 text
@another_clue
calculations are fast
I/O is slow
74
the idea
Slide 75
Slide 75 text
@another_clue 75
Source: Latency Numbers Every Programmer Should Know: https://gist.github.com/jboner/2841832
CPU vs I/O
Slide 76
Slide 76 text
@another_clue
I/O is everywhere
76
Slide 77
Slide 77 text
@another_clue
I/O is everywhere
third party HTTP APIs (RESTful, SOAP, you name it…)
77
Slide 78
Slide 78 text
@another_clue
I/O is everywhere
third party HTTP APIs (RESTful, SOAP, you name it…)
mysql, postgres
78
Slide 79
Slide 79 text
@another_clue
I/O is everywhere
third party HTTP APIs (RESTful, SOAP, you name it…)
mysql, postgres
filesystem I/O (session files)
79
Slide 80
Slide 80 text
@another_clue
I/O is everywhere
third party HTTP APIs (RESTful, SOAP, you name it…)
mysql, postgres
filesystem I/O (session files)
redis, memcache 80
Slide 81
Slide 81 text
@another_clue
I/O is slow!
81
Slide 82
Slide 82 text
@another_clue
I/O is slow!
So why wait?
82
Slide 83
Slide 83 text
@another_clue
This is React
83
Slide 84
Slide 84 text
@another_clue
This is React
84
Start multiple I/O operations (non-blocking)
Slide 85
Slide 85 text
@another_clue
This is React
85
Start multiple I/O operations (non-blocking)
Get notified when something happens (react)
Slide 86
Slide 86 text
@another_clue
This is React
86
Start multiple I/O operations (non-blocking)
Get notified when something happens (react)
Don’t waste time waiting
Slide 87
Slide 87 text
@another_clue
What React is not
87
Slide 88
Slide 88 text
@another_clue
What React is not
React is not black magic / vodoo
88
Slide 89
Slide 89 text
@another_clue
What React is not
React is not black magic / vodoo
React is not a framework
89
Slide 90
Slide 90 text
@another_clue
What React is not
React is not black magic / vodoo
React is not a framework
React is not the new buzz
90
Slide 91
Slide 91 text
@another_clue
React core components
91
Slide 92
Slide 92 text
@another_clue
event loop
92
Slide 93
Slide 93 text
@another_clue
Event loop
Consumers
- THE core, low-level component
93
Slide 94
Slide 94 text
@another_clue
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
94
Slide 95
Slide 95 text
@another_clue
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
95
@another_clue
Event loop
Implementors
- Reactor pattern (hence the name)
- start timers
- once
- periodic
- ticks
97
Slide 98
Slide 98 text
@another_clue
Event loop
Implementors
- Reactor pattern (hence the name)
- start timers
- once
- periodic
- ticks
98
Slide 99
Slide 99 text
@another_clue
Event loop
Implementors
- Reactor pattern (hence the name)
- start timers
- once
- periodic
- ticks
99
Slide 100
Slide 100 text
@another_clue
Event loop
Implementors
- Reactor pattern (hence the name)
- start timers
- once
- periodic
- ticks
- plus wait for stream resources to become
- readable
- writable
100
Slide 101
Slide 101 text
@another_clue
streaming data
101
Slide 102
Slide 102 text
@another_clue
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)
102
Slide 103
Slide 103 text
@another_clue
Stream interfaces and events
103
Slide 104
Slide 104 text
@another_clue
Stream interfaces and events
104
Slide 105
Slide 105 text
@another_clue
Stream interfaces and events
105
Slide 106
Slide 106 text
@another_clue
sockets
106
Slide 107
Slide 107 text
@another_clue
Sockets
107
- Streams, but over the network
Slide 108
Slide 108 text
@another_clue
Sockets
108
- Streams, but over the network
- servers listen
- clients connect
Slide 109
Slide 109 text
@another_clue
Sockets
109
- Streams, but over the network
- servers listen
- clients connect
- Focus on TCP/IP (UDP etc. also possible)
Slide 110
Slide 110 text
@another_clue
Sockets
110
- Streams, but over the network
- servers listen
- clients connect
- Focus on TCP/IP (UDP etc. also possible)
Slide 111
Slide 111 text
@another_clue
Sockets
111
- Streams, but over the network
- servers listen
- clients connect
- Focus on TCP/IP (UDP etc. also possible)
Slide 112
Slide 112 text
@another_clue
Sockets
112
- Streams, but over the network
- servers listen
- clients connect
- Focus on TCP/IP (UDP etc. also possible)
Slide 113
Slide 113 text
@another_clue
Sockets in practice
113
Slide 114
Slide 114 text
@another_clue
Sockets in practice
- implementation detail
114
Slide 115
Slide 115 text
@another_clue
Sockets in practice
- implementation detail
- higher level protocols (HTTP anybody)
115
Slide 116
Slide 116 text
@another_clue
Sockets in practice
- implementation detail
- higher level protocols (HTTP anybody)
- higher level concepts (RESTful etc.)
116
Slide 117
Slide 117 text
@another_clue
Sockets in practice
- implementation detail
- higher level protocols (HTTP anybody)
- higher level concepts (RESTful etc.)
- higher level APIs (SDKs, API clients etc.)
117
Slide 118
Slide 118 text
@another_clue
Sockets in practice
- implementation detail
- higher level protocols (HTTP anybody)
- higher level concepts (RESTful etc.)
- higher level APIs (SDKs, API clients etc.)
- Concepts still apply, details are hidden
118
Slide 119
Slide 119 text
@another_clue
promises
119
Slide 120
Slide 120 text
@another_clue
Promises
- Placeholder for a single future result
- Possible states:
- pending
- fulfilled (successfully resolved)
- rejected (Exception occured)
120
Slide 121
Slide 121 text
@another_clue
Promise API
121
Slide 122
Slide 122 text
@another_clue
Promise API
122
Slide 123
Slide 123 text
@another_clue
Promise API usage
123
Slide 124
Slide 124 text
@another_clue
Promise API usage
124
Slide 125
Slide 125 text
@another_clue
Promise API usage
125
Slide 126
Slide 126 text
@another_clue
Promises in practice
-
126
Slide 127
Slide 127 text
@another_clue
Promises in practice
- Everywhere!
127
Slide 128
Slide 128 text
@another_clue
Promises in practice
- Everywhere!
- react/socket
- clue/buzz-react
- clue/packagist-react
- clue/redis-react
- react/mysql
- …
128
Slide 129
Slide 129 text
@another_clue
HTTP streaming
129
Slide 130
Slide 130 text
@another_clue
HTTP in a gist
130
Slide 131
Slide 131 text
@another_clue
HTTP in a gist
131
GET / HTTP/1.1
Slide 132
Slide 132 text
@another_clue
HTTP in a gist
132
GET / HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/plain
hello world!
Slide 133
Slide 133 text
@another_clue
HTTP in a gist
133
GET / HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/plain
hello world!
@another_clue
Server-Sent Events in a gist
142
- Normal Request/Response
- Client API for streaming access
Slide 143
Slide 143 text
@another_clue
Server-Sent Events in a gist
143
- Normal Request/Response
- Client API for streaming access
HTTP/1.1 200 OK
Content-Type: text/event-stream
Slide 144
Slide 144 text
@another_clue
Server-Sent Events in a gist
144
- Normal Request/Response
- Client API for streaming access
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: hello
Slide 145
Slide 145 text
@another_clue
Server-Sent Events in a gist
145
- Normal Request/Response
- Client API for streaming access
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: hello
data: world
Slide 146
Slide 146 text
@another_clue
Server-Sent Events in a gist
146
- Normal Request/Response
- Client API for streaming access
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: hello
data: world
Slide 147
Slide 147 text
@another_clue
fully-functional streaming
HTTP server in a single PHP
file
147
Slide 148
Slide 148 text
@another_clue
PubSub
148
Slide 149
Slide 149 text
@another_clue
PubSub
149
- Publish/Subscribe
Slide 150
Slide 150 text
@another_clue
PubSub
150
- Publish/Subscribe
- common pattern for distributed system
Slide 151
Slide 151 text
@another_clue
PubSub
151
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
Slide 152
Slide 152 text
@another_clue
PubSub
152
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
Publisher Redis Subscriber
Slide 153
Slide 153 text
@another_clue
PubSub
153
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
- horizontal scalability
- independent components
Publisher Redis Subscriber
Slide 154
Slide 154 text
@another_clue
PubSub
154
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
- horizontal scalability
- independent components
Publisher Redis Subscriber
Publisher Subscriber
Publisher Subscriber
Slide 155
Slide 155 text
@another_clue
PubSub
155
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
- horizontal scalability
- independent components
Publisher Redis Subscriber
Publisher Subscriber
Publisher Subscriber
Slide 156
Slide 156 text
@another_clue
PubSub
156
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
- horizontal scalability
- independent components
Publisher Redis Subscriber
Publisher Subscriber
Publisher Subscriber
Slide 157
Slide 157 text
@another_clue
PubSub
157
- Publish/Subscribe
- common pattern for distributed system
- message broker in between
- horizontal scalability
- independent components
Publisher Redis Subscriber
Publisher Subscriber
Publisher Subscriber
Slide 158
Slide 158 text
@another_clue
fully-functional, distributed
HTTP streaming server in a
single PHP file
158
Slide 159
Slide 159 text
@another_clue
many, MANY more third-party projects:
https://github.com/reactphp/react/wiki/Users
159
Slide 160
Slide 160 text
@another_clue
Conclusions
160
Slide 161
Slide 161 text
@another_clue 161
Can I use ReactPHP to make my website 1000x faster?
Slide 162
Slide 162 text
@another_clue
YES
162
Can I use ReactPHP to make my website 1000x faster?
Slide 163
Slide 163 text
@another_clue
YES
163
But you might be missing the point…
Can I use ReactPHP to make my website 1000x faster?
Slide 164
Slide 164 text
@another_clue
PHP
164
Slide 165
Slide 165 text
@another_clue
PHP
faster than you probably thought
165
Slide 166
Slide 166 text
@another_clue
PHP
faster than you probably thought
more versatile than you probably thought
166
Slide 167
Slide 167 text
@another_clue
ReactPHP
167
Slide 168
Slide 168 text
@another_clue
ReactPHP
168
a real deal and here to stay
Slide 169
Slide 169 text
@another_clue
ReactPHP
169
a real deal and here to stay
stable & production ready
Slide 170
Slide 170 text
@another_clue
ReactPHP
170
a real deal and here to stay
stable & production ready
*awesome*
Slide 171
Slide 171 text
@another_clue
try!
171
Slide 172
Slide 172 text
@another_clue
try!
whenever having to wait
172
Slide 173
Slide 173 text
@another_clue
try!
whenever having to wait
whenever accessing network
173
Slide 174
Slide 174 text
@another_clue
help!
174
Slide 175
Slide 175 text
@another_clue
help!
elaborate documentation on ReactPHP.org
175
Slide 176
Slide 176 text
@another_clue
help!
elaborate documentation on ReactPHP.org
tweet @ReactPHP or #reactphp
176
Slide 177
Slide 177 text
@another_clue
help!
elaborate documentation on ReactPHP.org
tweet @ReactPHP or #reactphp
Talk to me
177
Slide 178
Slide 178 text
@another_clue
help!
elaborate documentation on ReactPHP.org
tweet @ReactPHP or #reactphp
Talk to me
Did I mention I’m available? 178
@another_clue
integration
non-blocking and blocking don’t mix well
181
Slide 182
Slide 182 text
@another_clue
integration
non-blocking and blocking don’t mix well
decide for either approach
182
Slide 183
Slide 183 text
@another_clue
integration
non-blocking and blocking don’t mix well
decide for either approach
isolate & communicate
183
Slide 184
Slide 184 text
@another_clue
Integration with traditional environments
184
integrating async into sync is easy
Slide 185
Slide 185 text
@another_clue
Integration with traditional environments
185
integrating async into sync is easy
- just run the loop until you’re done
- see clue/block-react
Slide 186
Slide 186 text
@another_clue
Integration with traditional environments
186
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 187
Slide 187 text
@another_clue
Integration with traditional environments
187
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
Slide 188
Slide 188 text
@another_clue
Avoid blocking!
- The loop must not be blocked
188
Slide 189
Slide 189 text
@another_clue
Avoid blocking!
- The loop must not be blocked
- Many functions / lib assume blocking by default
- Anything >1ms should be reconsidered
189
Slide 190
Slide 190 text
@another_clue
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
190
Slide 191
Slide 191 text
@another_clue
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
191
Slide 192
Slide 192 text
@another_clue
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
192
Pay attention:
- PDO, mysql etc.
- file system access
- network access
- third-party APIs