Jump to content

Php Equivalent For Node.js ?


Recommended Posts

Working on a web application involving server sent events.

Apache is not good for this because of the persistent connections.

node.js is awesome for this - but it's Javascript.

 

I like postgresql (which I prefer to interface with via PDO), I like php, I like to keep the server side of things all in one language if I can.

I've found node.js interface to MySQL but it really looks like node.js database layers are bolt on and not that mature yet.

 

Is there a good web server for maintaining lots of connections that runs php?

Link to comment
Share on other sites

Apache runs out of threads. It just can not maintain that many simultaneous connections.

 

The point of Server Sent Events is that the web server can send messages to the browser without the browser needing to request them. If the connection is broken, the browser re-connects with the ID of last message it received so that it can get any messages it missed. It allows web applications that need server push capabilities to work without using flash or java - and without frequent polling that can be very hard on the server.

 

I don't know if nginx is an option or not, I've not used it.

Link to comment
Share on other sites

If you want to make a socket server in PHP, then I think you'll find everything you need in the PHP manual. It's quite simple to make a socket server in PHP after all.

That said, if your Apache install is running out of threads you're either configured it wrong, written some bad code, or you have a huge number of unique visitors. I suspect that the first two scenarios are the most likely ones.

Link to comment
Share on other sites

node.js is awesome for this - but it's Javascript.

 

I'm not sure I see the point here. Javascript is an awesome language that should be known by all web developers IMO.

 

node.js database layers are bolt on and not that mature yet.

 

Just like PHP, everything in node is an extension library, and yeah, a lot of this stuff is pretty new. Node.js is however advancing really quickly and there are a lot of real smart dudes working on / with it.

Link to comment
Share on other sites

The point is that by using the same language for all things in the web app, code can be shared. For example, the class for session handling (I keep session info in a database), database connection, etc. so that future maintenance of the web application does not require changes to the php side of things and then the Javascript port of the php side of things. You can update a single class that both the main web app and the server side EventSource page.

 

Of course the web developer needs to know Javascript for the client side programming, but the client side programming doesn't interface with server resources directly, it sends those as requests to the web application.

 

If I have to do that part of the web app in node.js I'll do it in node.js but I would prefer to do it in php because the rest of the web app (a social network platform) is php. What I'm trying to do is leverage the power of html5 to remove all use of plugins. I hate browser plugins with a passion. Probably from my days running ppc linux where there were none so any site that required them was broken, but hell, I even hate dependence upon them in Windows. To keep everything server side php is why I'm seeking a php capable http server that does not have the same persistent connection limitations of Apache.

Link to comment
Share on other sites

What you would end up doing with PHP is create your server socket and then use non-blocking IO + stream_select to handle all the incoming connections. You could manage quite a few connections that way in just a single thread/process. Throwing pcntl_fork into the mix may allow even more connections to be handled.

 

https://aoeex.com/chat/phpchatd.phps is a example I made a while back that uses the principal above. It acts as a simple server that handles all the long-poll connections for a AJAX chat room. The demo chat room that uses it is at https://aoeex.com/chat/chat.html.

Link to comment
Share on other sites

  • 2 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.