admin管理员组

文章数量:1426231

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)

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)

Share Improve this question asked Jun 10, 2010 at 5:48 Michael StumMichael Stum 181k119 gold badges409 silver badges541 bronze badges 2
  • 2 HTML5 WebSockets have support for true push from the server, but to use that you may have to make sacrifices on the browsers your application supports, and add a server side implementation of WS. – Anurag Commented Jun 10, 2010 at 6:42
  • Thanks. I might actually look at that as I'm mostly in charge of the Browsers, only IE8 may be an issue, but they could then fall back to polling or manual refresh. – Michael Stum Commented Jun 10, 2010 at 7:31
Add a ment  | 

6 Answers 6

Reset to default 3

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

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.

A Comet style workaround is the most mon 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.

As Samuel Neff says, "You're going to need an open connect to "push" data from the server to the client."

You can use a service like pubnub to open persistent connections from the client and support fallbacks for older browsers.

I made a small demo to show you how the front-end of this application may work. The demo shows PubNub latency over time. The source is available here.

The browser subscribes to a channel and fires a callback when a message is received.

 pubnub.subscribe({
     channel: 'my_channel',
     message: function(m){console.log(m)}
 });

In the demo the client also publishes messages. In your case you would include the PubNub IIS library.

pubnub.Subscribe<string>(channel="mychannel", DisplaySubscribeReturnMessage,    DisplaySubscribeConnectStatusMessage, DisplayErrorMessage);
// NOTE: DisplaySubscribeReturnMessage, DisplaySubscribeConnectStatusMessage and DisplayErrorMessage are callback methods

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 munications, 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.

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

本文标签: httpPushing notifications to a JavaScriptStack Overflow