Asynchronous Requests (on the server)
Moderator: General Moderators
Asynchronous Requests (on the server)
Dear All,
I'm new to this forum and I hope i'm writing in the right place.
Before you move me to a newbie forum, please note, this is not an AJAX question, what i'm interested about is Aynchronous request handling on the server side of PHP.
I want to make an app, quite similar to a chat system, in which the client will poll the server for news CONSTANTLY. The obvious way is to have the client poll every "x" seconds, which depending on the value of "x" will either get unresponsive on the client, or will kill the server.
A method I found, and have successfully implemented in .Net is not responding to each poll with "nothing new here" every time, but instead keeping the client request (not responding at all), and only responding when there's effectively something to tell (or when something like 30 seconds go by, so as not to time out on the client)
Now, the "normal" way of doing this implies that while the server is not responding, it keeps a web server thread busy, which implies that i need 2000 threads if I have 2000 people on the client. (If I'm understanding this correctly)
.Net gave me this magical solution called "asynchronous handlers" that let me release the thread, but not reply to the client, and then only reply when i have to, using another thread (and a magical object I keep a reference to, which is the user's request). This lets me scale wildly while i still have only very few threads on the server.
Is it possible to do something like that in PHP?
Or at least, in ANY *nix environment at all (taking into account the fact that i'd like to avoid having to write my own web server)? (I can learn other languages like Python if I have to)
Has anyone heard of an approach like this that I can use?
Thank you very much in advance for your help
Daniel Magliola
I'm new to this forum and I hope i'm writing in the right place.
Before you move me to a newbie forum, please note, this is not an AJAX question, what i'm interested about is Aynchronous request handling on the server side of PHP.
I want to make an app, quite similar to a chat system, in which the client will poll the server for news CONSTANTLY. The obvious way is to have the client poll every "x" seconds, which depending on the value of "x" will either get unresponsive on the client, or will kill the server.
A method I found, and have successfully implemented in .Net is not responding to each poll with "nothing new here" every time, but instead keeping the client request (not responding at all), and only responding when there's effectively something to tell (or when something like 30 seconds go by, so as not to time out on the client)
Now, the "normal" way of doing this implies that while the server is not responding, it keeps a web server thread busy, which implies that i need 2000 threads if I have 2000 people on the client. (If I'm understanding this correctly)
.Net gave me this magical solution called "asynchronous handlers" that let me release the thread, but not reply to the client, and then only reply when i have to, using another thread (and a magical object I keep a reference to, which is the user's request). This lets me scale wildly while i still have only very few threads on the server.
Is it possible to do something like that in PHP?
Or at least, in ANY *nix environment at all (taking into account the fact that i'd like to avoid having to write my own web server)? (I can learn other languages like Python if I have to)
Has anyone heard of an approach like this that I can use?
Thank you very much in advance for your help
Daniel Magliola
Re: Asynchronous Requests (on the server)
You say this isn't Ajax, but it is on the server side.. But then you say this..
However if you actually want the SEVER to get data from another server or such, take a look at curl.
The only way for a web browser(client) to "poll" the server this way is with Javascript (Ajax) or an Applet like Flash/Java.dmagliola wrote:which the client will poll the server for news CONSTANTLY
However if you actually want the SEVER to get data from another server or such, take a look at curl.
Re: Asynchronous Requests (on the server)
Hi, thank you for your reply.
When I said this wasn't AJAX, i meant "this isn't the typical AJAX question". I said it because I assumed that since I said "asynchronous" in the subject, you'd assume I needed help with the XMLHTTPRequest object.
I know I need AJAX, and I will use it, but I do know how to do the client part of this.
My question is related to the server side, which will be in charge of replying to the AJAX requests. I want to do asynchronous handling on the server, so as to not keep threads busy, and also not have to reply immediately with "nothing new for you yet".
Thanks
Daniel
When I said this wasn't AJAX, i meant "this isn't the typical AJAX question". I said it because I assumed that since I said "asynchronous" in the subject, you'd assume I needed help with the XMLHTTPRequest object.
I know I need AJAX, and I will use it, but I do know how to do the client part of this.
My question is related to the server side, which will be in charge of replying to the AJAX requests. I want to do asynchronous handling on the server, so as to not keep threads busy, and also not have to reply immediately with "nothing new for you yet".
Thanks
Daniel
Re: Asynchronous Requests (on the server)
This sounds like what has been called COMET.
Unix HTTP servers that support long-lasting connections and thread sharing: this feature is offered by Jetty. I'm fairly sure there are other ones... google for Comet, asynchronous http, etc.
Also, I wouldn't exclude the possibility of writing your own server. It's wouldn't really be a webserver, it would be a network server that implements just enough of the HTTP protocol to be able to communicate with your clientside application, and it may be easier than trying to shoehorn a COMET application into a conventional webserver that has been designed with statelessness and quick transactions in mind.
Unix HTTP servers that support long-lasting connections and thread sharing: this feature is offered by Jetty. I'm fairly sure there are other ones... google for Comet, asynchronous http, etc.
Also, I wouldn't exclude the possibility of writing your own server. It's wouldn't really be a webserver, it would be a network server that implements just enough of the HTTP protocol to be able to communicate with your clientside application, and it may be easier than trying to shoehorn a COMET application into a conventional webserver that has been designed with statelessness and quick transactions in mind.
Re: Asynchronous Requests (on the server)
Dml,
Thank you very much for your response!
Yes, in fact what i'm trying to do (and which i'm doing in .Net) is exactly COMET. I didn't know it had a name.
I'll look into Jetty, that sounds pretty interesting. Someone else also mentioned Medusa, for Python.
I'd love to find something that I can write PHP in, but lacking that, Java and Python will do just fine.
About writing my own server software, truth is, I believe that falls quite far from my abilities. I know some of the theory behind it, but I don't think I can hand code myself a production-quality service that's managing sockets. It's way out of my league. I'd need to use something that's already done.
If i really can't find anything, i'll go the .Net way rather than try to write my own server program.
Thanks again for the response. You've given me many starting points to continue my search. I didn't know how what i'm doing was called, so I couldn't even google for it.
If you have any other ideas, please let me know.
Also, if you believe you'd be up to the task of writing a server like you describe, please do let me know. We might need to in the future when the project grows (for now, it'll be simple polling).
Thanks
Daniel
Thank you very much for your response!
Yes, in fact what i'm trying to do (and which i'm doing in .Net) is exactly COMET. I didn't know it had a name.
I'll look into Jetty, that sounds pretty interesting. Someone else also mentioned Medusa, for Python.
I'd love to find something that I can write PHP in, but lacking that, Java and Python will do just fine.
About writing my own server software, truth is, I believe that falls quite far from my abilities. I know some of the theory behind it, but I don't think I can hand code myself a production-quality service that's managing sockets. It's way out of my league. I'd need to use something that's already done.
If i really can't find anything, i'll go the .Net way rather than try to write my own server program.
Thanks again for the response. You've given me many starting points to continue my search. I didn't know how what i'm doing was called, so I couldn't even google for it.
If you have any other ideas, please let me know.
Also, if you believe you'd be up to the task of writing a server like you describe, please do let me know. We might need to in the future when the project grows (for now, it'll be simple polling).
Thanks
Daniel
Re: Asynchronous Requests (on the server)
PHP supports sockets.. i believe they even have an example for a sample chat server..
http://www.php.net/sockets
http://www.php.net/sockets
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: Asynchronous Requests (on the server)
There is nanoweb webserver written completely in php (and sibling project called nanoserv, which is pure socket server implementation written in php as well)About writing my own server software, truth is, I believe that falls quite far from my abilities. I know some of the theory behind it, but I don't think I can hand code myself a production-quality service that's managing sockets. It's way out of my league. I'd need to use something that's already done.
Re: Asynchronous Requests (on the server)
There's also Philtron by yours trully, that uses non-blocking sockets (nano uses fork if I recall correctly). (I haven't released anything on that project for a long time, but the socket code is well separated and documented enough to give you a start)
Re: Asynchronous Requests (on the server)
This might be relevant for you - http://www.chabotc.com/phpsocketdaemon/
A php socket server built specifically to handle COMET chat
A php socket server built specifically to handle COMET chat