PHP code example of hisorange / posix-rpc

1. Go to this page and download the library: Download hisorange/posix-rpc 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/ */

    

hisorange / posix-rpc example snippets


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

$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]);

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

// Would be funny, right?
$worker->respond->calculator->sum('array_sum');

// Other machine.
$six = $main->request->calculator->sum([1, 2, 3]);