Event-driven Programming

SM
by Steve M11 views·Updated Jun 30, 2014
Powered by AI

Need to present on this topic?

Create your own polished presentation in minutes. Just describe what you need.

Free to try, no credit card required

Transcript

Event-driven

Programming

(and Websockets!)

in PHP

Event-driven Sockets

  • Most of ReactPHP depends on an event-driven socket library
  • Callbacks occur whenever data is received on the socket
  • When the callback is complete, control returns to the event loop
  • This is the architecture used by highly scalable web servers like Nginx

About Me

ReactPHP Libraries

Websockets

Simple HTTP Web Server

Simple ZeroMQ Server

  • What are websockets?
  • How are they implemented?
  • What browsers support websockets?
  • Predis/Async – Redis client
  • DNode-PHP – Node.js-compatible RPC bindings
  • Ratchet – Websockets and other servers
  • React/ZMQ – ZeroMQ messaging protocol
  • React/Stomp – Stomp messaging protocol
  • React/Promise – CommonJS Promises/A for PHP
  • React/Partial – Pre-fill function arguments

$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);

$http = new React\Http\Server($socket);

$http->on('request', function ($request, $response) {

$response->writeHead(200, [ 'Content-Type' => 'text/plain' ]);

$response->end("Hello World!\n");

});

$socket->listen(8080);

$loop->run();

$loop = React\EventLoop\Factory::create();

$context = new React\ZMQ\Context($loop);

$pull = $context->getSocket(ZMQ::SOCKET_PULL);

$pull->bind('tcp://127.0.0.1:5555');

$pull->on('message', function ($msg) {

printf("RECV: %s\n", $msg);

});

$loop->run();

Websockets - JavaScript

Websockets - PHP

$app = new Ratchet\App("chat.local", 8080);

$app->route("/chat", new Demo\Chat());

$app->run();

var conn = new WebSocket('ws://chat.local:8080/chat');

conn.onmessage = function(e) {

$(“#output”).prepend($(“<div />”).html(e.data);

}

function send_message(msg) {

conn.send(msg);

}

Demo\Chat needs to implement Ratchet's MessageComponentInterface

  • onOpen
  • onClose
  • onError
  • onMessage
  • 15 years experience in web development and administration (systems, databases, networks)
  • Various jobs: Web Developer, Chief Architect, Manager of IT, Director of Technical Operations, Database Consultant
  • Mostly worked with MySQL, as a developer, DBA, and administrator
  • Member of the Utah Open Source Foundation (UTOS) core team
  • Help organize the OpenWest Conference
  • Organizer of the Ski PHP Conference

Partial Functions

Promises API

  • Implements the CommonJS Promises/A standard
  • Allows you to defer giving an answer while allowing other processing to continue
  • A “Promise” is a value returned as a placeholder for the final result
  • To use a promise, pass a function to the then() method of the Promise object

Demo Code

  • Pre-fill function arguments, return new function
  • Use Partial\...() or Partial\placeholder() to skip arguments

use React\Partial;

$square = Partial\bind(“pow”, Partial\…(), 2);

$four = $square(2);

$nine = $square(3);

$sixteen = $square(4);

Promises API - Example

$wisdom = new Wisdom\Wisdom($whois_client);

foreach ($domains as $domain) {

$wisdom->check($domain)->then(

function ($avail) use ($domain) {

$txt = $avail ? “unused” : “taken”;

echo “Domain $domain is $txt.\n”;

}

);

}

$loop->run();

https://github.com/stevecoug/reactphp-whois

https://github.com/stevecoug/reactphp-demo

composer.json

bin/chat.php

app/Demo/Http.php

onOpen

app/Demo/Chat.php

History of ReactPHP

require_once __DIR__."/../vendor/autoload.php";

$ip = (isset($argv[1])) ? $argv[1] : "127.0.0.1";

$hostname = (isset($argv[2])) ? $argv[2] : "localhost";

$loop = React\EventLoop\Factory::create();

$http_socket = new React\Socket\Server($loop);

$http = new React\Http\Server($http_socket);

$http->on('request', [ new Demo\Http(), "onRequest" ]);

$http_socket->listen(8000, $ip);

$app = new Ratchet\App($hostname, 8080, $ip, $loop);

$app->route("/chat", new Demo\Chat());

$app->run();

{

"autoload": {

"psr-0": {

"Demo": "app"

}

},

"require": {

"php": ">5.4",

"cboden/Ratchet": "0.3.*",

"react/zmq": "0.2.*",

"react/partial": "~2.0",

"react/http": "*"

}

}

namespace Demo;

define("HTDOCS", realpath(__DIR__."/../../htdocs"));

class Http {

public function onRequest($request, $response) {

$path = $request->getPath();

$file = realpath(HTDOCS.$path);

if ($path === "/") return $this->redirect($response, "/chat.html");

if (substr($file, 0, strlen(HTDOCS)) !== HTDOCS || !is_file($file)) return $this->error404($response);

$ext = pathinfo($file, PATHINFO_EXTENSION);

switch ($ext) {

case "js": $type = "application/javascript"; break;

case "html": $type = "text/html"; break;

default: $type = "text/plain"; break;

}

echo "HTTP FILE: $path ($type)\n";

$response->writeHead(200, [ 'Content-Type' => $type ]);

$response->end(file_get_contents(HTDOCS.$path));

}

}

namespace Demo;

use Ratchet\MessageComponentInterface;

use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {

protected $clients;

protected $next_conn_id = 1;

public function __construct() {

$this->clients = new \SplObjectStorage;

}

public function onOpen(ConnectionInterface $conn) {

$conn_id = $this->next_conn_id++;

$name = "Connection #$conn_id";

$auth = false;

$color = "000000";

$info = [

"id" => $conn_id,

"conn" => $conn,

"auth" => $auth,

"name" => $name,

"color" => $color,

];

$this->clients->attach($conn, $info);

if ($name = $conn->WebSocket->request->getCookie("demo_chat_name")) {

$color = $conn->WebSocket->request->getCookie("demo_chat_color");

try {

$this->setupAuth($conn, $name, $color, $info);

} catch (\Exception $e) {

echo "WS Invalid cookie auth received ($conn_id)\n";

}

}

}

SplObjectStorage

Standard PHP Library

Store objects, and

associated data

onClose, onError

onMessage

setupAuth

recvMessage

validateName, validateColor

private function setupAuth($conn, $name, $color, $info) {

if ($info === null) $info = $this->clients->offsetGet($conn);

$this->validateName($name);

$this->validateColor($color);

$conn->send(json_encode([

"type" => "auth",

"valid" => true,

"name" => $name,

"color" => $color,

]));

$info['auth'] = true;

$info['name'] = $name;

$info['color'] = $color;

$this->clients->offsetSet($conn, $info);

}

public function onClose(ConnectionInterface $conn) {

$info = $this->clients->offsetGet($conn);

$this->clients->detach($conn);

printf("WS DISCONNECTED: %s\n", $info['name']);

}

public function onError(ConnectionInterface $conn, \Exception $e) {

$conn->close();

}

private function recvMessage($from_conn, $data, $info) {

if ($info['auth'] === true) {

foreach ($this->clients as $client) {

if ($client !== $from_conn) {

$client->send(json_encode([

"type" => "chat",

"from" => $info['name'],

"color" => $info['color'],

"message" => $data->message,

]));

}

}

}

}

private function validateName($name) {

if (strlen($name) < 3) {

throw new \Exception("Display name ($name) must be at least 3 characters long");

}

if (strlen($name) > 20) {

throw new \Exception("Display name ($name) cannot exceed 20 characters long");

}

if (!preg_match('/^[A-Za-z]([A-Za-z0-9]+[ ._-])*[A-Za-z0-9]+$/', $name)) {

throw new \Exception("Display name ($name) contains illegal characters");

}

}

private function validateColor($color) {

if (!preg_match('/^[0-9a-fA-F]{6}$/', $color)) {

throw new \Exception("Invalid color selected: $color");

}

}

public function onMessage(ConnectionInterface $from_conn, $payload) {

try {

$data = json_decode($payload);

$info = $this->clients->offsetGet($from_conn);

switch ($data->type) {

case "chat":

$this->recvMessage($from_conn, $data, $info);

break;

case "auth":

$this->setupAuth($from_conn, $data->name, substr($data->color, 1), $info);

break;

default:

print_r($data);

$this->sendError($from_conn, "Unknown message type received: $data->type");

break;

}

} catch (\Exception $e) {

$this->sendError($from_conn, $e->getMessage());

}

}

  • Ratchet began as a socket abstraction in 2011 by Chris Boden
  • In April 2012, worked with Igor Wiedler to create an evented API for sockets
  • Core contributors also include Daniele Alessandri, Jan Sorgalla, Christian Lück, and Jeremy Mikola
  • The library uses the Reactor pattern, which is where the name came from

Thank you for coming!

@stevecoug

http://github.com/stevecoug

http://joind.in/10872