Is there an alternative to Ajax in the following scenario?

Let’s say I have this UI (just for asking my question purpose)

There are multiple download buttons/text there. The way it’s going to work is, when a user clicks on the Download text/option, it’s going to send an Ajax call to the Java RESTful webservice and that thing is going to take time because the stored procedure associated with it could take hours to finish and it could take a while before user gets any response back to the UI.
There’s another table in the backend which start keeping track of the status of the request (like RUNNING, COMPLETE,ERRONEOUS etc) as soon as the call to the main stored procedure is made via an Ajax call.

Since user could send series of requests: By series of requests I mean :

  1. User sends an Ajax request which triggers the backend stored procedure, and this SP is going to take long time.
  2. While the request in Step #1 is still running, user could send another Ajax request ( I don’t know if this is possible or not because I am using Ajax; I might have to use something else to handle (send and receive) asynchronous requests?)
  3. Similarly, if the above two requests are still RUNNING, user could send 3rd request and so on and so forth.

Could anyone please tell me if this is a correct way to handle such requests? I mean, is Ajax should be used in this scenario or something else?

Please let me know if I could elaborate on anything else or if my explanation is unclear. Thanks !


If you do this you’re going to make people think your app has crashed and you run the risk of users hitting the download button multiple times, which sounds like it’ll bring your backend to its knees.

I wouldn’t publicly offer up any endpoints that are going to kick off a backend process that takes hours to complete (bear in mind that you don’t need a browser to send requests to a web service). And are you expecting the user to keep their browsers open until the download is ready? I think I’d bounce tbh.

If I was doing something like this, I’d make a form where users can select whatever they need to download. Have them enter an email address, then mail them a download link when your backend has prepared everything.

1 Like

Hi Pullo,

Nope. User could go home after they click on download button or they can click on other downloads. Since I would know the status of request, what I could do is, if user try to click on same download button, I could tell them that it’s still RUNNING and not yet COMPLETED. However, they are free to click on other download options. Once everything is finished, I would need to find some way to let the user know like email or something and then have them download the data using a link to the backend path. Does this approach sounds better?

I was wondering is Ajax call going to allow user run multiple download requests? For example, if user decides to click on 3 download options one by one, do you think this would work? Thanks !

Once you start talking about things that take a long time, your AJAX call is out of the question. AJAX requests will time out. You’re going to need a heartbeat call to check if the download is ready or WebSocket connection. The original AJAX call could initiate the request, but if it takes more than a second or 2, you need to use something else to listen for the response.

Yeah, I am worried about the same thing. So if I go ahead with heartbeat call approach, I am still going to stuck with the first request only until it is finished, right? In my scenario, user could initiate another request as well side by side so would this heartbeat approach be still good in this scenario?

OR any other approach like using promise in javascript?

Your first request should resolve with a 204 - No Content while it processes the request.

The heartbeat is to check if it’s done.

Could you elaborate more on your answer?Thanks!

When you make the first request to start the process, then your server should respond with a 204 No Content. This says that it was successful but the server has no data to give the client.

From there, you should check another endpoint with a heartbeat to see if it has completed. If it has completed then you download it.

A system I am rewriting does something similar. It was solved by sending the reports to the user’s email because the reports are very time consuming. There is no need for a heartbeat or websocket because the data is handled outside the web client.

I see. Thanks for elaborating.

  1. So, do I have to program the server to send out this 204 No Content or is it going to happen automatically when an Ajax call is made for the first time (with this first request).

  2. Secondly, when a user decides to hit another download button while the first one is still pending/running, with the approach you have mentioned, it’s not going to block the user, right? By blocking, I mean, user can easily click on other download options and initiate other requests without waiting to have the first one complete, right?

Thanks again !

If you go to your Profile Activity page here, you will see a “Download All” button. When you click it and confirm, there is a modal that displays letting you know that the server-side process was initiated.

When the process is complete, Discourse gives you a Notification of the Message that contains the download link

IMHO, this approach works very well.

Do you know if what JS library they are using for download?

AJAX able to make async. requests. So if you need only request without permanently feedback - AJAX is absolutely OK.

But if you need feedback, I think web-sockets would be alternative.

Thanks. Do you know what is happening behind the scenes for this download thing mentioned in the post above:

Discourse is built with Ruby and Ember, heavy on the Ember.

My rough description of what happens is

  1. user requests a file be put together for downloading
  2. the task is put in a job queue
  3. the job runs, creating the file
  4. a notification that the task has completed is created
  5. if or when the user is logged in they see the notification
  6. opening the notification provides an expiring file download link

Oh Okay. So this sitepoint community website uses discourse. Thanks. I am only going to use jQuery/Javascript to get the files from the server.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.