A First Look at React

SitePoint Sponsors

For the past couple of years, Node.js has been drawing increasing amounts of attention as a promising web technology. While it has some strengths, like being event driven, some people just want to stick to PHP. For over a year now, however, there has been a similar project for PHP named React.

React is mostly coded by Igor Wiedler, who is also a prominent contributor to the Silex framework. While reading through the React examples, it really does look similar to Node.js.

Like Node.js, you’re encouraged to write your applications to execute asynchronously. For those who may not be familiar with asynchronous programming, the server answers requests with a single thread but uses worker threads for time-consuming tasks like writing to a file, reading from a database, retrieving results from the Twitter API, etc. When the task is finished, the worker returns the result to the main thread which finalizes the request. This style of programming can give an enormous speed boost.

Getting React

React is available with Composer through Packagist (if you’re not familiar with working with Composer, check out my previous article). To install React, create a new composer.json file with the following content:

"require": {
"react/react": "0.2.*"

Open a console, navigate to where the file is, and run php composer install. React and its dependencies will be retrieved and can then be used by including the vendor/autoload.php file.

Creating a Simple Server

With Node.js you create a server script yourself instead of using a server application like Apache or Nginx. You set up an event function, define a port which your server will listen to, and then wait for requests. With each request the event function is executed. This is the same in React.

This example is taken from the the React project’s README.md file:

require_once 'vendor/autoload.php';

$i = 0;
$app = function ($request, $response) use (&$i) {
$text = "This is request number $i.n";
$headers = array('Content-Type' => 'text/plain');

$response->writeHead(200, $headers);

$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);

$http->on('request', $app);


First it includes the Composer autoloader file, and then sets the variable $i to zero which will be used to count the requests. Then, something that is not an overly common practice in the PHP community, our event function which will handle all incoming requests is stored to a variable. The function sets the response’s headers and prints the number of handled requests.

The script creates an event loop that handles unblocked I/O for us, a socket, and then the server. Finally, the event function is assigned, the socket is set to listen on port 1337, and the event loop is run.

If you save the code as server.php and run it from the command line with php server.php, and then navigate your browser to http://localhost:1337, you’ll see the response. Don’t forget to check your command line to see how many requests have been handled!

Exploring React Further

React can do much more than act as an HTTP server. We can use it for some much cooler things. Think of this example: you’re a spy for the CIA and you want to transfer data to your boss. The boss creates a server, you connect as a client via a socket, and everything you type is then logged in the server’s log.

Support for sockets is handled by the socket component in React, and for this example you don’t need the full React library. The following composer.json should be sufficient:

"require": {
"react/socket": "0.2.*"

And this is the example code:

require_once 'vendor/autoload.php';

$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);

$socket->on('connection', function ($conn) use ($loop) {
$conn->write("Successfully connected to the writing servern");
echo 'client connected';
$dataStream = new ReactStreamStream(fopen('data.txt', 'w'), $loop);

$conn->on('data', function($data) use ($conn, $dataStream) {

$conn->on('end', function() {
echo 'connection closed';


We create an event loop again and then create a socket. When the connection event is emitted, we print a message to the console. We create a new writing stream and name it $dataStream and when we receive data, we push it down the stream and the data will be written to our file. When the client disconnects, the stream is closed.

After running php server.php you can connect by using nc localhost 4000. Everything you type and press enter afterwards is logged to data.txt.

Who Would Use React?

So React offers some really awesome functionality, but why would you use a younger library that’s not yet stable instead the more mature Node.js, which also has a big community? I was thinking this myself, so I asked Igor Wiedler:

I wouldn’t really recommend running react in production at this point. Some of the lower level parts are quite stable, the higher level stuff not so much. The target audience right now is people who like to experiment with cutting-edge technologies and aren’t afraid to debug stuff when it breaks. I particular: I’m interested in having new libraries based on the react event loop, so that they can be used together.


Is React an interesting project? I think so, even if it just ports Node.js’ functionality, it does so in a cool way, and who would have thought that it was even possible to do such things in PHP? At least I didn’t.

React seems like a promising project, even while it’s not production-ready yet, it seems to have some cool features and a good developer to maintain it. Of course I can’t explain every aspect of React in this short article, so if you want to learn more about React:

Image via Fotolia