|
| 1 | +# Streaming |
| 2 | + |
| 3 | +This guide gives an overview of how to implement streaming requests and responses. |
| 4 | + |
| 5 | +## Independent Uni-directional Streaming |
| 6 | + |
| 7 | +The request and response body work independently of each other can stream data in both directions. {ruby Protocol::HTTP::Body::Stream} provides an interface to merge these independent streams into an IO-like interface. |
| 8 | + |
| 9 | +```ruby |
| 10 | +#!/usr/bin/env ruby |
| 11 | + |
| 12 | +require 'async' |
| 13 | +require 'async/http/client' |
| 14 | +require 'async/http/server' |
| 15 | +require 'async/http/endpoint' |
| 16 | + |
| 17 | +require 'protocol/http/body/stream' |
| 18 | +require 'protocol/http/body/writable' |
| 19 | + |
| 20 | +endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000') |
| 21 | + |
| 22 | +Async do |
| 23 | + server = Async::HTTP::Server.for(endpoint) do |request| |
| 24 | + output = Protocol::HTTP::Body::Writable.new |
| 25 | + stream = Protocol::HTTP::Body::Stream.new(request.body, output) |
| 26 | + |
| 27 | + Async do |
| 28 | + # Simple echo server: |
| 29 | + while chunk = stream.readpartial(1024) |
| 30 | + stream.write(chunk) |
| 31 | + end |
| 32 | + rescue EOFError |
| 33 | + # Ignore EOF errors. |
| 34 | + ensure |
| 35 | + stream.close |
| 36 | + end |
| 37 | + |
| 38 | + Protocol::HTTP::Response[200, {}, output] |
| 39 | + end |
| 40 | + |
| 41 | + server_task = Async{server.run} |
| 42 | + |
| 43 | + client = Async::HTTP::Client.new(endpoint) |
| 44 | + |
| 45 | + input = Protocol::HTTP::Body::Writable.new |
| 46 | + response = client.get("/", body: input) |
| 47 | + |
| 48 | + begin |
| 49 | + stream = Protocol::HTTP::Body::Stream.new(response.body, input) |
| 50 | + |
| 51 | + stream.write("Hello, ") |
| 52 | + stream.write("World!") |
| 53 | + stream.close_write |
| 54 | + |
| 55 | + while chunk = stream.readpartial(1024) |
| 56 | + puts chunk |
| 57 | + end |
| 58 | + rescue EOFError |
| 59 | + # Ignore EOF errors. |
| 60 | + ensure |
| 61 | + stream.close |
| 62 | + end |
| 63 | +ensure |
| 64 | + server_task.stop |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +This approach works quite well, especially when the input and output bodies are independently compressed, decompressed, or chunked. However, some protocols, notably, WebSockets operate on the raw connection and don't require this level of abstraction. |
| 69 | + |
| 70 | +## Bi-directional Streaming |
| 71 | + |
| 72 | +While WebSockets can work on the above streaming interface, it's a bit more convenient to use the streaming interface directly, which gives raw access to the underlying stream where possible. |
| 73 | + |
| 74 | +```ruby |
| 75 | +#!/usr/bin/env ruby |
| 76 | + |
| 77 | +require 'async' |
| 78 | +require 'async/http/client' |
| 79 | +require 'async/http/server' |
| 80 | +require 'async/http/endpoint' |
| 81 | + |
| 82 | +require 'protocol/http/body/stream' |
| 83 | +require 'protocol/http/body/writable' |
| 84 | + |
| 85 | +endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000') |
| 86 | + |
| 87 | +Async do |
| 88 | + server = Async::HTTP::Server.for(endpoint) do |request| |
| 89 | + streamable = Protocol::HTTP::Body::Streamable. |
| 90 | + output = Protocol::HTTP::Body::Writable.new |
| 91 | + stream = Protocol::HTTP::Body::Stream.new(request.body, output) |
| 92 | + |
| 93 | + Async do |
| 94 | + # Simple echo server: |
| 95 | + while chunk = stream.readpartial(1024) |
| 96 | + stream.write(chunk) |
| 97 | + end |
| 98 | + rescue EOFError |
| 99 | + # Ignore EOF errors. |
| 100 | + ensure |
| 101 | + stream.close |
| 102 | + end |
| 103 | + |
| 104 | + Protocol::HTTP::Response[200, {}, output] |
| 105 | + end |
| 106 | + |
| 107 | + server_task = Async{server.run} |
| 108 | + |
| 109 | + client = Async::HTTP::Client.new(endpoint) |
| 110 | + |
| 111 | + input = Protocol::HTTP::Body::Writable.new |
| 112 | + response = client.get("/", body: input) |
| 113 | + |
| 114 | + begin |
| 115 | + stream = Protocol::HTTP::Body::Stream.new(response.body, input) |
| 116 | + |
| 117 | + stream.write("Hello, ") |
| 118 | + stream.write("World!") |
| 119 | + stream.close_write |
| 120 | + |
| 121 | + while chunk = stream.readpartial(1024) |
| 122 | + puts chunk |
| 123 | + end |
| 124 | + rescue EOFError |
| 125 | + # Ignore EOF errors. |
| 126 | + ensure |
| 127 | + stream.close |
| 128 | + end |
| 129 | +ensure |
| 130 | + server_task.stop |
| 131 | +end |
0 commit comments