writing from stdout to the browser?

hi all,

i’m working on a rails app that calls a ruby script which writes to $stdout. i’d like somehow intercept that text and write it in realtime (usuing ajax?) to the web browser. does anyone have general (or specific) hints about how to approach this?

i can’t change the code of the the called script, which is currently called using backticks.

thanks for any help!!

-reid

Well say your script was

#!/usr/bin/env puts "Hello world"

Then output = `./test.rb` will set output to Hello world.

You'll only get it when the program returns. If that's not ok, then you probably want to use IO.popen or similar and then read from the pipe it yields you.

Fred

if i used the IO.popen method, what ajax technique could i use to get the browser to display the collected text as i gather it?

Reid Oda wrote:

if i used the IO.popen method, what ajax technique could i use to get the browser to display the collected text as i gather it?

An iframe may be the only way.

ah yes. that’s what i want. this is my first project using AJAX, so i have a lot to learn.

the text i’ll be loading into the iframe is a log file that sometimes grows as quickly as 1 line per second and other times as slowly 1 line per hour. is there a way that the rails server can tell the browser when a new line is ready to be uploaded, instead of having to check back at regular intervals?

Reid Oda wrote:

ah yes. that's what i want. this is my first project using AJAX, so i have a lot to learn.

the text i'll be loading into the iframe is a log file that sometimes grows as quickly as 1 line per second and other times as slowly 1 line per hour. is there a way that the rails server can tell the browser when a new line is ready to be uploaded, instead of having to check back at regular intervals?

An iframe would have allowed you to simply point the src parameter of the iframe to the stream and let the browser render it progressively as if it was a normal webpage. But unless there is some way to keep the loading alive (e.g. sending null characters), I fear that browsers will time-out at the 1 line/hour rate you want.

So you might have to go to a more complicated AJAX polling method.

Others may be able to suggest a good solution for your circumstances.