Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 23, 2022 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/76365da1b444482561d58d53b002a62a to your computer and use it in GitHub Desktop.
Save havenwood/76365da1b444482561d58d53b002a62a to your computer and use it in GitHub Desktop.
A Rack 3 example of bidirectional streaming with Async HTTP
# frozen_string_literal: true
require 'async/http/internet'
Async do
internet = Async::HTTP::Internet.new
response = internet.get('http://localhost:9292')
stream = response.connection.stream
stream.write('Hello World!')
stream.close_write
puts response.read
end
# frozen_string_literal: true
def headers = {'content-type' => 'text/plain'}
STREAM = -> do
_1.write _1.read
ensure
_1.close
end
run do |_env|
[200, headers, STREAM]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment