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.