News for hackers. Stuff that matters.
Re:Live Streaming in Rails 4.0 by tenderlove on Monday July 30, 2012 @04:50PM (#4314606)
Yup, IO objects use `each` for reading. I want to write to the socket. I am lamenting the hoops I have to jump through in order to make a reader act like a writer. Somehow data must be fed to the `each` function which means probably a Queue and a Thread, or possibly feeding Rack the read end of a pipe. None of these solutions is very appetizing to me. :)
Re:Live Streaming in Rails 4.0 by randomdata on Monday July 30, 2012 @05:36PM (#4314810)
In sufficiently modern versions of Rails you could write:

    def index
      self.response_body = Enumerator.new do |socket|
        100.times do
          socket << "hello world"
        end
      end
    end
Your API is a little less verbose, I'll give you that, but I don't see why you need Queues and Threads unless the problem actually calls for those things. It still looks like you are writing to a socket either way.
Re:Live Streaming in Rails 4.0 by lloeki on Tuesday July 31, 2012 @04:05AM (#4316204)
That's what I do, encapsulated in a bunch of methods aggregated in a streaming module. Don't forget to set this if you're using nginx (which my module does).

    headers['X-Accel-Buffering'] = 'no'
Re:Live Streaming in Rails 4.0 by ROFISH on Monday July 30, 2012 @06:12PM (#4314956)
Does this solve the problem of Rails' ridiculously high stack? (functions in functions in functions, Rails' stack is daunting...) If you had the ability to write to a buffer, you don't need the stack, just pass the buffer object around.
Re:Live Streaming in Rails 4.0 by masklinn on Monday July 30, 2012 @04:59PM (#4314656)
> I want to write to the socket.

That's what you do when you yield to the each's block. It's not like the caller calls each multiple times when it wants new data, he calls `each` once and you feed it data via the block it provides.

> Somehow data must be fed to the `each` function

Just as it "must be" fed to the `write` in your examples.

Re:Live Streaming in Rails 4.0 by tenderlove on Monday July 30, 2012 @05:13PM (#4314718)
> > I want to write to the socket.

> That's what you do when you yield to the each's block.

No IO object in ruby uses the `each` method for writing.

Re:Live Streaming in Rails 4.0 by masklinn on Tuesday July 31, 2012 @04:32AM (#4316272)
You are mistaken and confused. All IO objects in Ruby use `each` for writing out, non-IO objects use something else to write to IO objects. But from an IO object's perspective, that's a read.

Rack responses are IO objects, so they use #each to write data out. That's perfectly coherent.

Re:Live Streaming in Rails 4.0 by bascule on Tuesday July 31, 2012 @01:25PM (#4318497)
Responding to #each is neither a necessary or sufficient property of Ruby IO objects. You are mistaken and confused.

Don't believe me? Let's undefine #each on the metaclass of an IO object and see what happens. Guess what: it still works!

https://gist.github.com/3218710