Long Polling with PHP on Apache
Asked Answered
B

2

9

Hopefully I can explain this well enough. I am working on creating a PHP library to handle ajax requests through PHP in an object oriented manner. I am currently pondering a good way to implement a long polling solution but am curious about something.

Apache doesn't handle keeping multiple connections open very well. The thread-per-request model makes Apache highly inefficient for long polling. Using server's such as nginx and lighttpd handle these threads much better so in the library, I plan on implementing different functions optimized to the specific servers available from a single function call. In the case of Apache, which happens to hold an enormous market share for PHP applications, I need a better solution.

Is there a way to place an incoming request on hold/pause and continue processing other requests until I am ready to reactivate it?

I'm not sure if that makes sense or if I am even on the right track. What is a possible solution that SO recommends for long polling in PHP on an Apache server?

Basie answered 16/3, 2012 at 2:2 Comment(3)
why would you pause processing on the server? wouldn't that make your user wait a lot?Telemachus
With Apache, it uses a thread-per-request model so an initial page request in an application uses a thread, and then the ajax request (called once the DOM is loaded) will use a separate thread. The problem is attempting high volume traffic will spawn thousands of threads and ultimately disable the server.Basie
We had a similar problem, after turning on ajax/PHP long polling page loads started randomly (cca every 4-5 load) taking too much time...for now and only for notices I solved it by NOT sleep()in PHP in any way, and moved the timewait functionality to javascript, so now javascript is requesting new PHP execution each e.g. 10s but the page loads seems to be OK now.Committee
F
2

This sounds like continuations. You can definitely not do this in PHP, in any elegant way. If you want to do this, your best chance would be to save the current state, and write the code in such a way that you can resume from where you left off when you load the state.

Fragment answered 17/3, 2012 at 0:1 Comment(1)
I'm beginning to come to the same conclusion. I think I am just going to implement a basic long polling solution and in the documentation/comments annotate that it is less than optimal on certain server environments. That way if somebody wants to try to use it, the base functionality is already there.Basie
N
0

I don't think there is a solution. You can't distinguish a polling request from a normal request. Only avoid Apache that could help (e.g. running nginx on 80, forward all requests to Apache on 81, except polling requests).

I also don't think you have a problem. Nginx or other server is not much more efficient than Apache. Polling is a PHP request, Apache with mod_php is less or more a good choice. Nginx won't use less resource than Apache in serving PHP.

Nganngc answered 16/3, 2012 at 23:55 Comment(2)
Nginx won't use less resource than Apache in serving PHP. are you sure about that? serverfault.com/questions/157520/apache-vs-nginxBasie
Sure. I don't read anything in that url stating the opposite. "mod_php is marginally faster than php-fpm", "cgi is much slower than mod_php" etc. Of course, all depend on how much you do long polling vs normal PHP files/static files, on if you have a caching reverse proxy so that Apache is not bothered with static files, on how many modules you are using in Apache...Nganngc

© 2022 - 2024 — McMap. All rights reserved.