A flexible command bus for ReactPHP
Go to file
2024-03-01 21:21:09 +01:00
examples Implemented notify 2024-03-01 15:20:54 +01:00
src Error handling fixes 2024-03-01 21:21:09 +01:00
.gitignore Initial commit 2024-03-01 14:34:14 +01:00
composer.json Initial commit 2024-03-01 14:34:14 +01:00
phpstan.neon Initial commit 2024-03-01 14:34:14 +01:00
README.md Bugfixes, reconnection in client, readme 2024-03-01 18:19:25 +01:00

Command Bus for ReactPHP

Features

  • Can run monolithic (create a bus and use as is), or distributed (create bus and use clients), or in a hybrid setup. The CommandBus functions identical with or without listeners and clients.
  • All commands called asynchronously using promises and deferreds.
  • Push notifications from the bus to subscribers and listeners, such as progress or log/error messages.

Potential caveats

  • There is no security, nor encryption. This is intended to work between a daemon and a frontend, and is designed primarily considering all activity taking place on localhost. You can use ReactPHPs SecureConnector and SecureServer to encrypt the traffic, but this still allows anyone to connect and interact with the bus. If this sounds like something that should be changed, open a ticket.

Installing

$ composer require noccylabs/react-command-bus:^0.1.0

Usage

// The registry holds the callable commands
$commands = new CommandRegistry();

// Register some function to call. The name here is "hello", and it will
// receive a Context holding the call context. Any passed data will be
// available as properties on the Context object.
$commands->register("hello", function (Context $context) {
    // You don't have to, but you should return a promise from your 
    // commands.
    return new Promise(function (callable $resolve) use ($context) {
        Loop::addTimer(1, function () use ($context, $resolve) {
            $resolve("Hello, {$context->name}");
        });
    });
});

// Create the bus
$bus = new CommandBus($commands);

// And execute some commands!
$bus->execute('hello', ['name'=>'Bob'])->then(
    function ($result) {
        var_dump($result);
    }
);