I’m working on a web application that needs to frequently poll the server database and check for any available data to the client.
Ideally, I would need to be able get a javascript function callback in the server, in order to be able to call the javascript function back whenever any new data is available on the database, instead of having to poll the server every 5 seconds.
Simplifying, I would need to be able to call a method in the server and pass a js function as a parameter for callback. I want to avoid the overhead of polling the server repeatedly.
Is there any way this could be possibly done with asp.net and ajax?
The alternative that you’re speaking about is some form of COMET style interface, where the client makes a request and the server holds on to it until it has a response. WCF has the PollingDuplex binding type, but I’m not terribly certain how to go about implementing it in javascript.
Well, this appears to be a very relevant link. It says Silverlight all over it, but it’s an specifically for an AJAX browser application:
http://www.silverlightshow.net/news/AJAX-Client-for-HTTP-Polling-Duplex-WCF-Channel-in-Microsoft-Silverlight-3-.aspx
UPDATE:
I don’t understand why all the alternate answers amount to “omg do polling on your own!” We have modern frameworks and protocols specifically to reduce to the amount of manual code we write. The PollingDuplex functionality provided by WCF does exactly what you want, and the link I provided implements the client side of that system in pure javascript.
Not sure what more you could want.
UPDATE 2:
Sorry, original article link. We all recognize that polling is the only solution for HTTP folks. The primary difference that the author is looking for is simulated push vs constant polling. You can achieve simulated push by placing a long running polling request to the server. With the right server architecture in place, it’ll hold onto that request until it has data. Then it’ll respond. At that point, the client immediately re-requests. In this manner, you utilize the existing HTTP request-response cycle to simulate “pushing” to the client.
This is primarily accomplished by having the appropriate server architecture in place. That way your requests are put to sleep and woken up in the appropriate manner. This is a far better paradigm than a manual polling answer where you ask the server for updates every 5 seconds. Chatty applications where you want responses now and not 4.8s from now is what
I’m talking about. Manual polling (making a request for new data every 5s) gives the appearance of being laggy and during periods where no data is updated, it causes unnecessary request/response cycles.
From the article:
From the .HTM file in the download:
There’s no silverlight objects being created or libraries being referenced. I can dump the .JS files into here as well, to show that it is merely a “reusable, stand-alone JavaScript library that implements the client side of the HTTP polling duplex protocol” and didn’t hinge on silverlight being installed.