Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce a number of AJAX requests for a chat?

I have a chat on my web site and it needs to send many AJAX requests to my server (at least 2 times per second) in order to check if there are new messages.
Is there a way to reduce the number of requests and reload messages only after they have been posted?
I know that there are no possibility to use sockets (because I can't use flash, java or features of HTML5), but maybe there is some trick dealing with keep alive option of HTTP 1.1?

like image 453
tsds Avatar asked May 17 '11 19:05

tsds


1 Answers

Yes, there is an easy way to do this. Effectively, what you do is you increase the timeout for your ajax call to a long timeout (say, 5 minutes). Your server receives the request and then holds it, occasionally checking for new responses. Then, when a new response is warranted, it simply responds to the request, and your client receives the update.

If no response is had within 5 minutes, your client simply timeouts and starts a new ajax request. Your server, if it hasn't responded within 5 minutes, usually then just terminates the held request to get it out of the queue.

Effectively 'server-side' pushing.

like image 60
Tejs Avatar answered Sep 23 '22 19:09

Tejs