danog/ipc

IPC component for Amp.

1.0.0 2023-02-14 13:19 UTC

This package is auto-updated.

Last update: 2024-12-04 17:41:09 UTC


README

Continuous Integration License

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();