# Writing from stdout to the browser?

**URL:** <https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763>\
**Category:** Rails\
**Created:** [March 13, 2008, 2:32am UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763 "2008-03-13T02:32:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 13, 2008, 2:32am UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/1 "2008-03-13T02:32:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 13, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/2 "2008-03-13T12:39:24Z")

</div>

On 13 Mar 2008, at 01:31, Reid O. wrote:

> 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.

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

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 18, 2008, 12:48am UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/3 "2008-03-18T00:48:21Z")

</div>

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?

On Thu, Mar 13, 2008 at 4:38 AM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 18, 2008, 5:14am UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/4 "2008-03-18T05:14:04Z")

</div>

Reid O. 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.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 18, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/5 "2008-03-18T16:44:35Z")

</div>

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?

On Mon, Mar 17, 2008 at 9:12 PM, Mark Reginald J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brownerthanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@brownerthanu](https://www.ruby-forum.com/u/brownerthanu)\
**Post date:** [March 19, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/writing-from-stdout-to-the-browser/131763/6 "2008-03-19T12:34:39Z")

</div>

Reid O. 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.

–  
We develop, watch us RoR, in numbers too big to ignore.
