Page 1 of 1

PHP node alternative?

Posted: Sun Mar 30, 2014 8:07 pm
by Jackolantern
This is pretty interesting. I don't know if it is a good idea to use it in production, but it is interesting nevertheless.

Basically, it is very similar to Node.js, but in PHP. It is called React PHP. It is an asynchronous non-blocking I/O evented platform. Sound familiar? Even the code will look quite familiar to node devs:

Code: Select all

require 'vendor/autoload.php';

$app = function ($request, $response) {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end("Hello World\n");
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run(); 
You could almost just take out the dollar signs and have the classic node web server example. It even have its own version of Socket.io called Ratchet. The thing is, I think I actually posted Ratchet here quite some time ago. It actually existed before React PHP did, and simply used a blocking I/O scheme in PHP CLI. They had to actually say on their website to not use it in production since it would fall apart after 10 or 12 users connected (due to blocking the thread). But Ratchet has new life with the creation of React PHP, now that it has a platform that can allow it to scale much better.

Of course, React's community would have to be a grain of sand compared to node's, and that does bring about certain challenges. But for those interested in playing on the bleeding edge of PHP or for those who come from a PHP background and just can't get used to node, this could be an option. What it needs now is its own version of Express :cool:

Re: PHP node alternative?

Posted: Mon Mar 31, 2014 2:24 pm
by hallsofvallhalla
VERY interesting. It will hard for PHP to get use to it if they make it look exactly like Node though. Why not just use Node :P I know the answer to that. You can now make MySQL data queries much easier and use your favorite PHP libraries and that makes it very interesting. What is the connection status now though as in numbers? If it was 12 before what is it now?

Re: PHP node alternative?

Posted: Tue Apr 01, 2014 2:52 am
by Jackolantern
I have no idea, but I would imagine it is much, much higher. The reason why Ratchet would fail after 10 or 12 connections was because it was using sequential, blocking I/O. Without threading, any non-asynchronous socket server will fail between about 10 - 20 connections just due to the amount of time waiting for network latency.

V8 is faster than PHP. That is a fact. But I would say PHP set up asynchronously with evented I/O could probably pull at least half as many connections as node. So (of course entirely dependent on what your code is doing) maybe an average estimate would be about 100 - 200 real-time connections. I think node can handle on average about 200 - 400 real-time connections on a single process depending on the code, and of course much more for serving non-real-time websites (which would be measured in hits/min).

But 100 - 200 would be workable if it is that high. Most of the old school MMORPG (EQ, DAoC and others) typically only allowed around 300 concurrent users at a time on any one server. And if you go by the figure that only 25% of your players are logged in at a time, and it is usually less than that, that means you could have 400 - 800 total players per "server". Not a bad number at all for an indie MMO.

Of course, I have no idea what ReactPHP's ability to work with child processes is, and thus have no idea if you could scale out a single instance. Node comes with that in-the-box.

Re: PHP node alternative?

Posted: Tue Apr 01, 2014 6:22 am
by a_bertrand
I can't say it makes me giggle... Sorry but I would really not pick PHP for such things. Sure I could use some existing PHP code, or library, but I doubt it makes sense. The base idea behind Node is that you don't use multiple thread as the user code should be short enough that it gives immediately the hand back to node. PHP? It's not meant to be like that, and all the existing code is meant to run on a "do once and die" basis.

Also, existing shared hosts will not allow you to start your own service, which means you would not be able to run it anyhow beside if you have a VPS or a dedicated server.

For all that, either use Node.JS or pick something of the class of C# / .NET or Java

BTW I don't really think the argument like "Yes but I'm good with PHP while not in JS" is any kind of answer. Today a web page without JS is... poor. And if you want websockets or any interactivity you will need to use JS on the client side. So why not use the same language on the backend as well?

Re: PHP node alternative?

Posted: Tue Apr 01, 2014 1:53 pm
by hallsofvallhalla
very good points.

Re: PHP node alternative?

Posted: Tue Apr 01, 2014 4:08 pm
by Jackolantern
I was never actually considering to use it, and I suggest not to use it in production ;)

I actually like node. I just thought it was interesting is all.