Long Polling: How do I calm it down?
I'm working on a simple chat implementation in a function that has an ajax
call that invokes a setTimeout to call itself on success. This runs every
30 seconds or so. This works fine, but I'd like a more immediate
notification when a message has come. I'm seeing a lot of examples for
long polling with jQuery code that looks something like this:
function poll()
{
$.ajax(
{
data:{"foo":"bar"},
url:"webservice.do",
success:function(msg)
{
doSomething(msg);
},
complete:poll
});
}
I understand how this works, but this will just keep repeatedly sending
requests to the server immediately. Seems to me there needs to be some
logic on the server that will hold off until something has changed,
otherwise a response is immediately sent back, even if there is nothing
new to report. Is this handled purely in javascript or am I missing
something to be implemented server-side? If it is handled on the server,
is pausing server execution really a good idea? In all of your experience,
what is a better way of handling this? Is my setTimeout() method
sufficient, maybe with just a smaller timeout?
I know about websockets, but as they are not widely supported yet, I'd
like to stick to current-gen techniques.
No comments:
Post a Comment