Forum: Ruby on Rails Streaming in Rails 3.2

Posted by Ravi Laudya (Guest)
on 2012-12-09 14:29
(Received via mailing list)
Hello all,

I am writing an app to stream log files (or any files) on the browser as 
they grow. The idea is that the rails app should do a 'tail -f' on the 
file and keep sending the data to the browser. I was thinking if there 
was any way to achieve this in rails.

Through googling, I came across the 'streaming' support in rails. But, I 
didn't get it working. Here is what I tried

class LogsController < ApplicationController

  def index
    headers['X-Accel-Buffering'] = 'no'
    self.status = 200
    self.response_body = Streamer.new
  end

end

class Streamer
  def each
    100000.times do |n|
      yield "Streaming...#{n} "
      if (n % 1000 == 0)
        sleep 1
      end
    end
  end
end

The browser time outs waiting for the data.

Can you please let me know how can I achieve this functionality in 
rails?


thanks
ravi
Posted by Norbert Melzer (Guest)
on 2012-12-09 15:28
(Received via mailing list)
I the client poll every few seconds via AJAX. There is no other way.
Am 09.12.2012 14:28 schrieb "Ravi Laudya" <mouryaravi@gmail.com>:
Posted by Hassan Schroeder (Guest)
on 2012-12-09 17:43
(Received via mailing list)
On Sat, Dec 8, 2012 at 6:49 AM, Ravi Laudya <mouryaravi@gmail.com> 
wrote:

> I am writing an app to stream log files (or any files) on the browser as they 
grow. The idea is that the rails app should do a 'tail -f' on the file and keep 
sending the data to the browser. I was thinking if there was any way to achieve 
this in rails.

You might want to look into "WebSockets", which allow keeping a
connection open between client and server.

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Posted by Bill Walton (Guest)
on 2012-12-09 18:50
(Received via mailing list)
The other way is to couple your app with either Faye or Juggernaut.
Posted by Ravi Laudya (Guest)
on 2012-12-11 08:40
(Received via mailing list)
Thanks all for your responses. I am looking to use Web sockets.


thanks
ravi
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.