views:

32

answers:

3

I can easely use javascript to auto refresh the page every 10 sec for example but, isnt this going to overload the page having to ask for any change every 10 secons? is there a more efitient way to do this. Some body told me once than there is a way having the client side waiting for a response from the server that only sends it when it really has some change, for example some budy wrote a text in the chat. Is this really factible to do? is there a better way?.

A: 

Why refresh a chat page? Why not just check the server to see if there are new posts/chats etc. If so, append those to your chat box.

Have your timer (setTimeOut) fire say ever 5 seconds:

Quick pseudo code:

function updateChat()
{
  //Query a web service on the server for new text chat
  //If successful append the chat to the chat area

  //Call timeout again.
  setTimeout(updateChat, 500);
}

Edit: Seeing you have .NET I will assume you're using ASP.NET. Here's a link to a demo chat program:

http://trappedinhoth.blogspot.com/2009/04/ajax-jquery-chat-demo.html

Update: Also another ASP.NET / JQuery chat prog that will soon be available on CodePlex.

http://aspnetjquerychat.codeplex.com/

Ryan Ternier
ok, thats a good solution, but is not to much calls to de server? is this really efficient? is there another way?
Giancarlo Solarino
@Giancarlo Solarino ~ http://google.com/search?q=premature+optimization ... give it up, quit trying to second guess what too much load is. Write it correctly, then see if it's too much load. For reference, servers can handle load like this for thousands of users, with no discernible hit.
drachenstern
It's loads more efficient than refreshing an entire page. This is a similar approach to how FaceBook chat works.
Ryan Ternier
BTW: It is `setTimeout` (not `setTimeOut`). Also, 5 seconds is 5000 milliseconds, not 500 (that would be 0.5 seconds).
jake33
Thanks for the type (Corrected). I did set it to 500ms. 5 seconds is forever sometimes for chat.
Ryan Ternier
Yeah, I thought about that. But your answer still says: "Have your timer (setTimeOut) fire say ever 5 seconds"
jake33
A: 

You can apply outputcache on server side. IN this way server only need to generate 1 answer per each 10 seconds

Sergey Osypchuk
A: 

You can try using the new WebSockets technology in JavaScript. It is not supported in many browsers (only Firefox 4, Chrome 4, and Safari 5 currently support it), but it allows the connection to the server to remain "open" and then the server can send a message to the client. More info:

jake33