DEV Community

Aleksey Razbakov
Aleksey Razbakov

Posted on

Node vs PHP

Is anyone here prefers Node over PHP for Backend?

Oldest comments (71)

Collapse
 
fyapy profile image
aabdullin

Websockets my love🙂

Collapse
 
ismail9k profile image
Abdelrahman Ismail

Is PHP considered as a programming language, compared to Node.js?

Collapse
 
vlasales profile image
Vlastimil Pospichal

Yes, PHP is considered as a programming language. NodeJS too.

Collapse
 
thekingofwit profile image
Brian Barrett

As far as I understand, Node isn't a programming language, JavaScript is. Node is a runtime environment that lets you execute JavaScript outside of the browser.

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

Exactly. A fair comparison would be node vs roadrunner for php. Even fpm isn't really a proper comparison since it only connects prerouted requests for php scripts with the php interpreter.

The mess is that there are technically 2 php interpreters, php cli and the one tied to fpm, both configurable separately.

Collapse
 
redeemefy profile image
redeemefy • Edited
  • This is for learning purpose or for a real project?
  • This is for a team or solo developer?
Collapse
 
razbakov profile image
Aleksey Razbakov

It is more about your personal experience and feeling.

This is for learning purpose or for a real project?

Both. I am a PHP developer and I decided to start a Quasar + Firebase project for learning purposes. It is already being a year me working on this project and now I deep stuck with decision how to proceed. I need now to add job queues and proper logging. Probably it is also better to have own database aside.

This is for a team or solo developer?

For now solo, but it should be scalable.

Collapse
 
redeemefy profile image
redeemefy

If you know how to do this on PHP, I will go Node just for the sake of learning.

Collapse
 
echorris profile image
Eric • Edited

Dockerize your environments and use any of the many queueing systems to manage your processes. E.g. RabbitMQ. I'd stick with php, it has its quirks but I love it over all other languages personally.

Collapse
 
vlasales profile image
Vlastimil Pospichal

I prefer PHP.

Collapse
 
razbakov profile image
Aleksey Razbakov

Symfony or Laravel?

Collapse
 
vlasales profile image
Vlastimil Pospichal • Edited

Vanilla MVC.

Thread Thread
 
razbakov profile image
Aleksey Razbakov

No frameworks?

Thread Thread
 
vlasales profile image
Vlastimil Pospichal

No frameworks - some thin boilerplates only. PDO for databases, XSLT for templates. Model 55 lines, router 10 lines. Without any HTML inside.

Collapse
 
haikelfazzani profile image
Haikel Fazzani

Node.js is a JavaScript runtime and PHP is language !! PHP vs Javascript or you can compare Nodejs vs Zend Engine. be productive and avoid comparing languages .. all languages are good ...

Collapse
 
razbakov profile image
Aleksey Razbakov

My question is not about language, but about Server-Side Environment. How would you let your server handle request?

P.S. Should I put PHP-FPM in original question instead? :D

Collapse
 
andreidascalu profile image
Andrei Dascalu • Edited

You should really make your question clear as to the scope of the expected answer.

You can mention fpm, sure, but fpm is simply the interpreting engine.

As to how to let the server handle requests, it's more about what features you want and whether you have external constraints from other systems in your stack.

Theoretically both node and fpm benefit from a reverse proxy or lb in front of them. The downside of fpm is that it needs a compatible web server as opposed to node.

Fpm only interprets php code, so the proxy in front needs to route those requests for php files and be able to serve non+php directly, unless you want to write a generic we server in PHP (not efficient). Therefore php works with nginx or lighttpd unless you go modphp and use apache with the embedded module (where prefork raises questions on performance VS the multithreaded nature of nginx)

Node has no such constraints. It can work with plain proxies like haproxy or even serve requests straight up (but you would need to pay attention to stuff like security).

In a containerized environment, php shows some limitations in that nginx +fpm is a bit of a pain to setup efficiently (think kubernetes) and you end up using roughly 3 lb's in front of fpm, nevermind the inherent networking overhead (usually I go with apache there)

Node is easier from this perspective, but switching ecosystems coming from php is not that easy.

Myself I ended up going with Roadrunner for php, as it is wrapped in a very efficient golang server + enforces psr compliant requestd/responses

Thread Thread
 
razbakov profile image
Aleksey Razbakov

If you would start a new project, what would you make as your backend? Node or PHP? Express or Laravel? Let's say you write a REST API, what stack would you use and why?

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

I would make my decision based on the goal. For example I have projects that leave a lot of room for learning. I would go with express in this case just to improve my knowledge, but I would also use it in cases where node packages may be superior to php (in case of grpc for example)

Otherwise I would use php but I would not use any of the major frameworks (I have a performance comparison of returning 100k json encoded rows from two related tables, Laravel is the worst performer of all).
I would favor a microservices framework like Swoole if I weren't inclined to make a monolithic API. Otherwise I would use a microframework like Symlex but nowadays I sort of favor a custom solution built around a fast router (there's nikic but also the phpleague router). For an API there aren't many requirements: fast router with Middleware support, DI container (phpdi is excellent) and maybe doctrine for a strong orm with several layers of caching. Of course, logging and some metrics/observability would be nice.
I have a sort of personal 'framework' that rivals symfony 5 in performance but it still lags behind the likes of ubiquity 2 and symlex.
But my main requirement from a php stack would be to handle routing for psr requests and responses and neither symfony nor laravel do that.
This comes from the fact that nowadays I consider it mandatory for a php api to be deployable with roadrunner (symlex is built for roadrunner compatibility), in this case php blows node/express out of the water and can close in on rust performance for example.

Thread Thread
 
tracker1 profile image
Michael J. Ryan

I wouldn't use either for grpc... If you're interested in container orchestration like kubernetes, I might use node for it. You can't use grpc+cluster modules together iirc.

Collapse
 
juelcf profile image
Just another dev

Server Side PHP is the way to go.

dev.to/brewer1_jane/why-php-is-the...

Collapse
 
sfxworks profile image
Samuel Walker

Node if you want to do cloud work.

Collapse
 
razbakov profile image
Aleksey Razbakov

How would you implement state machines in Node? I am looking for a solid alternative to Symfony Workfow Component.

Collapse
 
tracker1 profile image
Michael J. Ryan

Depends on what you are trying to do... I use refund patterns.

Collapse
 
motss profile image
Rong Sen Ng • Edited

Neither or it depends.

Collapse
 
sunnepazzy123 profile image
sunnepazzy123

I prefer Php at backend because it easy for beginners...
But NodeJs is superb when it come to backend...

Collapse
 
mojskin profile image
MojSkin • Edited

I prefer PHP and Laravel as backend, and also a JS framework as fronted like VUE. Laravel is really nice, easy, handy and secure to develop a mid range project. I have to say, I love it!

Collapse
 
rosseyn profile image
Rosseyn

Never again with the php.

Collapse
 
weylar profile image
Idris

I use php (laravel) mostly for my backend logics. Although Node.js is now taking over

Collapse
 
xroal profile image
Roman Stejskal

Node all the way.

JavaScript syntax is much nicer than PHP, also more concise, and actually OO. JavaScript doesn't need special handling of UTF-8, unlike PHP, where my scripts very much like to save mumble-jumble into the database instead of the actual characters.

Node also has a proper package system, which lack of has bitten my ass many times in PHP (when the official docs tell to you composer install a package but you actually can't because a third dependency of both of them has a version mismatch... Looking at you, Lumen), but never in Node, where each package can require its own version of anything and it just won't clash.

TypeScript.

Also, native JSON support is nice when working with JSON based REST APIs or services in general.

Collapse
 
razbakov profile image
Aleksey Razbakov

JavaScript syntax is much nicer than PHP

Can you please share some nice projects in Node? Do you consider this github.com/HugoDF/express-bull-es6 as a good practice?

I am lacking some abstraction and organization of the code that PHP has.

Collapse
 
xroal profile image
Roman Stejskal

You can take a look at Example Node (Express + Mongoose) codebase containing real world examples.

What I meant specifically by syntax is, e.g.,

  • no $ for variables,
  • object literals { key: value } vs [ 'key' => value ],
  • anonymous functions (args) => retVal vs function (args) use (vars) { return retVal; }

What abstractions and organization of code do you mean?

Collapse
 
nerdlyist profile image
Coury Ryan Richards

Composer works great and you can 100% specify versions. Also, composer is the only one which do you prefer npm, bower, yarn... whatever comes next.

Syntax is an opinion. I mean don't get me wrong php could have helped itself with naming and parameter ordering consistency. Some people like ruby and I know cold fusion evangelist.

PHP has type hinting or you could move to Java or .net if you wany types and "actual oop/d".

Json is pretty straightforward with json_encode/json_decode. Where a lot of other languages you need an entire library to make that work on complex objects (no not js).

As for the question. I would say js/node are hot to trot right now. There are a lot of companies willing to pay top dollar for that skill set.

If you are starting a company though php can get all the work done for a fraction of the cost. Php developer (who creat the same systems) are very underpaid.

Also, realize whatever is best right now won't be in 10 years and you should always be evolving your skillset.

Also realize that best is very tricky and depends a lot on what you are doing.

My personal experience is Java, python,php and js. But I've gotten stuff done in ruby, cold fusion and .net.

Server wise node for js seems pretty standard. PHP has Apache mod-php which is well documented. If you are making an API I think php-fpm would be a good alternative although I usually run into issues when it comes to returning static content like images or css files. Still have not found a good solution for that without some hacky methods.

Collapse
 
tdias25 profile image
Thiago Dias

when people say simple sh*t like this i can't do nothing but wonder what the engineers who made complex frameworks such Symfony and ZEND would say lol

Collapse
 
randompages profile image
random pages

no. still php

Collapse
 
ttshivhula profile image
Tshivhula Tshilidzi

The question is not clear since you comparing two different things. Assuming you comparing PHP and Javascript. I prefer working with Javascript just because I think it's best to write in one language because in most cases you end up writing Javascript anyway. Php itself is still a solid language!!!

Collapse
 
razbakov profile image
Aleksey Razbakov

Let's say you need to write a newsletter system that would send email to 1k users weekly. Would you write this script in Javascript or PHP? Would you use any framework for that? What would your cronjob look like?

Collapse
 
chrisrichter profile image
chris-richter

I do something like that from firebase using AWS-SES with a nodejs cloud function (like lamda in AWS). I don't use the cloud scheduler for it, but that certainly could be done.

Similar logic lives in other functions that use pubsub for transactional emails and alerts.

The farther I can get away from email tasks and servers, the better. 1,000 emails is $0.10.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.