PHP code example of innmind / ipc

1. Go to this page and download the library: Download innmind/ipc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innmind / ipc example snippets


# 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'),
    );