amphp / react-adapter
Adapter to make any ReactPHP library compatible with Amp.
Fund package maintenance!
amphp
Installs: 164 294
Dependents: 8
Suggesters: 0
Security: 0
Stars: 24
Watchers: 7
Forks: 5
Open Issues: 2
Requires
- amphp/amp: ^2
- react/event-loop: ^0.5 || ^1
Requires (Dev)
- amphp/php-cs-fixer-config: dev-master
- phpunit/phpunit: ^7 || ^6.4
This package is auto-updated.
Last update: 2024-11-07 22:17:57 UTC
README
amphp/react-adapter
makes any ReactPHP library compatible with Amp v2.
Note If you're using AMPHP v3, have a look at
revolt/event-loop-adapter-react
instead.
Installation
This package can be installed as a Composer dependency.
composer require amphp/react-adapter
Usage
Everywhere where a ReactPHP library requires an instance of LoopInterface
, you pass ReactAdapter::get()
to run the ReactPHP library on Amp's event loop.
<?php require 'vendor/autoload.php'; use Amp\Loop; use Amp\ReactAdapter\ReactAdapter; Loop::run(function () { $app = function ($request, $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("Hello World\n"); }; $socket = new React\Socket\Server(ReactAdapter::get()); $http = new React\Http\Server($socket, ReactAdapter::get()); $http->on('request', $app); echo "Server running at http://127.0.0.1:1337\n"; $socket->listen(1337); });
You can also use the adapter to run ReactPHP apps on an Amp event loop implementation without relying on Amp's global event loop.
$loop = new Amp\ReactAdapter\ReactAdapter((new Amp\Loop\DriverFactory)->create());