2

I have a service project that provides a HTTP server via TIdHTTPServer, and a web frontend. A number of functions that the user may initiate can take 5-10 seconds to complete, during which time they only see a gif animation while the ajax request waits for my delphi code to complete and return a status.

I'd like to implement a progress bar, or percentage (perhaps estimated time remaining, etc), but I'm not sure how it should be implemented. XMLHTTPRequest() has a progress event, which seems easy enough to implement on the client, but how do I have the server respond with it's progress?

Griffyn
  • 173
  • 1
  • 11
  • Related: http://stackoverflow.com/questions/76976/how-to-get-progress-from-xmlhttprequest – mjn Aug 17 '15 at 06:46
  • 2
    I will suppose that you already can hold progress of the function for each connection in any variable on the server side. Your client just sends the XMLHTTP request (which is easy to write). So all you have to do is to handle TIdHTTPServer event OnCommandGet, read request headers, find one called "X-Requested-With" with value "XMLHttpRequest" and return the progress instead of calling your function. – smooty86 Aug 17 '15 at 13:47

1 Answers1

2

From Ajaxpatterns.org:

Another way to deal with long XMLHttpRequest Calls is to explicitly introduce a second monitoring channel. While the primary request takes place, a sequence of monitoring requests are issued to ask the server for a progress estimates. For example, the server might be looping through 1000 records, running a transformation on each of those and saving it to the database. The loop variable can be exposed so that a monitoring service can convert it into a percentage remaining figure.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • This would be easy if there was only ever a single thread, but it's conceivable that identical requests might be made, even from the same client. I guess the client must generate a unique identifier for each request, and pass that along with the initial ajax request, and then again for each progress request. – Griffyn Aug 17 '15 at 14:30