How to reduce the number of AJAX requests for chat?

I have a chat on my website and I need to send many AJAX requests to my server (at least 2 times per second) to check if there are new messages.
Is there a way to reduce the number of requests and reload messages only after they are published?
I know that there is no way to use sockets (because I can’t use flash, java or HTML5 functions), but maybe there is some trick associated with saving the version of HTTP 1.1?

+5
source share
1 answer

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

If no response is received within 5 minutes, your client will simply disconnect and start a new ajax request. Your server, if it does not respond within 5 minutes, usually simply terminates the held request in order to withdraw it from the queue.

Effectively server-side.

+6
source

All Articles