From 6bb36b6117c2356f4dcbe02b5431973ee3cad9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 16 Mar 2015 21:04:44 +0100 Subject: [PATCH 1/2] Emit requestStream and responseStream via progress events --- src/Io/Sender.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Io/Sender.php b/src/Io/Sender.php index b1284f5..6fe80ac 100644 --- a/src/Io/Sender.php +++ b/src/Io/Sender.php @@ -102,7 +102,7 @@ public function send(Request $request) $deferred->reject($error); }); - $requestStream->on('response', function (ResponseStream $response) use ($deferred) { + $requestStream->on('response', function (ResponseStream $response) use ($deferred, $requestStream) { $bodyBuffer = ''; $response->on('data', function ($data) use (&$bodyBuffer) { $bodyBuffer .= $data; @@ -122,6 +122,8 @@ public function send(Request $request) )); } }); + + $deferred->progress(array('responseStream' => $response, 'requestStream' => $requestStream)); }); $requestStream->end((string)$body); From 32ca0b6fc6d41442798d883685a191216e25a048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 16 Mar 2015 21:16:15 +0100 Subject: [PATCH 2/2] Documentation for streaming API --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index bf03371..efbf834 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,24 @@ $client = new Browser($loop, $sender); $client->get('http://localhost/demo'); ``` +### Streaming + +Note: This API is subject to change. + +The [`Sender`](#sender) emits a `progress` event array on its `Promise` that can be used +to intercept the underlying outgoing request stream (`React\HttpClient\Request` in the `requestStream` key) +and the incoming response stream (`React\HttpClient\Response` in the `responseStream` key). + +```php +$client->get('http://www.google.com/')->then($handler, null, function ($event) { + if (isset($event['responseStream'])) { + /* @var $stream React\HttpClient\Response */ + $stream = $event['responseStream']; + $stream->on('data', function ($data) { }); + } +}); +``` + ## Install The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)