1

Is it possible to have one page send an ajax request and have another page handle the response ?

The use case is that of a long running task. The user submits an ajax post to the server, the server processes the job for the time it takes, could be a few minutes, and then the response is returned, but the user may have browsed to another page.

I would like to be able to have the same JavaScript functions to handle the response on all pages, in order to handle the ajax response from the original page. Is this possible ? If not any suggested solutions ?

I am using spring framework.

Thanks.

phil.e.b
  • 2,491
  • 2
  • 16
  • 17
  • You can use in the backend some queue processing, with no necessary of ajax requests. What language are you using ? – epsilones Oct 29 '13 at 00:03
  • @Newben I agree you can use a queue based approach, but if you want to receive data without having to manually refresh the page I don't see an alternative to Ajax – TGH Oct 29 '13 at 00:13
  • @TGH yes sure, this is the inconvenient – epsilones Oct 29 '13 at 00:15
  • yes i could have some repeated code on every page that would poll the server but I was wondering if there was a nicer way to do this with ajax. Have the code repeated on the required pages but no need to poll, simply wait for the ajax response, and since the same callback handler would be repeated on all pages, it would handle the respones. i am using spring framework. – phil.e.b Oct 29 '13 at 01:25

3 Answers3

3

You can use Spring MVC's Async capabilities: (http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/new-in-3.2.html)

An example (http://blog.inflinx.com/2012/09/09/spring-async-and-future-report-generation-example/)

redmallard
  • 1,093
  • 6
  • 10
  • the goal is to receive the async response in a different page than the one from which the request was sent. – phil.e.b Oct 29 '13 at 01:21
  • You need to do a combination of two things: 1) maintain state of the long running task somewhere on the server (either in the user's session, or ideally in some other distributed persistence so your application can scale beyond one machine), and 2) utilize long-polling / websockets / async / etc on each page the user visits to push an update when the job has completed. – redmallard Oct 29 '13 at 01:26
  • this is sounding closer to the solution. if we walk through the steps: client browser submits job to server, server stores job state in distributed DB or cache, then client sends an ajax request from every page visited to request the response from the long running job ? So the original request would not need to be an ajax request but could be a simple post, and hte sub-sequent requests would be ajax ? – phil.e.b Oct 29 '13 at 01:31
  • That's pretty much the right idea, you could replace AJAX w/ 'polling', 'long polling', 'websockets', or any other method used to receive updates from the server. The original request wouldn't necessarily have to be an ajax request. – redmallard Oct 29 '13 at 01:48
  • i ended up implementing the solution in the blog link you provided. the example was very easy to follow and provided what i needed to do pretty much. thanks. – phil.e.b Nov 01 '13 at 00:03
1

I would look into a push framework that can push changes from the server to the client. One to consider is SignalR if you're using asp.net.

http://signalr.net/

There are also other long polling techniques that you can use for this.

Some more info here What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135
  • could you please elaborate on how to use other long polling techniques to get the response back n a different page ? thanks. – phil.e.b Oct 28 '13 at 23:54
  • It's hard to fully explain this concept in a SO answer, but the general idea is to maintain a persistent connection between the client and the server in order to push changes to the client from the server. I would do some research around long polling and push frameworks. – TGH Oct 28 '13 at 23:57
0

the simples way of doing this is submitting the ajax request and then sending subsequent ajax requests to poll for it at an interval (say every 2 seconds).

A (much) better way to do this is using websockets - most notably if you're using nodejs have a look at http://socket.io . With sockets you have persistent connections and you can just assign an ID to each user. That way you queue your jobs on your server and when their ready you can just look up the respective socket and send the answer back. Furthermore, with this you can also cancel the job is you see that the user has disconnected completely so you don't waste server resources

Stefan
  • 3,962
  • 4
  • 34
  • 39