innmind / ipc
Inter-process communication
5.0.0
2026-03-15 15:44 UTC
Requires
- php: ~8.4
- innmind/foundation: ~2.1
Requires (Dev)
- innmind/black-box: ~6.5
- innmind/coding-standard: ~2.0
- innmind/static-analysis: ~1.3
README
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\{ IPC, Process\Name, Continuation, Server, Message, }; use Innmind\OperatingSystem\Factory; use Innmind\Immutable\Monoid; /** * @psalm-immutable * @implements Monoid<int> */ final class Addition implements Monoid { public function identity(): int { return 0; } public function combine(mixed $a, mixed $b): int { return $a + $b; } } $ipc = IPC::build(Factory::build()); $counter = $ipc ->serve(Name::of('a')) ->sink(new Addition) ->monitor(static fn(int $counter, Server\Continuation $continuation) => match ($counter) { 42 => $continuation->finish(), default => $continuation, }) ->with( static fn(Message $message, Continuation $continuation, int $counter) => $continuation ->respond($message) ->carryWith($counter + 1), ) ->unwrap(); // $counter will always be 42 in this case
# Process B use Innmind\IPC\{ IPC, Process, Process\Name, Message, }; use Innmind\OperatingSystem\Factory; use Innmind\MediaType\{ MediaType, TopLevel, }; use Innmind\Immutable\{ Str, Sequence, }; $ipc = IPC::build(Factory::build()); $server = Name::of('a'); $response = $ipc ->connectTo($server) ->flatMap( static fn(Process $process) => $process ->send(Sequence::of( Message::of( MediaType::from(TopLevel::text, 'plain'), Str::of('hello world'), ), )) ->map(static fn() => $process), ) ->flatMap(fn(Process $process) => $process->wait()) ->match( static fn(Message $message) => 'server responded '.$message->content()->toString(), static fn() => 'no response from the server', ); print($message);
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.