I am trying to write data to the stream at a set interval of time.
class StreamController < ApplicationController
include ActionController::Live
def stream
response.headers['Content-Type'] = 'text/event-stream'
5.times do
response.stream.write "data: hello world\n\n"
sleep 1
end
ensure
response.stream.close
end
end
But when I send a get request (using curl) to the /stream endpoint I get: "data: hello world\n\n" 5 times all at once after 5 seconds, instead of receiving "data: hello world\n\n" once per second. I am using the default puma webserver that comes with rails 7.0.4. My guess is that it buffers the response then sends it when calling response.stream.close.
I tried adding options = { flush_headers: true } to the puma config file but it didn't help.
rackandpumahave been figuring this out and fixing bugs for so long. Officially, you needrack v3andpuma v6, but rails 7 doesn't support rack v3.As far as I can tell the last hickup is
Rack::ETagmiddleware, which you can deleteconfig.middleware.delete Rack::ETag. But you probably need it, so a workaround for now:If you see
ETag: W/"53827b3eddcd709c853e748ffd5314fb", you're doing it wrong:you can't tell, but it was streaming :)
puma v6.3.1rack v2.2.7https://github.com/rack/rack/issues/1619