danog / ipc
IPC component for Amp.
Installs: 502 836
Dependents: 5
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 5
Open Issues: 0
Requires
- php: >=8.1
- amphp/byte-stream: ^2
- amphp/parser: ^1
Requires (Dev)
- amphp/amp: v3.x-dev
- amphp/parallel: v2.x-dev
- amphp/php-cs-fixer-config: v2.x-dev
- amphp/phpunit-util: v3.x-dev
- phpunit/phpunit: ^9
- psalm/phar: ^5
README
danog/ipc
provides an async IPC server.
Installation
composer require danog/ipc
Example
Server:
<?php declare(strict_types=1); require 'vendor/autoload.php'; use Amp\Ipc\Sync\ChannelledSocket; use function Amp\async; use function Amp\Ipc\listen; $clientHandler = function (ChannelledSocket $socket) { echo "Accepted connection".PHP_EOL; while ($payload = $socket->receive()) { echo "Received $payload".PHP_EOL; if ($payload === 'ping') { $socket->send('pong'); $socket->disconnect(); } } echo "Closed connection".PHP_EOL."==========".PHP_EOL; }; $server = listen(sys_get_temp_dir().'/test'); while ($socket = $server->accept()) { async($clientHandler, $socket); }
Client:
<?php declare(strict_types=1); require 'vendor/autoload.php'; use Amp\Ipc\Sync\ChannelledSocket; use function Amp\async; use function Amp\Ipc\connect; $clientHandler = function (ChannelledSocket $socket) { echo "Created connection.".PHP_EOL; while ($payload = $socket->receive()) { echo "Received $payload".PHP_EOL; } echo "Closed connection".PHP_EOL; }; $channel = connect(sys_get_temp_dir().'/test'); $thread = async($clientHandler, $channel); $channel->send('ping'); $thread->await();