开发者

Pushing notifications to a JavaScript?

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 f开发者_开发技巧unction 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)


If you're looking for a comet 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 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.


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.

Pushing notifications to a JavaScript?

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜