Our team is experienced both in PHP programming and Node.js development. We have a portfolio with both PHP-based applications and Node.js-based applications as well as mixed ones. What do we take into account when considering which tool to use in custom software development?
PHP 7 vs Node.JS: Terminology differences
- PHP is a programming language. Node.js is not a programming language, it is rather an interpreter of Javascript code (due to the Google Chrome V8 engine) to the operating system installed on the server. But Node.js is more than just that because it is extended with the libuv library that focuses on asynchronous I/O.
- To execute JavaScript code on the server, one needs a Node.js installer.
- PHP cannot work without a web server. Node.js has built-in tools for creating servers, hence it can work without the help of external servers. However, as a rule, Nginx server is used as a reverse proxy before Node.js server on a production hardware server.
PHP was created in 1994, Apache – in 1995, Javascript – in 1995, Nginx – in 2002, and Node.js – in 2009. Let’s take a look at the history of creating Node.js and reasons for its development.
PHP 7 vs Node.JS: Behind Node.JS creation
As everybody can see from the first presentation of Node.js in 2009, Ryan Dahl, the creator of Node.js, criticized:
- The limited possibilities of the most popular web server in 2009, Apache HTTP Server to handle a lot of concurrent connections (up to 10,000 or more);
- The most common way of creating code (sequential programming), when code either blocked the entire process or implied multiple execution stacks (read as “more hardware and software”, more money) in the case of simultaneous connections.
As Ryan Dahl noted, Node.js and Javascript were able to solve this problem at that time at no extra cost:
- Node.js provides a purely evented, non-blocking infrastructure to script highly concurrent programs.
- Javascript is designed specifically to be used with an event loop: anonymous functions, closures; only one callback at a time; I/O through DOM event callbacks. The culture of JavaScript is already geared towards evented programming.
Ryan Dahl‘s presentation was regaled with ovations. One of the most common web applications' performance problems was resolved! No additional hardware! No additional monthly hosting costs! The only strict requirement was retraining programmers to teach them using event loops, callbacks, and non-blocking I/O.
Some large companies expressed interest in the implementation of Node.js into their live project. As was reported, the transition from Rails to Node.js improved the scalability of LinkedIn’s mobile backend infrastructure. There’s been a lot of talk about PayPal moving from Java to Node.js for an application platform.
‘Node.js is not the best server side system ever’
admitted Ryan Dahl in his interview (2017)
PHP 7 vs Node.JS Perfomance
In 2015, PHP 7 with new long-expected features was introduced.
PHP 7 is based on the PHPNG engine that speeds up PHP applications more than the previous PHP interpreter (Zend Engine 2.0). Thanks to PHPNG, your apps see up to 2x faster performance and 50% better memory consumption than PHP 5.6
With PHP 7, possible differences in performance are already not an issue when one compares Node.js with PHP.
But even earlier, in 2013, Phil Sturgeon made a test and concluded:
‘PHP is not sucking as bad as some people would expect. Removing the blocking PHP approach (because obviously, it's slow) and running just the other three scripts [PHP 5.5.5 + ReactPHP + phpQuery] looks like this [take a look on the image below]. Look, they're the same. At this point, it is just a network test. The speed between the two systems for handling this specific task is essentially identical.’
Need Node.js or PHP developers? Hire a dedicated team!
PHP 7 VS NODE.JS: PHP is alive and has learnt its lessons from Node.JS to be stronger than before
A lot has changed since 2009. The WebSocket protocol was standardized in 2011 and NGINX server started to support it. PHP programmers have learned to use event loops, callbacks, and non-blocking I/O with ReactPHP.
So, today’s experienced developers can choose what tool to use for any particular project. What do they take into account when considering what tool to use?
Andrey M., a software developer at Belitsoft Company (10 years of experience with PHP, 1 year – with Node.js):
For some tasks, Node.js may be faster than the “standard” webserver with PHP because it runs as a single thread with non-blocking IO for each connection, hence there is no memory overrun. Due to this, Node.js is useful when there is a need to process data in real-time (chats, games, video, big data streams without logic). However, Node.js can also be used for creating CMSs, e-commerce, and other web applications, which are usually developed using PHP. Node.js is also able to perform high-load tasks, but these tasks should be moved away from the main thread using appropriate tools such as callbacks, workers etc.
On the other hand, tasks for which Node.js is an excellent choice can be performed using PHP. For example, ReactPHP enables developers to write PHP-based socket server to process requests constantly as well as Node.js does it (yes, Node.js is faster, but the fact is that PHP can also do it). The same thing with Workers (the classes responsible for running asynchronous jobs and synchronizing their results) or Amp (libraries that are used for writing non-blocking asynchronous code). Hence, it is easy to create long running processes using PHP. Additionally, there are a lot of tools for supporting and managing these processes (such as supervisord).
Finally, Node.js can be used as complement to the PHP (or to another programming language) with the aim of reducing load on a server and performing program (or, as one can say, PHP can be used as complement to Node.js :))) For example, the first version of a website was built in PHP and then the client asked to develop a real-time system that gets data from stock data systems. I would prefer to develop that real-time system as a separate application using Node.js (and then integrate it into the PHP-based application).
So, the same tasks may be performed either with PHP or with Node.js. The question "what tool to use" is a question of personal preferences. I use Node.js for tasks involving big data flows and PHP for tasks involving complex logic, high-load tasks, for dealing with external utilities, applications, and OS. PHP has a lot of different libraries, frameworks, and it is more standardized than Node.js (however, Node.js is not far behind and continues to gain momentum). In general, I think it is a good idea not to counterpose them, but use them cooperatively.
Sergey K., a software developer at Belitsoft Company, participant and award winner of various hackathons (8 years of experience with PHP, 1 year – with Node.js):
Comparatively low entry barrier for writing PHP code means there are many PHP specialists on the market, hence, it is pretty easy to find developers for PHP-based projects. Due to age factor, PHP has more production-ready modules, frameworks, and CMSs. It means if a client needs a product and there are ready solutions in PHP, then it is better to use PHP because it will be cost effective.
I think the entry barrier for writing Node.js code is higher. Node.js uses JavaScript, but it does not mean that any front-end developer can easily start writing back-end using JavaScript. And it does not mean that any front-end developer can use the same code snippets both on the front-end and back-end.
PHP and Node.js are tools that can very often be used interchangeably for the same tasks. However, Node.js is often used for real-time applications, web sockets and workers (especially, persistent workers), and rarely for high-load tasks and computation. From the scalability perspective, there are no big differences between PHP and Node.js, it is more important to consider the project’s architecture.
It is always interesting to hear what other experienced developers think. Here is what we have gathered from the web.
PHP 7 vs Node.JS: Some opinions from the Web
Jonathan: We are planning to run a large-scale E-commerce marketplace (eventually a pool of websites, one for each country) and we are trying to decide on whether to use PHP + Apache Stack or go with Node.js. We plan to get 5000 peak simultaneous connections for our "product search" function. What would you suggest?
Thomas Hunter (a Senior Node.js Engineer): I'd lean towards PHP as you're probably not going to benefit from any real-time communication, shared data between different users, etc. Also, if Node crashes, it could cause a lot of trouble, especially since most of what you are doing is transactional, but if one PHP execution fails, it's much less dangerous. There's also a lot of PHP eCommerce applications and libraries out there already that you could make use of.
Azat Mardan: There are many better alternatives to PHP, e.g., Ruby on Rails and Django, however, nothing is as approachable as Node.js. I worked with many technologies including Ruby on Rails, Python, Java/J2EE, VB, ASP, Perl and of course PHP. However, my focus during the past couple of years has been dedicated solely to Node.js and front-end JavaScript frameworks like Backbone.js. So my opinion might be biased.
Juan (a developer with experience on Node.js, PHP, and Ruby): I developed an amazing and faster webRTC solution using Node.js and an astounding video portal using PHP that receives 1, 5 million visits every day; however I don’t think that I can achieve these projects’ goals by interchanging those technologies.
Laurens: I still doubt whether to use Node.js or native PHP sockets. Wouldn’t it make more sense to use default PHP instead of Node.js? As far as I know, Node.js is limited and barely has support for a good MYSQL connection, isn’t PHP more optimized for this? I know that Apache is slower than Node.js, but for what kind of application is it better to use Node.js? I do not have any idea of Node.js advantages over PHP Socket, perhaps the speed, but I will be using Nginx.
Gonzalo Ayuso (an experienced web architect and web developer): WebSockets with Rachet (PHP) works fine. I started to play with socket.io (node.js) some time ago and now I’m very comfortable with it. I haven’t done any in-depth benchmark to choose one or another, indeed. Socket.io for example, has a very fail-over system to work with old browsers (it works even with IE6), but Rachet works fine with newer ones. As you said, to perform traditional SQL operations is a bit of a nightmare using node. This is mainly because of the functional nature of js (OK js is not a fully functional programming, but it’s very close). I don’t have this problem because I always rely on backend server (PHP for me) and I only use node.js for the strictly real-time operations. My node.js servers for WebSockets are just a proxy and all the logic is bypass to the backend. Anyway, if you choose PHP or node.js for WebSockets you must take into account that you will need to handle two different servers. That’s means share authorization layer and things like that.
Since WordPress.com has moved to Node.JS, do you think Laravel will also look towards that option even though PHP 7 is here?
Dayle Rees (a Laravel Framework contributor and developer): For a long time PHP was the butt of many language jokes, but I honestly feel that it’s becoming not only a popular language but a powerful one. PHP7 is great. The speed boost is one thing, but having optional support for full type hinting is a game changer. We’ve also got modern tools like Laravel and Composer, breathing new life into the language and its supporting community. With this in mind, I think it’s unlikely that Laravel will move from PHP. I think it’s more likely to gain further integration with front-end tools to provide a complete application building platform. That’s where I see it heading in terms of future expansion. I’m sure Node will continue to excel when dealing with microservices and threaded applications.
Contact us and get a free quote on your PHP or Node.js project!
Rate this article
Belitsoft has been the driving force behind several of our software development projects within the last few years. This company demonstrates high professionalism in their work approach. They have continuously proved to be ready to go the extra mile. We are very happy with Belitsoft, and in a position to strongly recommend them for software development and support as a most reliable and fully transparent partner focused on long term business relationships.
Global Head of Commercial Development L&D at Technicolor