React

Event-driven, non-blocking I/O with PHP.

Version: v0.4.1

GitHub Twitter Vimeo

React in the industry







An example: Webserver

This simple web server written in React responds with "Hello World" for every request.

require 'vendor/autoload.php';

$app = function ($request, $response) {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end("Hello World\n");
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();

To install, just run this command:

% composer init --require=react/http:0.3.* -n
% composer install

To run the server, put the code into a file example.php and execute it with the php program:

% php example.php
Server running at http://127.0.0.1:1337