PosixRPC for PHP by hisorange
Easy to use library to handle the IPC (Inter Process Communication) for You! It provides a messaging interface for publish / subscribe pattern, and also supports safe RPC (Remote Procedure Calls). That is not all! With an intuitive syntax You can call handlers synchronously or asynchronously with a non-blocking transport layer.
But wait, isn't Posix queues are small and slow?! Nope! This library can push gigabyte sized messages through the queue, and can easily reach 30k messages per second ^.^
Let's get started!
composer require hisorange/posix-rpc
Yep, it's ready to be used by You! ^.^
No need for big setup, just initialize a node with a unique name:
use hisorange\PosixRPC\Node;
use hisorange\PosixRPC\Contract\IMessage;
$main = new Node('main');
$worker = new Node('worker');
// Do the "hard" job here ^.^
$worker->respond->sum(function(IMessage $message) {
return array_sum($message->getPayload());
});
// Yep, You can scale your app this easily!
$twelve = $main->request->sum([10, 2]);
// Also, You can simply publish messages which does not expect an answear!
$main->publish('log.error', 'Database connection refused!');
// And listen on them in Your lazy process
$worker->subscribe('log.error', function(IMessage $message) {
take_action($message->getPayload());
});
By default every message sent async, and You could pool the transport for messages. But if You wana sequence a logic and Your process can block execution while waiting for a message, then You can use the await call, this will pool the transport until the response is arrived.
$worker->respond('sum', function($request) {
return my_array_sum($request);
});
// Will pool the transport until the response is arrived.
$response = $main->request('sum', [10, 2]);
Async syntax
$worker->respond('sum', 'array_sum');
$main->request('sum', [10, 2], function($response) {
assert($response, 12);
});
// Call the pool, when Your process is free to receive messages.
$main->tick();
To make the usage more programer friendly, the package supports fluent syntax.
// Would be funny, right?
$worker->respond->calculator->sum('array_sum');
// Other machine.
$six = $main->request->calculator->sum([1, 2, 3]);