views:

95

answers:

5

I'm just wondering if there is a way to have a server push information to a JavaScript function. Essentially I have a Dashboard-type page that has a javaScript function to get updates from the server and update the dashboard.

I would like my server to be able to "ping" the JS.

I don't even know how that could be possible (I'm guessing Twitter and Facebook use polling?), but I'd thought I ask.

I heard of Comet, but I don't know if that works with a plain standard IIS 7 installation? (It's a SharePoint 2010 site if that matters in any way) If I understand it correctly, Comet is essentially a constantly open connection, so it seems like it's actually the opposite of what I want (reducing # of requests and therefore load)

+2  A: 

Truly initiating a connection from the server is not possible using HTTP. Comet isn't really a single technique, but a set of different workarounds (many of which are described at the article you linked).

For information on Comet techniques with IIS, see the prior question, Comet Programming in IIS. One of the programs discussed there is WebSync.

Matthew Flaschen
A: 

You have to do it the other way around, by having the client "pinging" the server with JS.

You can do something like:

function pollServer()
    {
    // Get some parameter
    var param = .......
    AJAXCall("page.php?param="+param, onReturn);
    }

function onReturn(response)
    {
    // do something with response
    setTimeout("pollServer()", 5000);
    }

pollServer();

AJAXCall being the function you use to do an AJAX call and that calls onReturn when it gets a response. Once it gets a response it waits in this case 5 seconds and polls the server again

nico
+2  A: 

A Comet style workaround is the most common way to get this functionality. The connection is not constantly open, but rather throttled to make calls every x seconds, then try again upon timeout. The timeout essentially means that the server didn't have anything to give to the client in the duration of the poll. You'll see that the Etherpad code used this same approach, which has been integrated into other Google products now like Google Docs and Wave.

Jeff Schumacher
Thanks, I'll look at Etherpad's source.
Michael Stum
+1  A: 

You're going to need an open connect to "push" data from the server to the client. So even if you went the route of using a plugin like Flash to open a socket connection which supports two-way communications, you have an open socket connection.

Your statement "reducing # of requests and therefore load" really is problematic. You're equating number of requests with load and that is not accurate. With Comet the majority of requests are waiting on data. Therefore you can have a very high number of requests, but really a very low load on the server--it's hardly using resources besides a waiting thread from the worker thread pool.

Use Comet. Works great, is simple to implement, and does exactly what you need.

Sam
+2  A: 

If you're looking for a comet server for IIS, check out WebSync; it's exactly that :)

jvenema