innmind/ipc

Inter-process communication

4.4.0 2024-03-10 14:33 UTC

This package is auto-updated.

Last update: 2024-11-10 18:55:17 UTC


README

Build Status codecov Type Coverage

Library to abstract the communication between processes by using message passing over unix sockets.

Installation

composer require innmind/ipc

Usage

# Process A
use Innmind\IPC\{
    Factory as IPC,
    Process\Name,
};
use Innmind\OperatingSystem\Factory;

$ipc = IPC::build(Factory::build());
$counter = $ipc->listen(Name::of('a'))(0, static function($message, $continuation, $counter): void {
    if ($counter === 42) {
        return $continuation->stop($counter);
    }

    return $continuation->respond($counter + 1, $message);
})->match(
    static fn($counter) => $counter,
    static fn() => throw new \RuntimeException('Unable to start the server'),
);
// $counter will always be 42 in this case
# Process B
use Innmind\IPC\{
    Factory as IPC,
    Process\Name,
    Message\Generic as Message,
};
use Innmind\OperatingSystem\Factory;
use Innmind\Immutable\Sequence;

$ipc = IPC::build(Factory::build());
$server = Name::of('a');
$ipc
    ->wait(Name::of('a'))
    ->flatMap(fn($process) => $process->send(Sequence::of(
        Message::of('text/plain', 'hello world'),
    )))
    ->flatMap(fn($process) => $process->wait())
    ->match(
        static fn($message) => print('server responded '.$message->content()->toString()),
        static fn() => print('no response from the server'),
    );

The above example will result in the output server responded hello world in the process B.

You can run the process B 42 times before the server in the process A stops.