Progress notifications from HTTP/REST service

Polling

The client keeps polling the server to get the status of the response.

Pros

  • Being really RESTful means cacheable and scaleable.

Cons

  • Not the best responsiveness if you do not want to poll your server too much.

Persistent connection

The server does not close its HTTP connection with the client until the response is complete. The server can send intermediate status through this connection using HTTP multiparts.

Comet is the most famous framework to implement this behaviour.

Pros

  • Best responsiveness, almost real-time notifications from the server.

Cons

  • Connection limit is limited on a web server, keeping a connection open for too long might, at best load your server, at worst open the server to Denial of Service attacks.

Client as a server

Make the server post status updates and the response to the client as if it were another RESTful application.

Pros

  • Best of every worlds, no resources are wasted waiting for the response, either on the server or on the client side.

Cons

  • You need a full HTTP server and web application stack on the client
  • Firewalls and routers with their default "no incoming connections at all" will get in the way.

Feel free to edit to add your thoughts or a new method!


I guess it depends on a few factors

  • How accurate the feedback can be (1 percent, 5 percent, 50 percent)
    Accurate feedback makes it worth pursuing some kind of progress bar and comet style push. If you can only say "Busy... hold on... almost there... done" then a simple ajax "are we there yet" poll is certainly easier to code.
  • How timely the Done message has to be seen by the client
  • How long each task takes (1 second, 10 seconds, 10 minutes)
    1 second makes it a bit moot. 10 seconds makes it worth it. 10 minutes means you're better off suggesting the user goes for a coffee break :-)
  • How many concurrent requests there will be
    Unless you've got a "special" server, live push style systems tend to eat connections and you'll be maxed out pretty quickly. Having to throw more webservers in for a fancy progress bar might hurt the budget.

I've got some sample code on 871184 that shows a hand rolled "forever frame" which seems to work out well. The project I developed that for isn't hammered all that hard though, the operations take a few seconds and we can give pretty accurate percent. The code uses asp.net and jquery, but the general techniques will work with any server and javascript framework.

edit As John points out, status reporting probably isn't the job of the RESTful service. But there's nothing that says you can't open an iframe on the client that hooks to a page on the server that polls the service. Theory says the server and the service will at least be closer to one another :-)


Look into Comet. You make a single request to the server and the server blocks and holds the connection open until an update in status occurs. Once that happens the response is sent and committed. The browser receives this response, handles it and immediately re-requests the same URL. The effect is that of events being pushed to the browser. There are pros and cons and it might not be appropriate for all use cases but would provide the most timely status updates.