Skip to content

Commit b9e36fb

Browse files
authored
Merge pull request #110 from clue-labs/unix-sockets
Update Socket to support HTTP over Unix domain sockets (UDS)
2 parents be92b9e + b1f13f8 commit b9e36fb

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Event-driven, streaming HTTP client for [ReactPHP](http://reactphp.org)
88

99
* [Basic usage](#basic-usage)
1010
* [Example](#example)
11+
* [Advanced usage](#advanced-usage)
12+
* [Unix domain sockets](#unix-domain-sockets)
1113
* [Install](#install)
1214
* [Tests](#tests)
1315
* [License](#license)
@@ -98,6 +100,31 @@ $loop->run();
98100

99101
See also the [examples](examples).
100102

103+
## Advanced Usage
104+
105+
### Unix domain sockets
106+
107+
By default, this library supports transport over plaintext TCP/IP and secure
108+
TLS connections for the `http://` and `https://` URI schemes respectively.
109+
This library also supports Unix domain sockets (UDS) when explicitly configured.
110+
111+
In order to use a UDS path, you have to explicitly configure the connector to
112+
override the destination URI so that the hostname given in the request URI will
113+
no longer be used to establish the connection:
114+
115+
```php
116+
$connector = new FixedUriConnector(
117+
'unix:///var/run/docker.sock',
118+
new UnixConnector($loop)
119+
);
120+
121+
$client = new Client($loop, $connector);
122+
123+
$request = $client->request('GET', 'http://localhost/info');
124+
```
125+
126+
See also [example #11](examples/11-unix-domain-sockets.php).
127+
101128
## Install
102129

103130
The recommended way to install this library is [through Composer](https://getcomposer.org).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": ">=5.4.0",
88
"guzzlehttp/psr7": "^1.0",
99
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
10-
"react/socket": "^1.0 || ^0.8.3",
10+
"react/socket": "^1.0 || ^0.8.4",
1111
"react/stream": "^1.0 || ^0.7.1",
1212
"react/promise": "~2.2",
1313
"evenement/evenement": "^3.0 || ^2.0"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use React\HttpClient\Client;
4+
use React\HttpClient\Response;
5+
use React\Socket\FixedUriConnector;
6+
use React\Socket\UnixConnector;
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
$loop = React\EventLoop\Factory::create();
11+
12+
$connector = new FixedUriConnector(
13+
'unix:///var/run/docker.sock',
14+
new UnixConnector($loop)
15+
);
16+
17+
$client = new Client($loop, $connector);
18+
19+
$request = $client->request('GET', 'http://localhost/info');
20+
21+
$request->on('response', function (Response $response) {
22+
var_dump($response->getHeaders());
23+
24+
$response->on('data', function ($chunk) {
25+
echo $chunk;
26+
});
27+
28+
$response->on('end', function () {
29+
echo 'DONE' . PHP_EOL;
30+
});
31+
});
32+
33+
$request->on('error', function (\Exception $e) {
34+
echo $e;
35+
});
36+
37+
$request->end();
38+
39+
$loop->run();

0 commit comments

Comments
 (0)