Saran
Saran

Reputation: 3884

Can grails controller dynamically return an update to a view before the action finishes?

Current situation

I'm executing an external application (by using Runtime.getRuntime().exec... example here) and I'm listening on the Stream the app writes stdout to. That part is working as intended and I get results (in grails controller), line by line, as they are available and -- when the stream has no more content -- I render the result on the page.

Other aspect is initiation of that application from the gsp page, asynchronously. That is also already covered and the remoteFunction call is made (to the above mentioned controller method), the results are gathered and returned and they are correctly displayed in designated area on the page.


But, what is bugging me and what I actually want to achieve is:

Goal

to display the lines read from the stream as they are read, not after the whole stream finishes (as it is done now).

That means (something like, for example) calling render for each line in the Stream-reading loop:

while ((line = bri.readLine()) != null) {
  System.out.println(line);
  //render line here?
}

Can this be achieved?

Please explain :)

Upvotes: 3

Views: 754

Answers (1)

aiolos
aiolos

Reputation: 4697

I think there is no standard grails way to achieve your desired behavoir.

The g:remoteFunction ist still an tag that wraps the AJAX request for you. The request is bounded to the request response pattern - you can't get multiple responses for one request. Your action code won't be modified automatically.

You have to implement it on your own.

I would try the following:

  • Split your single remoteFunction call into multiple AJAX requests.
  • The first request initializes a new thread which listen to your external application.
  • The results of this application have to be stored in session scope, so that ...
  • the following periodical requests from the client can poll the current state of the execution.

Upvotes: 2

Related Questions