php vs nodejs performance on websockets server

Asked

Viewed 1,284 times

5

What I take for granted at the moment is that nodejs is more suitable to work with a constant pool of socket connections (websockets in this case) because being single-threaded each new connection generates a minimal extra memory consumption (a few hundred Kbs), for php each new connection would generate a new process that would consume a few extra megabytes.

Am I right or wrong in this idea? regardless of the answer, because one language would be preferable to another in the implementation of a websockets server?

  • For those who are interested, this question was prompted by these comments: http://answall.com/questions/9566/howto checkSe-users-está-online#comment236112_113233

  • 1

    I imagine you are wrong, because if you use PHP, you will leave the same PHP running to manage all the connections, because it would not be run by the HTTP server. An example, which is not websockets, but is comparable, is Guerrillamail’s SMTP. It is a single service running, receiving a gigantic number of simultaneous connections without the least performance problem, in PHP.

  • 2

    To complement: this SMTP server in PHP is totally non-blocking IO, and uses a single thread. It would be no problem to use the same method to serve Websockets. Sockets usually work well with non-blocking in virtually any language. A basic state-machine can manage multiple connections with ease, and from conventional sockets to Websockets, what changes most is initial trading only.

  • 2

    @Bacco very good, that’s what I said in the other post to Bruno, it was probably a mistake he had and the memory problem he supposedly faced with PHP was with some "misspelled" script: http://answall.com/questions/9566/howto verifyis-online#comment236125_113233

  • Total confusion on my comparison of nodejs vs php+apache, because it is apache who spawnes new processes for each request and running nodejs behind it would have the same result, so there is no clear advantage in nodejs when it comes to connecting memory as I thought. However it is still interesting to see responses with evaluations of both languages in this case, there are other factors for comparison such as php GC which is historically full of memory Leaks (although a few years ago has improved a lot) and nodejs is async natively.

  • 2

    My friend, what edition was that? It won’t help/benefit other users who have your question.

  • I did the rollback, because although "vague" or that you have confused, no one answered and we can reuse the question to talk only about the websockets.

  • I use php for websocket and am satisfied. And I’m fine, thank you. ;P

  • @Brunorb edited the question to focus only on the websocket question that seems interesting yet.

  • The doubt was a confusion of mine that I thought would not be relevant to others so I decided to cut it, but if you think it can benefit others quiet. But now with the last issue got very confusing, long-Polling is different from websockets and "constant long-pooling of connections" doesn’t seem to make sense. I’ll reverse the original version since no one had complained, but nothing against editing to improve it.

  • @Brunorb instead of using <del> and comment that had an error you should DELETE the question, please understand we are not a forum, being a Q&A here should be a question and answers, so the issue there was what made the people come here to comment, she got quite strange. In the case I edited, because the initial doubt arose about websockets and even if it was some confusion of understanding I’ve heard other people say that php is bad for a certain thing, the question can be well received, let’s see with time.

  • Some references in case someone wants to formulate a reply (if I answer myself if no one appears): http://stackoverflow.com/questions/15220501/websockets-php, http://stackoverflow.com/questions/11400541/are-websockets-suitable-for-use-with-php,.

Show 7 more comments

1 answer

3


I’m right or wrong in this idea?

You are partially right, or partially wrong.

Nodejs works with a single process, which executes a loop de eventos. Every message exchanged, connection that opens, connection that closes is one evento. Thus, in theory, it is easier to develop websocket applications with Nodejs.

With PHP you can try to do the same by creating a process long running that communicates with your PHP application. There are already some libraries to help you with this architecture:

However, if you want something much more like Nodejs, there is the extension Phpreact.

Because one language would be preferable to another in the implementation of a websocket server?

First comes taste. Use the language you like, even if it doesn’t perform as well as others.

Secondly, there is the labour market. If you create a project that ends up becoming a product or a company, use a language that is easier to find people to work with you.

Last place comes cost. In the future, if your application has millions of visits and you want to save money, it is worth changing language, even if this language is not your favorite or is not very popular with developers. Netflix migrated the application that generates the Java user interface to Nodejs and thus reduced server spend by 70%. Nodejs In Flames

Browser other questions tagged

You are not signed in. Login or sign up in order to post.