concerto/http-workers

Library for managing a pool of HTTP servers.

Installs: 19

Dependents: 0

Suggesters: 0

Stars: 1

Watchers: 1

Forks: 0

Type:concerto-component

v0.2 2014-05-02 11:24 UTC

README

Library for managing a pool of HTTP servers.

Build Status

Install

The recommended way to install HTTP Workers is through composer.

{
    "require": {
        "concerto/http-workers": "0.*"
    }
}

Usage

Server

use Concerto\HttpWorkers\Pool;
use React\EventLoop\Factory as EventLoopFactory;

$loop = EventLoopFactory::create();
$pool = new Pool($loop);

// Report on the status of our worker clients:
$pool->on('ready', function($worker) {
    echo "Server ready on port {$worker->getPort()}.\n";
});
$pool->on('crash', function($worker, $error) {
    echo "Server {$worker->getPort()} exited unexpectedly.\n";
});
$pool->on('update', function($worker) {
    echo "Restarting server {$worker->getPort()} for code changes.\n";
});
$pool->on('log', function($worker, $text) {
    echo '>', $text;
});

// Listen for worker processes:
$pool->listen();

// Start a worker on port 8080:
$pool->createProcess(__DIR__ . '/app.php', '127.0.0.1:8080')->open();

// Start a worker on the next port, 8081:
$pool->createProcess(__DIR__ . '/app.php', '127.0.0.1:8081')->open();

$loop->run();

Client (app.php)

use Concerto\HttpWorkers\Process;
use React\EventLoop\Factory as EventLoopFactory;

// Begin the HTTP server:
$loop = EventLoopFactory::create();
$worker = new Process($loop);

// Prepare request handler:
$worker->on('request', function($request, $response) use ($worker) {
    $worker->writeToLog("This will appear in the server log when a request is made.\n");

    $response->writeHead(200, ['Content-Type' => 'text/html; charset=UTF-8']);
    $response->end('Hello World!');
});

// Start handling requests:
$worker->listen();
$loop->run();