We are trying to stream large data as JSON in Rails 3.2. Rails out of the box streaming works only for partials and hence we decided to generate JSON as text. As suggested in other posts, I created a Stream class with each method and did a "yield" for every object.
Unicorn server is not sending any response for 3 mins and then sends all the data back.
Does streaming working in Unicorn server?
How do I force flush the output?
We are hosting this server behind ELB and the request times out after 60 seconds.
Controller Method
def listings
self.response_body = Stream.new Listing
end
Stream class
class Stream
def initialize collection
@collection = collection
end
def each
size = @collection.count
index = 0
yield "["
@collection.find_each do |obj|
yield (size - 1 == index) ? obj.to_json : "#{obj.to_json},"
index += 1
end
yield "]"
end
end