1.4 KiB
1.4 KiB
Command Bus for ReactPHP
- Can run monolithic (create a bus and use as is), or distributed (create bus and use clients), or a hybrid.
- All commands called asynchronously using promises and deferreds.
Installing
$ composer require noccylabs/react-command-bus:^0.1.0
Usage
// This is enough to setup a local bus.
$bus = new CommandBus();
$bus->register('hello', function (Context $context) {
return new Promise(function (callable $resolve) use ($context) {
return $resolve([ 'message' => "Hello, {$context->name}" ]);
});
});
// You can call it as expected
$bus->execute('hello', [ 'name' => "Bob" ])
->then(function (array $result) {
echo "Result: {$result['message']}\n";
});
// Add a listener, and you can now connect to it!
$bus->addServer($server);
// So using this in another script works as expected, if you consider
// the async flow. See the examples for working examples.
$client = new CommandBusClient();
$client->connect($socket);
$client->execute('hello', [ 'name' => "Bob" ])
->then(function (array $result) {
echo "Result: {$result['message']}\n";
});
// The bus can also notify all clients about important events
$bus->notify('updateCompleted', [ 'info' => [] ]);
// Listening on the bus or client will yield the event
$bus->on('notify', function (string $event, array $data) {});
$client->on('notify', function (string $event, array $data) {});