Skip to content

Update react adapter to v4 #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions clients/react-adapter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,31 @@ dependency.
Usage
-----

The React client adapter needs a :ref:`message factory <message-factory>` in
order to to work::

use Http\Adapter\React\Client;

$client = new Client($messageFactory);

For simplicity, all required objects are instantiated automatically if not
explicitly specified:

:React\EventLoop\LoopInterface: The event loop used inside the React engine.
:React\HttpClient\Client: The HTTP client instance that must be adapted.

If you need more control on the React instances, you can inject them during
If you need control on the React instances, you can inject them during
initialization::

use Http\Adapter\React\Client;

$eventLoop = React\EventLoop\Factory::create();
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$systemDnsConfig = React\Dns\Config\Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers[] = '8.8.8.8';
}

$factory = new React\HttpClient\Factory();
$reactHttp = $factory->create($loop, $dnsResolver);
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $factory->create($config);

$adapter = new Client($messageFactory, $eventLoop, $reactHttp);
$connector = new React\Socket\Connector([
'dns' => $dnsResolver,
]);
$browser = new React\Http\Browser($connector);

If you choose to inject a custom React HTTP client, you must inject the loop
used during its construction. But if you already use an EventLoop inside your
code, you can inject only this object.
$adapter = new Client($browser);

You can also use a ``ReactFactory`` in order to initialize React instances::

use Http\Adapter\React\ReactFactory;

$eventLoop = ReactFactory::buildEventLoop();
$reactHttp = ReactFactory::buildHttpClient($eventLoop);
$reactHttp = ReactFactory::buildHttpClient();

Then you can use the adapter to send synchronous requests::

Expand All @@ -76,6 +64,18 @@ Or send asynchronous ones::
// Returns a Http\Promise\Promise
$promise = $adapter->sendAsyncRequest(request);

Note that since v4 calling `wait` on `Http\Promise\Promise` is expected to run inside a fiber::

use function React\Async\async;

async(static function () {
// Returns a Http\Promise\Promise
$promise = $adapter->sendAsyncRequest(request);

// Returns a Psr\Http\Message\ResponseInterface
$response = $promise->wait();
})();

.. include:: includes/further-reading-async.inc

.. _React HTTP client: https://github.com/reactphp/http-client