jakubkulhan / bunny

Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library
MIT License
698 stars 101 forks source link

The best way for non-blocking publishing #121

Open gegorov2030 opened 2 years ago

gegorov2030 commented 2 years ago

Can you suggest the best way to forward messages from stdin to rabbitmq. Unfortunately, in the example below, stdin is blocked for a significant amount of time.

<?php
declare(strict_types=1);

use Bunny\Client;

require_once __DIR__ . '/vendor/autoload.php';

$channel = (new Client())->connect()->channel();

while (($record = fgetcsv(STDIN, 4096, ',')) !== false) {
    $channel->publish(json_encode($record), [], '', $config['queue']['name']);
}

Logically, I need to use an asynchronous client but did not find a similar example.

WyriHaximus commented 2 years ago

Maybe https://github.com/clue/reactphp-stdio can help with this? Alternatively this package and example might be even better for your use case: https://github.com/clue/reactphp-csv#usage