Skip to content

Commit dd68757

Browse files
committed
Create the React adapter documentation
1 parent 9f87c4c commit dd68757

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

clients/react-adapter.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
11
React Adapter
22
=============
3+
4+
An HTTPlug adapter for the `React Http client`_.
5+
6+
Installation
7+
------------
8+
9+
To install the React adapter, which will also install React itself (if it was
10+
not yet included in your project), run:
11+
12+
.. code-block:: bash
13+
14+
$ composer require php-http/react-adapter
15+
16+
Usage
17+
-----
18+
19+
The React client adapter needs a :ref:`message factory <message-factory>` in
20+
order to to work::
21+
22+
use Http\Adapter\React\Client;
23+
24+
$client = new Client($messageFactory);
25+
26+
For simplicity, all the required objects are instantiated automatically if not
27+
explicitly specified:
28+
29+
:React\EventLoop\LoopInterface: The event loop used inside the React engine.
30+
:React\HttpClient\Client: The HTTP client instance that must be adapted.
31+
32+
If you need more control on the React instances, you can inject them during
33+
initialization::
34+
35+
use Http\Adapter\React\Client;
36+
37+
$eventLoop = React\EventLoop\Factory::create();
38+
$dnsResolverFactory = new React\Dns\Resolver\Factory();
39+
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
40+
41+
$factory = new React\HttpClient\Factory();
42+
$reactHttp = $factory->create($loop, $dnsResolver);
43+
44+
$adapter = new Client($messageFactory, $eventLoop, $reactHttp);
45+
46+
If you choose to inject a custom React HTTP client, you must inject the loop
47+
used during its construction. But if you already use an EventLoop inside your
48+
code, you can inject only this object.
49+
50+
For more simplicity, there is a ``ReactFactory`` which can be used to initialize
51+
React instances::
52+
53+
use Http\Adapter\React\ReactFactory;
54+
55+
$eventLoop = ReactFactory::buildEventLoop();
56+
$reactHttp = ReactFactory::buildHttpClient($eventLoop);
57+
58+
Then you can use the adapter to send synchronous requests::
59+
60+
use GuzzleHttp\Psr7\Request;
61+
62+
$request = new Request('GET', 'http://httpbin.org');
63+
64+
// Returns a Psr\Http\Message\ResponseInterface
65+
$response = $adapter->sendRequest($request);
66+
67+
Or send asynchronous ones::
68+
69+
use GuzzleHttp\Psr7\Request;
70+
71+
$request = new Request('GET', 'http://httpbin.org');
72+
73+
// Returns a Http\Promise\Promise
74+
$promise = $adapter->sendAsyncRequest(request);
75+
76+
.. include:: includes/further-reading-async.inc
77+
78+
.. _React HTTP client: https://github.com/reactphp/http-client

0 commit comments

Comments
 (0)