Skip to content

Commit af2fd53

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

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

clients/react-adapter.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,77 @@
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 built internally by default:
27+
28+
:React\EventLoop\LoopInterface: The event loop used inside the React engine.
29+
:React\HttpClient\Client: The Http client instance that must be adapted.
30+
31+
If you need more control on the React instances, you can inject them during
32+
initialization::
33+
34+
use Http\Adapter\React\Client;
35+
36+
$eventLoop = React\EventLoop\Factory::create();
37+
$dnsResolverFactory = new React\Dns\Resolver\Factory();
38+
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
39+
40+
$factory = new React\HttpClient\Factory();
41+
$reactHttp = $factory->create($loop, $dnsResolver);
42+
43+
$adapter = new Client($messageFactory, $eventLoop, $reactHttp);
44+
45+
If you choose to inject a custom React Http client, you must inject the loop
46+
used during its construction. But if you already use an EventLoop inside your
47+
code, you can inject only this object.
48+
49+
For more simplicity, there is a ``ReactFactory`` which can be used to initialize
50+
React instances::
51+
52+
use Http\Adapter\React\ReactFactory;
53+
54+
$eventLoop = ReactFactory::buildEventLoop();
55+
$reactHttp = ReactFactory::buildHttpClient($eventLoop);
56+
57+
Then you can use the adapter to send synchronous requests::
58+
59+
use GuzzleHttp\Psr7\Request;
60+
61+
$request = new Request('GET', 'http://httpbin.org');
62+
63+
// Returns a Psr\Http\Message\ResponseInterface
64+
$response = $adapter->sendRequest($request);
65+
66+
Or send asynchronous ones::
67+
68+
use GuzzleHttp\Psr7\Request;
69+
70+
$request = new Request('GET', 'http://httpbin.org');
71+
72+
// Returns a Http\Promise\Promise
73+
$promise = $adapter->sendAsyncRequest(request);
74+
75+
.. include:: includes/further-reading-async.inc
76+
77+
.. _React HTTP client: https://github.com/reactphp/http-client

0 commit comments

Comments
 (0)