Page 1 of 1

ajax chatbox question.

Posted: Mon Sep 19, 2011 1:01 pm
by Isaac
How often should I have a chatbox use ajax to check for new comments? Is every second too often?

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 6:01 pm
by fliptw
if once a second is too much you have a seriously bad connection.

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 6:32 pm
by Isaac
So twice a second won't bother people?

I might not have been clear. I'm wondering if people's machines might go slower because of an ajax module constantly calling out.

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 7:48 pm
by fliptw
as long as you don't let the chat box get too large it shouldn't be an issue. Keep each request to a maximum number of bytes and it should be good.

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 8:17 pm
by Isaac
gotchya! Thanks!

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 9:36 pm
by Jeff250
You could just have the client ask the server to poll every second for the next 30 seconds for you, returning either 1) the first newly discovered comment(s) or 2) nothing after the 30 second timeout, without the constant chatter between the client and server.

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 9:53 pm
by Isaac
I'm not too clear on that first line. It sounds like the server can forward info to the browser, with the client just waiting. We can do that? The browser can just sit there, requesting nothing, and expect to get data from the server when the server has something to send? Normally, I imagine we're pulling data from the server, normally. You're saying we can push data from the server?

Sorry, for not understanding. Thanks for your comment!

Re: ajax chatbox question.

Posted: Mon Sep 19, 2011 10:06 pm
by Jeff250
No, the client still makes a request, but the server just doesn't have to respond immediately. It can wait up to, say, 30 seconds before having to give its "no new comments" response. Otherwise, if it finds new comments before then, then all the better--it just immediately returns those comments instead of checking for new ones for the whole 30 seconds.

Re: ajax chatbox question.

Posted: Thu Dec 01, 2011 1:42 pm
by Thenior
I wonder how you'd do that though? Seems like default timeouts would get in the way.

Re: ajax chatbox question.

Posted: Thu Dec 01, 2011 2:05 pm
by Jeff250
I've found a good summary:
http://en.wikipedia.org/wiki/Comet_%28programming%29

The "long polling" approach is basically what I was describing.

I think something like 30 seconds should be safe, but timeouts might not even be a problem as long as you handle them properly.

Re: ajax chatbox question.

Posted: Thu Dec 01, 2011 7:35 pm
by Isaac
I'll be doing this during Christmas break.