Skip to content

Commit 098fca5

Browse files
authored
Merge pull request #19 from roadrunner-php/stream-docs
Add documentation about streaming response
2 parents ddd6432 + 4fcf592 commit 098fca5

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

README.md

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,37 @@ with much greater performance and flexibility.
2222
<a href="https://roadrunner.dev/docs"><b>Documentation</b></a>
2323
</p>
2424

25-
Repository:
26-
--------
25+
26+
## Repository:
2727

2828
This repository contains the codebase PSR-7 PHP workers.
2929
Check [spiral/roadrunner](https://github.com/spiral/roadrunner) to get application server.
3030

31-
Installation:
32-
--------
31+
32+
## Installation:
3333

3434
To install application server and HTTP codebase:
3535

3636
```bash
37-
$ composer require spiral/roadrunner-http nyholm/psr7
37+
composer require spiral/roadrunner-http nyholm/psr7
3838
```
3939

4040
You can use the convenient installer to download the latest available compatible version of RoadRunner assembly:
4141

4242
```bash
43-
$ composer require spiral/roadrunner-cli --dev
43+
composer require spiral/roadrunner-cli --dev
4444
```
4545

4646
To download latest version of application server:
4747

4848
```bash
49-
$ vendor/bin/rr get
49+
vendor/bin/rr get
5050
```
5151

52-
> You can use any [PSR-17 compatible implementation](https://github.com/search?q=psr-17).
52+
> You can use any [PSR-17 compatible implementation](https://packagist.org/providers/psr/http-factory-implementation).
53+
5354

54-
Example:
55-
-------
55+
## Example:
5656

5757
To init abstract RoadRunner worker:
5858

@@ -88,9 +88,9 @@ while (true) {
8888
$request = $psr7->waitRequest();
8989
} catch (\Throwable $e) {
9090
// Although the PSR-17 specification clearly states that there can be
91-
// no exceptions when creating a request, however, some implementations
92-
// may violate this rule. Therefore, it is recommended to process the
93-
// incoming request for errors.
91+
// no exceptions when creating a request, however, some implementations
92+
// may violate this rule. Therefore, it is recommended to process the
93+
// incoming request for errors.
9494
//
9595
// Send "Bad Request" response.
9696
$psr7->respond(new Response(400));
@@ -99,38 +99,61 @@ while (true) {
9999

100100
try {
101101
// Here is where the call to your application code will be located.
102-
// For example:
103-
//
102+
// For example:
103+
//
104104
// $response = $app->send($request);
105105
//
106106
// Reply by the 200 OK response
107107
$psr7->respond(new Response(200, [], 'Hello RoadRunner!'));
108108
} catch (\Throwable $e) {
109109
// In case of any exceptions in the application code, you should handle
110-
// them and inform the client about the presence of a server error.
111-
//
110+
// them and inform the client about the presence of a server error.
111+
//
112112
// Reply by the 500 Internal Server Error response
113113
$psr7->respond(new Response(500, [], 'Something Went Wrong!'));
114-
115-
// Additionally, we can inform the RoadRunner that the processing
116-
// of the request failed.
114+
115+
// Additionally, we can inform the RoadRunner that the processing
116+
// of the request failed.
117117
$worker->error((string)$e);
118118
}
119119
}
120120
```
121121

122-
<a href="https://spiral.dev/">
123-
<img src="https://user-images.githubusercontent.com/773481/220979012-e67b74b5-3db1-41b7-bdb0-8a042587dedc.jpg" alt="try Spiral Framework" />
124-
</a>
122+
### Stream response
123+
124+
To send a response in a stream, set the `$chunkSize` property in `PSR7Worker`:
125+
126+
```php
127+
$psr7 = new PSR7Worker($worker, $factory, $factory, $factory);
128+
$psr7->chunkSize = 512 * 1024; // 512KB
129+
```
130+
131+
Now PSR7Worker will cut the response into chunks of 512KB and send them to the stream.
132+
133+
### Early hints
134+
135+
To send multiple responses you may use the `\Spiral\RoadRunner\Http\HttpWorker::respond()` method with
136+
the `endOfStream` parameter set to `false`. This will send the response to the client and allow you to send
137+
additional responses.
138+
139+
```php
140+
/** @var \Spiral\RoadRunner\Http\PSR7Worker $psr7 */
141+
$httpWorker = $psr7->getHttpWorker()
142+
->respond(103, header: ['Link' => ['</style.css>; rel=preload; as=style']], endOfStream: false);
143+
144+
// End of stream will be sent automatically after PSR7Worker::respond() call
145+
$psr7->respond(new Response(200, [], 'Hello RoadRunner!'));
146+
```
147+
148+
149+
[![try Spiral Framework](https://user-images.githubusercontent.com/773481/220979012-e67b74b5-3db1-41b7-bdb0-8a042587dedc.jpg)](https://spiral.dev/)
125150

126-
Testing:
127-
--------
151+
## Testing:
128152

129-
This codebase is automatically tested via host repository - [spiral/roadrunner](https://github.com/spiral/roadrunner).
153+
This codebase is automatically tested via host repository - [spiral/roadrunner](https://github.com/roadrunner-server/roadrunner).
130154

131155

132-
License:
133-
--------
156+
## License:
134157

135158
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information. Maintained
136159
by [Spiral Scout](https://spiralscout.com).

0 commit comments

Comments
 (0)