Skip to content

Commit

Permalink
Merge pull request #31 from clue-labs/stream
Browse files Browse the repository at this point in the history
Streaming API
  • Loading branch information
clue committed Jun 13, 2015
2 parents afd06d8 + 32ca0b6 commit c69642b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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)
Expand Down
4 changes: 3 additions & 1 deletion src/Io/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -122,6 +122,8 @@ public function send(Request $request)
));
}
});

$deferred->progress(array('responseStream' => $response, 'requestStream' => $requestStream));
});

$requestStream->end((string)$body);
Expand Down

0 comments on commit c69642b

Please sign in to comment.