PHP node alternative?

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

PHP node alternative?

Post 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:
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP node alternative?

Post 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?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP node alternative?

Post 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.
The indelible lord of tl;dr
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: PHP node alternative?

Post 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?
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP node alternative?

Post by hallsofvallhalla »

very good points.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP node alternative?

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “General Development”