ruby - Rails render/stream IO-like object without buffering -
i have controller needs respond upto maybe 100mb of data getting source on network (so e.g. http, ftp, or custom protocol socket), , trying work out how should render response without considerable latency of writing temp file rendering/sending that.
its important can "close" stream when rails done, can limit number of active connections, or use pools (e.g. because protocols have slow "connect").
passing io directly render not work. render sock
'#<tcpsocket:fd 20>' not activemodel-compatible object for templates saw docs use render stream: true disable buffering, id still need rails accept (and maybe buffer) object anyway.
a possible solution make use of built-in live streaming support in rails actioncontroller::live module:
rails allows stream more files. in fact, can stream in response object.
actioncontroller::livemodule allows create persistent connection browser. using module, able send arbitrary data browser @ specific points in time.
class mycontroller < actioncontroller::base include actioncontroller::live def my_action response.headers["content-type"] = "your/content-type" tcpsocket.open("server", 1234) |socket| # each chunk of data read: response.stream.write data end ensure response.stream.close end end
Comments
Post a Comment