Scato Eggen

Scato Eggen
29 februari 2016

JavaScript is taking the world by storm, making technologies like PHP obsolete… This is of course not true.

In this third and last part of JavaScript, the Universe and Everything, I will take a look at Node and compare it to PHP.

Non-blocking I/O

The thing that sets out Node from other platforms is its use of non-blocking I/O. In PHP, if you read data from a file or perform a query on a database, the process is blocked until the I/O operation is finished. In Node, you pass a callback and execution resumes. Node has blocking versions of most I/O functions, but its use is discouraged.

Non-blocking I/O was popularized by Node, but has popped up all over the place. In PHP, you can let Guzzle send multiple requests concurrently using promises and asynchronous requests. ReactPHP brings even more non-blocking I/O to PHP. Basically, you can do pretty much anything with ReactPHP that you can do with Node, like creating a WebSocket server. The big difference is that Node has a vast community, while the ReactPHP community is rather small.

EDIT: simultaneuos HTTP requests in PHP are not new :)

Node as a web API server

Non-blocking I/O is especially useful for real-time messaging, like instant messaging and message queues. An important property of non-blocking I/O is that you get low latency, without the overhead of threads. Because of the low latency, Node is also very suitable for web API servers. In a microservice architecture, the overhead of one REST call can easily add up to a large delay.

PHP is excellent for buildings web API servers, so if you have more experience with PHP and latency is not your primary concern, then PHP is still the best choice.

Node as a web page server

There is a number of web application frameworks for Node. Express is probably the most well-known. If you compare Express to the frameworks for PHP, it best resembles Silex. It has support for routing, templates and serving static files. What it misses are dependency injection, configuration and a bundle structure. This is fine if you are building web APIs, but in my opinion, this makes Node less suitable to use as a web page server.

Which is why I was a bit surprised when I read about Netflix using Node for all client facing servers. The blog post did, however, say that their back-end still runs on Java. The switch to Node was part of a larger plan to migrate to microservices. This enabled them to experiment with changes to the user interface without the risk of compromising the rest of the system.

Node as a command line platform

The progress in the Node community with respect to command line tooling is overwhelming. YUI Compressor used to be the only choice for compressing and bundling front-end assets, but now we have Grunt, Gulp, Browserify, Webpack and JSPM. These tools are so much better than the asset managers and task runners that PHP has to offer.

Yeoman provides scaffolding for just about anything, ranging from HTML5 boilerplate to Symfony apps. For PHP, you can use composer to create a Symfony project. The SensioGeneratorBundle performs scaffolding on a lower level.

ESLint does linting, performs style checks and can be used with Webpack. PHP has a command line option that performs linting. Tools like PHPCS and PHPMD check your code for style errors and other smells. JavaScript doesn’t really have a way to check for complexity and coupling, like PHPMD.

Node as a command line HTTP server. PHP has a built-in web server.

Node as an alternative for PHP

At the moment, Node is not really an alternative for PHP. It is, however, a very valuable addition.

Node is very handy when you are working with messaging. You don’t have to choose one or the other, you can use both PHP and Node. For example, you could send a message from a PHP process to a Node server, which then reroutes this message to the appropriate clients over WebSockets.

The same goes for command line tools. You can take PHP tools like PHPCS and run them using Grunt.

The bottom line is: even though PHP is far from obsolete, Node still has a lot to offer.