noccylabs/react-bus (0.1.0)
Installation
{
"repositories": [{
"type": "composer",
"url": "https://dev.noccylabs.info/api/packages/noccy/composer"
}
]
}composer require noccylabs/react-bus:0.1.0About this package
React Bus
This is a zero config IPC/RPC bus with automatic failover and dynamic masters.
The only configuration is available through environment variables:
REACT_BUS_ADDRESS- The address to use for the broker, serving and connectingREACT_BUS_REMOTE- Broker address for connecting, implicitly disables serving
Components
noccylabs/react-bus implements the following components:
- PubSub Message Bus - for sending messages between application components
- Command Bus - for exporting callable functions to other application components
- Locking - for shared locks and mutexes
PubSub
The PubSub component lets a client subscribe to message topics. Wildcard patterns
can be used to select the topics to receive. Once a message is published to any of
the subscribed topics, it will appear as a publish event.
For now, all subscriptions are returned through the same event. You have to match the topic yourself to determine the path of action.
$conduit->subscribe("app/events/*");
$conduit->on("publish", function (string $topic, mixed $payload) {
// ...
});
$conduit->publish("app/events/startup", [ 'app' => 'worker42' ]);
Note
Successful subscriptions are tracked locally, and will be re-applied when reconnecting to the broker.
Command Bus
The Command Bus component lets a client expose methods, that can then be called by other connected clients.
$conduit->exportCommand(
"app.maintenance.cleanup.start",
function (string $target, array $options = []) {
// ...
}
);
$conduit->call("app.maintenance.cleanup.start", target: "temp")->then(
function ($result) {
// ...
}
);
Note
Successfully exported methods are tracked locally, and will be re-exported when reconnecting to the broker.
Locking
$conduit->acquireLock("app.tasks.cleanup")->then(
function (Token $token) {
// it is important that you make use of $token; the lock will be released
// when it falls out of scope, or its release() method is called.
}
)
$conduit->acquireMutex("app.tasks.indexer", 5)->then(
function (Token $token) {
// ...
}
)
Note
Lock states are synchronized on reconnect, reducing the risk of duplicate locks being granted. There is still a potential risk for this though, so make sure to track the
disconnectedevent in order to put sensitive tasks down just in case.
Conduit events
connected
This event will fire when the client has connected to a server.
disconnected
This event will fire when the client has disconnected from the server.
join
This event will fire when a new client has joined the broker. Use this to keep track of components or applications connected.
$conduit->on("join", function (string $nodeId, string $appId) {
// ..
});
leave
This event will fire when a client has disconnected from the broker. Use this to keep track of components or applications connected.
$conduit->on("leave", function (string $nodeId, string $appId) {
// ..
});
publish
$conduit->on("publish", function (string $topic, mixed $payload) {
// ..
});
Features
Dynamic master model
To make the library daemonless and dynamic, every client will start by trying to listen on the broker port. If it can't, that's fine. It it is available, it will set up a server to run in the process.
The next client will connect to the server hosted by the first client. If the first client is restarted or goes away, the entire process is repeated and the first client that is able to listen on the port will become the new master.
Dependencies
Dependencies
| ID | Version |
|---|---|
| noccylabs/react-protocol | ^0.1.0 |
| react/react | ^1.4 |