diff --git a/clients/react-adapter.rst b/clients/react-adapter.rst index 4a58071..4f69c29 100644 --- a/clients/react-adapter.rst +++ b/clients/react-adapter.rst @@ -20,43 +20,31 @@ dependency. Usage ----- -The React client adapter needs a :ref:`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:: @@ -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