Slide 1

Slide 1 text

Reacting With

Slide 2

Slide 2 text

what do we need?

Slide 3

Slide 3 text

asynchronous

Slide 4

Slide 4 text

non-blocking

Slide 5

Slide 5 text

event driven

Slide 6

Slide 6 text

input / output

Slide 7

Slide 7 text

why do we need it?

Slide 8

Slide 8 text

queue munching real time communications file transfers timed events

Slide 9

Slide 9 text

nodejs

Slide 10

Slide 10 text

nodejs

Slide 11

Slide 11 text

enter reactphp http://reactphp.org

Slide 12

Slide 12 text

adding react to our project

Slide 13

Slide 13 text

{ "require": { "react/react": "0.3.*" } } > php composer.phar install

Slide 14

Slide 14 text

the event loop

Slide 15

Slide 15 text

$loop = React\EventLoop\Factory::create(); // determines which event system is available. // libevent, libev, or stream_select, gives you back // an object that implements the loop interface. $loop->run(); // run the loop. // ... except our app does nothing... so...

Slide 16

Slide 16 text

$loop = React\EventLoop\Factory::create(); // create a timer that executes every 1 second. $loop->addPeriodicTimer(1,function(){ echo microtime(true), “ LOL”, PHP_EOL; }); $loop->run();

Slide 17

Slide 17 text

> php bin\loop1.php 1376205167.2002 LOL 1376205168.2011 LOL 1376205169.2011 LOL 1376205170.202 LOL 1376205171.2021 LOL 1376205172.2022 LOL 1376205173.2031 LOL ^C

Slide 18

Slide 18 text

$loop->addTimer(int $time, callable $func); // run once after $time seconds. $loop->addPeriodicTimer(int $time, callable $func); // run every $time seconds. both return Timer objects with various methods, most useful of which may be the cancel() method. - see: src/React/EventLoop/Timer/Timer.php

Slide 19

Slide 19 text

the socket server

Slide 20

Slide 20 text

$loop = React\EventLoop\Factory::create(); $server = new React\Socket\Server($loop); // create an object that handles sockets. $server->listen(7890); // and have it listen on port 7890. $loop->run();

Slide 21

Slide 21 text

> telnet localhost 7890 Connected to localhost:7890...

Slide 22

Slide 22 text

event: on connection

Slide 23

Slide 23 text

$server = new React\Socket\Server($loop); $server->listen(7890); // bind a function to connection events so that the // socket server knows what to do about them. $server->on( ‘connection’, function($cx) { echo “a client has connected.”, PHP_EOL; });

Slide 24

Slide 24 text

event: on data receive

Slide 25

Slide 25 text

$server->on(‘connection’, function($cx) { // attach a function to the connection itself that // deals with handling any data it sends us. $cx->on(‘data’, function($input,$cx){ echo “data from client: {$input}”, PHP_EOL; }); });

Slide 26

Slide 26 text

tcp to the ip, dawg i heard some guy named tripcee say that once.

Slide 27

Slide 27 text

just because you received data, does not mean it is all your data. example: windows telnet sends every keypress as it happens. unix telnet waits until new line. messages could get broken up into multiple packets and arrive to the client in the wrong order. that is how the internet works. so as they arrive the NIC may choose to send you what it can construct properly *now* and wait until later to send the rest.

Slide 28

Slide 28 text

solution: when you receive data, add it to a buffer. check the buffer each time to see if it has a complete message yet or not. super simple example: LF based protocols (fgets()) new line based protocol? does your data have a new line in it? start of buffer to the new line is a complete message.

Slide 29

Slide 29 text

1 2 3 4 6 8 9 1 2 3 4 7

Slide 30

Slide 30 text

6 8 9 1 2 3 4 7 5 oh hai guiz 6 8 9 7 5

Slide 31

Slide 31 text

public function BufferAdd($input) { $this->Buffer .= $input; return; } public function BufferDrain() { while(($pos = strpos($this->Buffer,chr(10))) !== false) { $cmd = trim(substr($this->Buffer,0,$pos)); $this->Buffer = substr($this->Buffer,($pos+1)); if(!$cmd) continue; else return $cmd; } return null; }

Slide 32

Slide 32 text

sending data back

Slide 33

Slide 33 text

$cx->on(‘data’,function($input,$cx){ $cx->DataBuffer .= $input; while($msg = magic_line_reader($cx->DataBuffer)) { switch($msg) { case ‘ping’: $cx->write(“pong\n”); break; default: $cx->write(“error: unknown command\n”); break; } } });

Slide 34

Slide 34 text

extend the server class just do it.

Slide 35

Slide 35 text

listen(7890); $server->on(‘connection’,function($cx){ $cx->on(‘data’,function($input,$cx) { ... }); $cx->on(‘close’,function($input,$cx) { ... }); }); $loop->run();

Slide 36

Slide 36 text

class Server extends React\Socket\Server { protected $EventLoop; protevted $Port = 7890; public function __construct() { $this->EventLoop = React\EventLoop\Factory::create(); parent::__construct($this->EventLoop); $this->listen($this->Port); $this->on('connection',[$this,'OnConnect']); return; }

Slide 37

Slide 37 text

class Server extends React\Socket\Server { protected $EventLoop; public function Run() { return $this->EventLoop->run(); } public function OnConnect($cx) { // do stuffs } }

Slide 38

Slide 38 text

listen(7890); $server->on(‘connection’,function($cx){ $cx->on(‘data’,function($input,$cx) { ... }); $cx->on(‘close’,function($input,$cx) { ... }); }); $loop->run();

Slide 39

Slide 39 text

Run();

Slide 40

Slide 40 text

needs more pew pew let’s look at some fleshed out demos

Slide 41

Slide 41 text

so you want to websocket?

Slide 42

Slide 42 text

have socket server? write javascript front end? will travel?

Slide 43

Slide 43 text

nope.

Slide 44

Slide 44 text

websocket: concept of awesome

Slide 45

Slide 45 text

websocket: implementation stupid as @#$%

Slide 46

Slide 46 text

ratchet saves the day http://socketo.me

Slide 47

Slide 47 text

class Server implements MessageComponentInterface { public function onOpen() { } public function onClose() { } public function onError() { } public function onMessage() { } } Ratchet\Server\IoServer::factory( new Ratchet\WebSocket\WsServer(new Server), 7890 )->run();

Slide 48

Slide 48 text

catch404.net/firefly the next world of warcraft killer mmorpg

Slide 49

Slide 49 text

so we can long herp our slow derps during all the things?

Slide 50

Slide 50 text

no. not directly. (we are still only a single thread)

Slide 51

Slide 51 text

asynchronous non- blocking event driven input / output

Slide 52

Slide 52 text

$server->on(‘connection’,function($cx){ sleep(9001); }); $cx->on(‘data’,function(){ ReportGeneratorThatTakesThirtyMinutes(); });

Slide 53

Slide 53 text

board members workers manager

Slide 54

Slide 54 text

WEBSITE: i need this report SERVER: ok, come back in 30 minutes. WEBSITE: ok thanks. SERVER: you there, you are not doing anything. generate this report. WORKER1: ok i’m off to work on it. bbl. // 30 min later WORKER1: all done. website can view it whenever. SERVER: ok thanks. i’ll email that bloke.

Slide 55

Slide 55 text

quickie about clients

Slide 56

Slide 56 text

class Server extends React\Socket\Server { public function __construct() { $this->EventLoop = React\EventLoop\Factory::create(); parent::__construct($this->EventLoop); $this->listen($this->Port); $this->on('connection',[$this,'OnConnect']); return; }

Slide 57

Slide 57 text

class Client { public function __construct($host,$port) { $this->EventLoop = React\EventLoop\Factory::create(); $fp = stream_socket_client("tcp://{$host}:{$port}"); if(!$fp) throw new \Exception(“unable to connect {$host}:{$port}”); stream_set_blocking($fp,false); $this->Socket = new React\Socket\Connection($fp,$this->EventLoop); $this->Socket->on('data',[$this,'OnDataIn']); $this->Socket->on('close',[$this,'OnDisconnect']); return; }

Slide 58

Slide 58 text

everything else about clients is basically the same as servers. servers need to know how to handle: connections, data inputs, disconnections. clients need to know how to handle: data inputs, disconnections. both need to be able to: send data to the other.

Slide 59

Slide 59 text

overview • EventLoops • React\Socket\Server • connection, error • React\Socket\Connection • data, close, error • Ratchet\WsServer • Class implementing MessageComponentInterface • open, close, error, message

Slide 60

Slide 60 text

the end Bob Majdak Jr • @bobmajdakjr • irc: freenode #dallasphp • http://reactphp.org • http://socketo.me