react-command-bus/examples/notify.php

44 lines
1.3 KiB
PHP

<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\React\CommandBus\CommandBus;
use NoccyLabs\React\CommandBus\CommandBusClient;
use NoccyLabs\React\CommandBus\CommandRegistry;
use NoccyLabs\React\CommandBus\Context;
use React\EventLoop\Loop;
use React\Promise\Promise;
use React\Socket\SocketServer;
// This example demonstrates notifications, so there are no commands here.
// You can add whatever commands you like.
$commands = new CommandRegistry();
$bus = new CommandBus($commands);
$server = new SocketServer("tcp://127.0.0.1:9999");
$bus->addServer($server);
$bus->on(CommandBus::EVENT_NOTIFY,
function (string $event, array $data) {
printf("notify event: %s %s\n", $event, json_encode($data, JSON_UNESCAPED_SLASHES));
}
);
$client = new CommandBusClient();
$client->connect("tcp://127.0.0.1:9999");
$client->on(CommandBusClient::EVENT_NOTIFY,
function (string $event, array $data) {
printf("notify client: %s %s\n", $event, json_encode($data, JSON_UNESCAPED_SLASHES));
}
);
// Wait for connections etc, before sending a notification.
Loop::addTimer(1, function () use ($bus) {
$bus->notify("hello", [ 'greet'=>"World" ]);
});
// Shutdown in 2 secs
Loop::addTimer(2, function () use ($bus, $client) {
$bus->close();
$client->close();
});