Jump to content


Photo

Php Equivalent For Node.js ?


  • Please log in to reply
11 replies to this topic

#1 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 13 October 2012 - 01:25 PM

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?

#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,266 posts
  • LocationBelgium

Posted 13 October 2012 - 02:44 PM

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


1) Why is that bad? 2) Why not simply send Connection: close upon each request instead of telling it to persist? 3) Would nginx be an option? It uses FastCGI?

Edited by ignace, 13 October 2012 - 02:47 PM.


#3 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 13 October 2012 - 03:39 PM

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.

#4 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 13 October 2012 - 03:56 PM

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.
Keeping it simple.

#5 trq

trq

    Advanced Member

  • Administrators
  • 31,032 posts
  • LocationSydney, Australia.

Posted 13 October 2012 - 07:21 PM

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.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#6 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 14 October 2012 - 02:07 AM

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.

#7 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,781 posts
  • LocationBonita, FL

Posted 14 October 2012 - 02:40 AM

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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#8 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 14 October 2012 - 03:06 PM

Thanks!

#9 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 14 October 2012 - 03:40 PM

This posts reminds me of

Edited by Mahngiel, 14 October 2012 - 03:41 PM.

?????????????????? This is a signature ????????????????????????

What do you  mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#10 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 14 October 2012 - 07:55 PM

That was awesome!

#11 AliceWonder32

AliceWonder32

    Newbie

  • New Members
  • Pip
  • 8 posts

Posted 21 October 2012 - 09:29 PM

I now have a CLI php based socket server doing much of what I want (rest is matter of finishing my code), thank you kicken for the suggestion.

#12 Andy-H

Andy-H

    Web developer

  • Members
  • PipPipPip
  • 2,007 posts
  • LocationManchester - UK
  • Age:22

Posted 29 December 2012 - 02:34 PM

https://github.com/reactphp/react Written by @igorwesome, although personally, I'd just use NodeJS

Are you a PHP Developer looking for work in Stockport? See http://forums.phpfre...oper-stockport/





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com