Why I switched from Node.js to PHP

Why I switched from Node.js to PHP?

Today, I will share with you my story: how I started with PHP, switched to Node.js, developed many projects in Node.js, but after a few years switched back from Node.js to PHP.

I started my career as a PHP developer. Just like everyone, I was developing web applications in core PHP, no frameworks, no libraries, just pure PHP. I was very happy with it. Then I started learning frameworks. My first framework was Laravel. My favorite thing in Laravel was the route. I did not like the filename directly in the URL (although I can do that using .htaccess), so Laravel providing clean and customized routes was a big “YES” for me. Then, Node.js started to grow. Every YouTube channel, every blog post was talking about Node.js and why it is the new big thing. Every developer was choosing it and I thought I would be left behind if I did not follow it. So I started learning Node.js.

It was good in the beginning: I am now able to create real-time apps using sockets. I can run multiple functions at once. The response is faster because the response is not dependent on the operations to be completed. I can just return the response to the client app without waiting for the server to finish processing all the operations (sending email, saving in database etc.). But things started to deteriorate. I realized that the development of Node.js has many advantages. It is faster than PHP. My frontend and backend are in the same language now (Javascript). But it comes at a very high cost.

Node.js does not work on shared hosting

Node.js works only on VPS or dedicated servers, not shared servers. Because Node.js is an environment, it runs as a process that needs to be kept running to run the app. VPS and dedicated servers are relatively more expensive than shared ones. Following are the estimated costs of each of these hostings.

Shared hostingVPS hostingDedicated hosting
$4/month$35/month$142/month

Above pricings are taken from hostgator.com

Upon my research and experience, I found out that most of the applications developed in Node.js, don’t actually need Node.js. They are better off with a simple PHP web app, better with Laravel. But they don’t need the complexity of Node.js and Express. My best guess is, some tech enthusiasts drive the owners to develop the website in Node.js because it is the latest tech stack. Or because everyone is doing it, so we must do it. Of course, when your website gets many visitors per day, then you can upgrade your plan to VPS or dedicated, but even then, having the complete backend in Node.js is risky because of other factors I will mention later. So you can start with shared hosting and then upgrade your plan to VPS or dedicated when needed.

While PHP can be deployed on a shared hosting and can easily handle up to 100,000 visitors per month to your website, which is a very decent number. Even after that, you do not have to change your whole tech stack to Node.js, just upgrade your server to VPS and PHP can handle more.

Error in 1 API will terminate whole application

Yes, you have read it right. That was one of the solid reasons why I switched from Node.js to PHP. Since Node.js runs in a single process, if one of your APIs caused a fatal error (or “Internal server error”), then your whole application will be stopped. No other request will be fulfilled. Although there are workarounds for this: you can create multiple processes for error-prone tasks, or you can use the legendary try catch block. However, if you do this, you need to manage the error logging by yourself, since the errors going to the catch block need to be dealt with today or tomorrow. This complicates further, because imagine, now you have to write tests for each case and pass those tests with so many different scenarios. Even then, when the app goes to the real users, some test scenarios will be missed.

I faced this problem when I was developing a social commerce website, sanyexpress.com. In every application, you have to develop a lot of APIs. If an error in 1 API results in terminating the whole application, that is a big problem for your business. On the other hand, if you look at PHP, the error in 1 API does not have any effect on the rest of the APIs. All other functions of your website will keep working fine. Not only that, if you are building in Laravel, it has a built-in logging mechanism that saves the error logs in storage/logs/laravel.log.

PHP is easy to deploy

This is one we can all agree on. When I switched from Node.js to PHP, I realized how easy it is to deploy a PHP application as compared to others. Just drag and drop all your files to your “File Manager” in your cPanel and Voilà! Your website is live. Need a database? No problem, just create one using the “MySQL Databases” page and enter the credentials in your PHP file. Not only is deploying PHP easy, but updating the code is easier as well. You want to make changes to a file? Fine, just open the file, do your changes and save the file, and it is updated on the live website.

While in Node.js, you have to use command line tools or Docker (adds complexity) to make your website live. Even after that, if you need to make even a small change to your website, you have to create a build and deploy it again on the server.

Imagine a scenario: An unexpected bug is on the live website. When you test it on localhost, it worked fine because you have little data. But it crashes on the live server because of some issue with the data on the live server. You cannot directly connect your local host to live data because testing on live data would be more risky. So now you are doing a hit-and-trial, trying different codes hoping to see which one works. No doubt, these types of scenarios can happen on PHP websites as well. But you will not be creating a build and deploying it to a server every time to test it. You will open the affected file once, and then edit and save the changes until it is fixed. This will save you a lot of time, which will be crucial, especially when the bug is on the live server.

Memory leaks

As discussed earlier, Node.js runs on a single process and the process stays alive. A single global variable that is taking the space in memory (whether it is in use or not) can cause very big problems. Node.js has persistent memory, which means that the variables you create in it will be shared with every request it receives. While PHP is stateless, every request is independent of others. Every request starts as a new fresh request to the server. That is why, in authentication, PHP developers typically use sessions. Because once the server fulfills the request, it forgets the client and when a new request arrives at the server, it treats it as a new request.

When to use Node.js

My advice for you is to use Node.js only for real-time communication like chats etc. Node.js has very good support for sockets that are used for real-time communication. So develop your backend in PHP, preferably in Laravel, and use Node.js only if you want to add a realtime chat feature to your website or want to send real-time notifications to the users. All other functionality of your application, authentication, billing, admin panel, should be in PHP. I ended up re-developing a whole application from Node.js to PHP just because PHP is just too simple. I can just grab my files, upload them to a server and my website is live. Updating the files is like updating files on my local computer.

For frontend, I would recommend React JS. You will face some SEO-related issues with this, but you can overcome that using social media marketing, email marketing or backlinks from other blogs. I developed a job portal platform in React JS and Laravel. I have been a Node.js developer for 2 years, and have developed many projects on it, but the above-mentioned reasons were why I switched from Node.js to PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *