Interactive web app

I have an arbitrary program written in any language, and it is a binary
(ie foo.exe), that will print out output to stdout constantly every 3-5
seconds.

Is there a way that I can have a web application designed in rails that
will print the output of this program to the browser every 3-5 seconds
in realtime, preferably even in an ajax manner.

thanks

That’s not a problem. Send the output of the program to a text file
(works on both *nix and windows). Create a rails action that parses the
file and outputs either the last line or the entire file (depending on
what you’re trying to do). Then, create an ajax updater in the view that
updates a div every few seconds with the output of the action.

1)By sending output of program to a text file, you mean modify the
binary right?

I doubt he meant for you to modify the binary. I’m sure he just meant
for you to redirect stdout to a file.

Like (perhaps under a cron job):

./myexecutable.exe > output.txt

or some such

I have 3 concerns:

1)By sending output of program to a text file, you mean modify the
binary right?

2)And if I were to modify the binary and send all outputs to a text file
and not stdout, how do I specifically read just the last lines that were
added, instead of rereading the whole file from the top.

  1. How do I do an update every few seconds using AJAX and how would I
    update it without redisplaying the entire file, but rather just the new
    lines that were added to the file.

thanks

Inge Jørgensen wrote:

That’s not a problem. Send the output of the program to a text file
(works on both *nix and windows). Create a rails action that parses the
file and outputs either the last line or the entire file (depending on
what you’re trying to do). Then, create an ajax updater in the view that
updates a div every few seconds with the output of the action.

User wrote:

I have 3 concerns:

1)By sending output of program to a text file, you mean modify the
binary right?

No. If it outputs to stdout, you could easily redirect it to a file. Try
typing “Foo.exe > myfile.txt”. This overwrites myfiles.txt with the
output from Foo.exe. If you want do append to the file instead of
overwriting, do “Foo.exe >> myfile.txt”. Works exactly the same on *nix

2)And if I were to modify the binary and send all outputs to a text file
and not stdout, how do I specifically read just the last lines that were
added, instead of rereading the whole file from the top.

If you need just the last line, you could read the file into a string,
and do:
filecontents = get_file_contents_somehow()
lastline = filecontents.split( /\n/ ).last

you could create an array of lines with
lines = filecontents.split( /\n/ )

  1. How do I do an update every few seconds using AJAX and how would I
    update it without redisplaying the entire file, but rather just the new
    lines that were added to the file.

Look at the prototype helpers, they’re documented in the rails api.

but how do i redirect the stdout to a file if it is constantly being
written (the program does not end, it will keep writing to stdout )

thanks

Jeff LaMarche wrote:

1)By sending output of program to a text file, you mean modify the
binary right?

I doubt he meant for you to modify the binary. I’m sure he just meant
for you to redirect stdout to a file.

Like (perhaps under a cron job):

./myexecutable.exe > output.txt

or some such

On 7/5/06, User [email protected] wrote:

but how do i redirect the stdout to a file if it is constantly being
written (the program does not end, it will keep writing to stdout )

thanks

I believe the file gets written to while the application is running
(compare with logfiles). Instead of printing it in the console it just
writes to the file. So the file will get updated every couple of
seconds as the program outputs more text.

Mathias.

Mathias W. wrote:

seconds as the program outputs more text.
I think the file is updated for every newline character. This could be
entirely wrong, I have no idea where I pulled that from.

I do not think you can read while a file is being written to. For
example, I ran a program that is in an infinite loop that prints Foo,
sleeps for 10 seconds, then prints Bar. I redirected the output to
log.txt. I opened the txt file each time, and saw nothing on the text
file. When I abnormally closed the program, the text file had all the
output.

Mathias W. wrote:

On 7/5/06, User [email protected] wrote:

but how do i redirect the stdout to a file if it is constantly being
written (the program does not end, it will keep writing to stdout )

thanks

I believe the file gets written to while the application is running
(compare with logfiles). Instead of printing it in the console it just
writes to the file. So the file will get updated every couple of
seconds as the program outputs more text.

Mathias.

nvm, you have to flush the output in the binary you are running for you
to access it. however, I noticed that redirecting output interpreted \n
as a newline instead of just directing \n as a string to the text file.
How do I prevent that?

thanks

User wrote:

I do not think you can read while a file is being written to. For
example, I ran a program that is in an infinite loop that prints Foo,
sleeps for 10 seconds, then prints Bar. I redirected the output to
log.txt. I opened the txt file each time, and saw nothing on the text
file. When I abnormally closed the program, the text file had all the
output.

Mathias W. wrote:

On 7/5/06, User [email protected] wrote:

but how do i redirect the stdout to a file if it is constantly being
written (the program does not end, it will keep writing to stdout )

thanks

I believe the file gets written to while the application is running
(compare with logfiles). Instead of printing it in the console it just
writes to the file. So the file will get updated every couple of
seconds as the program outputs more text.

Mathias.

On 7/6/06, User [email protected] wrote:

but how do i redirect the stdout to a file if it is constantly being
Mathias.
Would a pipe of the binary be more appropriate?
eg foo.exe | my_ruby_script

I’m not much of a *nix user but a small ruby script that responds to
input
via a gets in a loop, and then spits out a bunch of small files with the
text from the binary for a given period of time might work.

while some_global_condition
f = File.open( “some_file_name”, “w” )
until end_time_interval
f << gets
update_time_interval_condition
end
f.close
end

The ajax call could then remember in the session the last file name that
was
read, and when a new one is available, get it and send it to the
browser.

I really don’t know how this would go, and it’s quite late here :wink:

Okay, so I guess my last question is:

when you say update the div, I would have to update everything I last
displayed and then the new display. Is there a way I can just show the
newly outputted lines from the logfile ?

thanks

On Wed, Jul 05, 2006, User wrote:

nvm, you have to flush the output in the binary you are running for you
to access it. however, I noticed that redirecting output interpreted \n
as a newline instead of just directing \n as a string to the text file.
How do I prevent that?

Fairly universally, printing \n is a newline. Perhaps you should be
printing \n instead, if you don’t want newlines.

Ben

On Wed, Jul 05, 2006 at 03:35:25PM +0200, User wrote:

I have an arbitrary program written in any language, and it is a binary
(ie foo.exe), that will print out output to stdout constantly every 3-5
seconds.

Is there a way that I can have a web application designed in rails that
will print the output of this program to the browser every 3-5 seconds
in realtime, preferably even in an ajax manner.

Yes.

What you need is to make sure that the producer flushes it’s output when
it’s finished writing for it’s few seconds (because it doesn’t become
available for you to read until it’s escaped the producer’s buffers),
and
redirect the producer’s output to a named pipe.

As other posters have said, you use a repeating AJAX call in the browser
to
call an action in the webserver, which simply opens the pipe for
non-blocking reads, sucks out all of the available data, and then dumps
it
back to the browser. The AJAX call will probably then append that data
to
the end of the current contents of the div (or replace it, or something

whatever you need your app to do).

The important things to note are:

  • Use a named pipe. If you write to a regular file, you’ll need to keep
    note of where you’re up to in the file, and that’s tricky unless you use
    some seriously creepy shared state (you can’t delete the file because
    the producer’s redirection will still point to the old file handle).

  • This method will only work for a single consumer. If you’ve got
    multiple
    reading it, they’ll each only get some of the output. You can send the
    data
    to multiple writers, but you’ll need to maintain shared storage of the
    producer’s output and remember where each client is up to in the stream.
    Have fun with that one.

  • You must use non-blocking reads. If you open the pipe normally, your
    program will hang waiting for more output, which isn’t so useful when
    you’ve
    got an AJAX call waiting for you.

  • All of this is Unix-like OS centric. If you’re restricted to Windows,
    there’s probably ways of making all this work, but you’re not going to
    find
    out how from me (because I Don’t Do Windows).

  • Matt