views:

62

answers:

2

I want to dynamically updating a part of a web page for all users on it when I chose to (through a seperate page).

The scenario I'm thinking of:

  1. Say there's a chat box and a picture on a page.
  2. When I click a button on another page (or the same), the picture changes to an embedded youtube video for all users on the page chatting.

What's the best/easiest way to do this?

+2  A: 

You'll either need the client to poll (i.e. regularly make AJAX requests to the server to check for changes) or use Comet: basically a long-lived request which keeps an open connection to the server, and the server only returns either to let the request time out (at which point the client makes another request) or there's a change.

Note that if you do this with a popular site, you will need to be able to cope with a lot of simultaneous connections. The "one thread per connection" approach really sucks if you need to have ten thousand connections at a time. You'll want to be able to serve those requests asynchronously, basically. How you achieve that will depend on your server platform.

Jon Skeet
I'm currently considering using javascript/ajax for this - are there any benefits to using Comet?
stringo0
Comet is a technique more than a technology in itself. You'd still be using Javascript/AJAX, but your AJAX request would hang (at the server) until either there was a change or the request timed out.
Jon Skeet
+2  A: 

I would not be using 'Comet'.

It is almost certainly not neccessary for this update to be immediate across all people viewing the website.

To be perfectly honest I'd probably sneak this update it when you update whatever other data they are updating (say, the chat logs). Thus you'd just mark the video as the 'next' live item for that segment, and the code already updating the chatting would change that value if it noted it needed a new one.

Noon Silk
Including it with the chat logs is a good idea... but for responsive chatting I suspect you'd want something like Comet in the first place :)
Jon Skeet
I'll disagree with that, but I guess before we could definitively prove which method is better we'd need to compare implementations :P
Noon Silk
Well you haven't said how you'd implement the chat side in a responsive way, so there's nothing to compare even in theory. Of course if you're talking about opening a direct connection from a Java applet or something like that, that's different - but with pure HTML+JS, I'd be interested to hear how you'd get chat working nicely without a hanging request.
Jon Skeet
I haven't thought about it and if I were to implement it i'd give it more thought than I'm willing to give considering I *won't* be doing it. But my first thought would be a polling model with some statistical analysis to determine the polling time. And, of course, on each submission of data a pre-emptive check for new messages. But that's pretty naive, obviously. More rigorous thought it required if I were to actually do it, and it may be determined that your model is desired. But my 'feeling', is it probably isn't.
Noon Silk
I don't have control over the chat - it's a simple embedded chat :P
stringo0