streaming ajax?

69 views
Skip to first unread message

Simon

unread,
Aug 17, 2011, 10:34:22 PM8/17/11
to rRook
Hi All,

First thanks Jeff for the awesome rook package. The detailed
examples in the package are very useful for mortals.

I wonder whether it is possible to have an example implementing the
streaming ajax, or reverse ajax, with rook. It would be cool to
click some button and see the rook server spit out messages and graphs
continuously if the R calculating takes long time.

I figured out how to get part of the web page updated by jQuery via
the example codes. But only after the R finished all of the
calculating.

Regards,
Simon

Simon

unread,
Sep 13, 2011, 2:48:36 AM9/13/11
to rRook
I have figured out that the easiest way is to use jQuery and
setInterval while make the R function RefClass. Every time the
function is called, it will resume working a bit, save the status and
send out response.

So, with Rook and RefClass, we can do cool things now. Such as a
simulated stock market.

Simon

Aleksandar Blagotić

unread,
Sep 13, 2011, 4:01:15 AM9/13/11
to rr...@googlegroups.com
So you're actually talking about an infinite loop that triggers AJAX request after given amount of time, right?
--
aL3xa

Simon

unread,
Sep 13, 2011, 4:51:10 AM9/13/11
to rr...@googlegroups.com
Actually, a button is setup to start/pause the loop.   Something like:

var updateInterval = 30;
$('#onoff').click( function(){
if ($(this).val()=='Start') {
$(this).val('Pause');
updateChartHq();
timer = setInterval(updateChartHq,updateInterval);
} else {
$(this).val('Start');
clearInterval(timer);
}
});



2011/9/13 Aleksandar Blagotić <aca.bl...@gmail.com>

Aleksandar Blagotić

unread,
Sep 13, 2011, 5:07:16 AM9/13/11
to rr...@googlegroups.com
OK, but underneath, it's just a good ol' infinite loop with setInterval =D
--
aL3xa

Simon

unread,
Sep 13, 2011, 5:22:10 AM9/13/11
to rr...@googlegroups.com
Yes.  It seems that Rook utilize the built-in http service of R, which may not support other advanced streaming ajax techniques.    
There is a surprise that while answering quickly to an ajax call every 30 milliseconds, R still does a 10000 times loop calculating quite well.   I thought R is single-threaded.   Anyway, Rook is much powerful than I thought.


2011/9/13 Aleksandar Blagotić <aca.bl...@gmail.com>

Michele

unread,
Oct 30, 2013, 12:11:14 PM10/30/13
to rr...@googlegroups.com
Yes it is. And to improve it's capabilities I configure server sockets using svSocket.

Example: When I make an AJAX to a very long script I use to dispatch the job to the socket server, like

<%

  library(rjson)
  evalServer(con, "source(System_Calculator.R');NULL")
  
%>

<%= toJSON(list(result="OK")) %>

The above script takes ages, but the all the job is done by the socket and the r session I use to serve the page can keep doing just that smoothly.

I tried to implement server side event as well, but without success. Data are buffered and R hangs in a infinite while loop and no data is sent. So I use a "normal" AJAX either, inside a loop to retrieve the steps of the calculation already finished and create a progress bar for the above script.
Reply all
Reply to author
Forward
0 new messages