Chris Boden and Igor Wiedler joined forces to bring the PHP community a precious gift. And though others have stepped in to help, these gifted developers were instrumental in the evolution of PHP.

Perhaps you think I’m being dramatic. Well zip it! These folks are amazing and ReactPHP is amazing. You just haven’t spent enough time with it to come to that inevitable conclusion.

ReactPHP is a collection of libraries, but those that seem to get most of the spotlight are the event loop and socket/HTTP servers that go with it. Already heard of ReactPHP? It’s probably because of code like this:

$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(["Content-Type" => "text/plain"]);

$response->end("Hello World");

});



$socket->listen(1337);



$loop->run();

It looks a lot like the NodeJS reference HTTP server implementation, doesn’t it? There’s a bit more to do with the event loop, but otherwise it’s almost the same. It’s based on similar operating principles, though it has to achieve them using many different tools.

It’s also a little clearer that the event loop is involved. The socket server plugins into it so that incoming socket connections will find their way to code that interprets them. The HTTP server decorates this behaviour, adding HTTP message parsing to the equation.

Finally, we can add our own listeners to the HTTP server, so as to be able to interact with the incoming request and outgoing response. It’s fascinating seeing this within the bounds of PHP. We’ve conditioned ourselves to ignore the socket/HTTP message negotiation aspects of PHP requests.

We’ve learned to give up that control, and getting it back is equal parts interesting and scary. We need to do more to get back to the state we were in, with Apache, but we also get far more control over the request/response cycle.

A Wild Problem Appears!

It may seem like there’s a dependable parity between the NodeJS and ReactPHP environments. This is partly true. We can read and write to the same kinds of files. We can open and communicate through the same kinds of sockets. We can do the same kinds of DNS resolution.

The next bit might sound like I dislike the “old box of tools”. Like many aspects of life, it’s possible to love something while (at exactly the same time) being critical of its bad parts. I love PHP; the community and the language. That’s why I want to push the boundaries of the language and mental models we work with every day.

The trouble is that ReactPHP is working with an old box of tools. PHP was born in a time before JavaScript was a thing. Remember that JavaScript grew out of the competition between browser vendors. Different folks were trying to win the hearts of developers by offering the next best kind of interaction and animation. These new features were rooted in the event loop model, and every bit as asynchronous as the first unobtrusive DOM events.

Don’t get me wrong: JavaScript can be every bit as synchronous as the traditional PHP code we’ve been writing for decades. It’s just that in a web browser, synchronous execution feels sluggish. Battling for the affections of users has pushed JavaScript into an impossible race to be the most responsive, the best looking, the most useful.

Synchronous code is a hurdle that JavaScript developers have long since learned to jump. That’s how young JavaScript developers are taught. It’s a useful mental model to have, and so far from what young PHP developers are taught that it’s often difficult to change hats.