disclaimers!
!
›❯ async is not commonplace
›❯ async requires work
›❯ hatters gonna hat
Slide 3
Slide 3 text
the problem!
!
›❯ scaling php from the inside is hard
›❯ persistent connections halt execution
›❯ synchronous core and history
Slide 4
Slide 4 text
synchronous requests!
!
http request hits server
→ then you parse input
→ then you read the database
→ then you render the view
→ then you return the response
Slide 5
Slide 5 text
asynchronous requests!
!
http request hits server
→ then you ask for parsed input
!
you get parsed input
→ then you ask for a database read
!
...
Slide 6
Slide 6 text
fixing the problem!
!
›❯ does it need to be fixed?
›❯ hardware is cheaper than developers
Slide 7
Slide 7 text
queues
Slide 8
Slide 8 text
queue daemons!
!
›❯ beanstalkd
›❯ rabbit mq
Slide 9
Slide 9 text
queue services!
!
›❯ amazon sqs
›❯ iron mq
Slide 10
Slide 10 text
›❯ composer require "illuminate/queue:4.1.0"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing symfony/process (v2.4.4)
Loading from cache
gearman needs...!
!
›❯ a worker script
›❯ a manager daemon
›❯ a client script
Slide 17
Slide 17 text
how it works!
!
client connects to daemon
→ then client adds task to queue
→ then daemon sends task to worker
→ then worker processes task
→ then worker sends result to daemon
→ then daemon sends result to client
$worker = new GearmanWorker();
$worker->addServer();
!
$worker->addFunction("handle", function($job) {
// get the job data with $job->workload()
// mark the job as handled with $job->handle()
// return the result
});
!
while ($worker->work());
Slide 20
Slide 20 text
$client = new GearmanClient();
$client->addServer();
!
$client->setCompleteCallback(function($task) {
// get the result data with $task->data()
});
!
$task = $client->addTask("handle", "the task data");
$client->runTasks();
what react is...!
!
›❯ react is a replacement for plain old php + apache
›❯ react is like the php equivalent of node js
›❯ react is still beta
Slide 24
Slide 24 text
what react is not...!
!
›❯ react is not an external module
›❯ react is not a framework
›❯ react is not a magic bullet
Slide 25
Slide 25 text
›❯ composer require "react/react:0.4.0"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing react/promise (v2.0.0)
Loading from cache
Slide 26
Slide 26 text
require("vendor/autoload.php");
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);
switch($route[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// ... 404 Not Found
break;
!
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $route[1];
// ... 405 Method Not Allowed
break;
›❯ curl -X GET "http://127.0.0.1:8080/products"
showing
!
›❯ curl -X POST "http://127.0.0.1:8080/products"
saving
Slide 39
Slide 39 text
sockets!
!
›❯ download ratchet
›❯ implement MessageComponentInterface
›❯ pass your subclass to Http\Server
Slide 40
Slide 40 text
›❯ composer require "cboden/ratchet:0.3.0"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing symfony/routing (v2.4.4)
Loading from cache
!
- Installing symfony/http-foundation (v2.4.4)
Loading from cache
Slide 41
Slide 41 text
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
!
class Chat
extends React\Socket\Server
implements MessageComponentInterface
{
}
Slide 42
Slide 42 text
require("vendor/autoload.php");
$loop = React\EventLoop\Factory::create();
// $socket = new React\Socket\Server($loop);
$socket = new Chat($loop);
$http = new React\Http\Server($socket, $loop);
Slide 43
Slide 43 text
var socket = new WebSocket("ws://127.0.0.1:8080");
!
socket.addEventListener("message", function(e) {
console.log(JSON.parse(e.data));
});
!
socket.send(JSON.stringify({"foo" : "bar"});
Slide 44
Slide 44 text
public function onOpen(ConnectionInterface $socket)
{
$this->socket->send("connected");
}
!
public function onMessage(ConnectionInterface $socket, $message)
{
$this->socket->send("message: {$message}");
}
something new!
!
›❯ learning new language paradigms is great
›❯ event-based code may make cleaner code
›❯ async is useful and is being used in popular frameworks