bschmitt / laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages
MIT License
268 stars 86 forks source link

How to access all the messages from the que #87

Open anishansari opened 3 years ago

anishansari commented 3 years ago

I am able to push the messages to the queue and I want to access all the messages and send the messages to the users dashboard or through SMS so I wanted to know how to get the content or all the message so that I can send them one by one to the users. Please help it is urgent.

Thank you

liviublidar commented 2 years ago

Hi @anishansari

You could call a function that just adds the messages to an array, then process it all in one go.

use App\Http\Controllers\Controller; use Bschmitt\Amqp\Consumer; use Bschmitt\Amqp\Amqp; use Illuminate\Http\Request; use PhpAmqpLib\Message\AMQPMessage;

class MyCoolController extends Controller {

    private $messageBatch = [];

    private function addMessageToBatch(AMQPMessage $message)
    {
        $this->messageBatch[] = $message;
    }

    private function sendMessagesViaSms()
    {
        //use $this->messageBatch here and do whatever with it
    }

    public function myCoolEntryPoint(Request $request, Amqp $consumer) 
    {
        $consumer->consume('myfancyqueue', function (AMQPMessage $message, Consumer $resolver) {
            $this->addMessageToBatch($message);
            $resolver->acknowledge($message); //ack message
            $resolver->stopWhenProcessed();
        });
        $this->sendMessagesViaSms();   
    }
}

Sorry if some of the formatting is wrong here, I don't have access to a proper editor at the minute ^^