views:

287

answers:

6

Context: From my javascript web UI, I launch a long-running (several minutes) operation that is conducted on the .NET2.0 backend. The call returns immediately with operation ID while the long-running operation run parallel. The operations are not CPU intensive, but conduct slow network calls. Once the operation is completed, I want to see the results in the web UI.

Question: How can I notify the client when the job is done?

Options I have considered:

Option 1: I launch the long-running operation asynchronously directly from JS and I expect the return value to be the endresult instead of an operation ID. My AJAX library takes care of everything, and life looks very easy and neat. The problem is that on the server side the thread is a ThreadPool thread which I now lock up for several minutes. You do not need too many long-running parallel requests to cause the ThreadPool to starve and bring the entire server to it knees even if there is sufficient processing power.

Option 2: With the operation ID, I start to poll the server if the operation is completed. However, this is a denial of service attack against my own server. Furthermore, there must be fair ajax solution to it. This is not a unique problem.

A: 

what about having hidden (or small) iframe run the long-running script and making javascript to check only the content of the iframe? e.g. script writes ID to the iframe on its end.

dusoft
+3  A: 

Polling, if done properly, will not have a noticeable effect on your server load. You should customize the interval to best cover the specifics of your server job - e.g. you don't poll the server for at least 2 minutes if you know that the operation usually takes longer. Even after that you choose a big enough interval - e.g. 10 sec. Using a web service request or a custom handler will minimize the traffic and the server time needed to process the request.

lingvomir
A: 

Your option 2 is exactly what I've done before. Use setTimeout to limit your polling to every 30 seconds or so.

Geoff
+2  A: 

Perhaps reverse ajax is useful if you wont have too many concurrent clients. This is sometimes called comet.

Reverse Ajax refers to an Ajax design pattern that uses long-lived HTTP connections to enable low-latency communication between a web server and a browser. Basically it is a way of sending data from client to server and a mechanism for pushing server data back to the browser(what you need).

http://en.wikipedia.org/wiki/Reverse%5FAjax

Sounds like it might suit your needs, i just used it and it worked really well.

Paul Whelan
I've seen some browser-based instant messenger clients make HTTP requests of the server with long timeouts. The server can then reply whenever it has some data to push to the client. I guess this is the same thing.
Drew Noakes
Drew yes it, it can be seen as a push from the server to the client. of course you still need the long-lived http connection thats why you don't want thousands of simultaneous connections.
Paul Whelan
A: 

The ICEfaces Framework provides an AJAX Push technology. They support Scalable Asynchronous Request Processing on various application servers like Jetty, Tomcat and Glassfish.

While this is all Java stuff, it might help finding the right search terms for google.

Tim Büthe
+3  A: 

Check out WebSync, a comet implementation for .NET. Should be exactly what you're looking for.

jvenema