Description
Expected behavior vs actual behavior
Consider a simple streaming example like this:
class StreamingTestController < ApplicationController
include ActionController::Live
def index
# This header doesn't seem to be necessary, but it's probably good to set it.
response.headers['Content-Type'] = 'text/event-stream'
# These two headers are requried to work around a mismatch of expectations between
# rack 2.2.x and rails. https://github.com/rack/rack/issues/1619
response.headers['Last-Modified'] = Time.now.httpdate
response.headers['ETag'] = '0'
5.times do
response.stream.write "hello world\n"
sleep 1
end
ensure
response.stream.close
end
end
When you curl
that endpoint you should see one instance of hello world
output immediately, then a one second pause, and then another, and so on until you see five instances.
curl http://localhost:3001/streaming_test/index
hello world
hello world
hello world
hello world
hello world
When a version of active_model_serializers
from the 0.9.x
branch is loaded, instead of getting a streaming response you'll see nothing for about 5 seconds, then you'll see 5 instances of hello world
output all at once.
With the 0.10.x
branch the problem no longer happens.
I'm opening this issue mostly for documentation purposes so that if someone else discovers that streaming isn't working in their app they might stumble across this issue and save some time.
Steps to reproduce
- Generate a new rails app
- Make a streaming controller like the one above
curl
it to see that it streams- Add
gem "active_model_serializers", "0.9.8"
to theGemfile
bundle install
curl
the endpoint again to see that streaming is no longer working
Environment
ActiveModelSerializers Version: 0.9.3
& 0.9.8
Output of ruby -e "puts RUBY_DESCRIPTION"
: ruby 2.6.8p205 (2021-07-07 revision 67951) [x86_64-darwin20]
OS Type & Version: macOS 12.3
Integrated application and version (e.g., Rails, Grape, etc): rails 5.2.8
Backtrace
None
Additonal helpful information
Here's a repo that demonstrates the problem with the 0.9.x
line. The README
describes how to alter the Gemfile
to see that the 0.10.x
line fixes the issue.