-
Notifications
You must be signed in to change notification settings - Fork 107
Open
Description
Hi!
We have an async socket server written in ReactPHP. In some cases we would like to forward the processed packages to an AMQP server. Can you tell me what is the best practice to publish a message in this case? Currently we build up an async connection when the server starts, and in the onConnection event handler we connect to a channel, publish a message and then we close the channel. I don't think this is the proper way...
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:9999', $loop);
$amqp_options = [
'host' => 'localhost',
'vhost' => 'x',
'user' => 'x',
'password' => 'xx',
];
$amqp_async_client = new Bunny\Async\Client($loop, $amqp_options);
$amqp_async_client->connect()->then(function (Bunny\Async\Client $client) use (&$socket) {
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) use ($client) {
$connection->write("Hello " . $connection->getRemoteAddress() . "!\n");
$connection->on('data', function ($data) use ($connection, $client) {
$connection->write("You said: " . strtoupper($data) . "\n");
$client->channel()->then(function (Bunny\Channel $channel) use ($data) {
return $channel->exchangeDeclare('test', 'direct');
}
)->then(function (Bunny\Channel $channel) use ($data) {
$channel->publish($data, [], 'test')->then( function () use ($channel) {
$channel->close();
});
}
);
});
$connection->on('end', function () use ($connection) {
$connection->close();
});
$connection->on('error', function (Exception $e) {
echo 'conn error: ' . $e->getMessage() . PHP_EOL;
});
})->on('error', function (Exception $e) {
echo 'server error: ' . $e->getMessage() . PHP_EOL;
})->on('end', function () {
echo 'server quit';
});
});
$loop->run();Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels