ssi-anik / amqp

php-amqplib wrapper that eases the consumption of RabbitMQ. A painless way of using RabbitMQ
https://packagist.org/packages/anik/amqp
MIT License
134 stars 23 forks source link

How to keep the connection alive? #31

Closed abublihi closed 5 months ago

abublihi commented 5 months ago

@ssi-anik is it possible to keep the connection alive,,

Currently, for each time we need to publish a new message to RabittMQ we need to make a new connection, it's very slow, is it possible to make the connection as a keepalive?

ssi-anik commented 5 months ago

Hey @abublihi I assume your data is produced by the web application serving behind web servers. If that's the case then I'd say that no. To my knowledge, It's not possible even with any available package. Because that's the nature of PHP. Your script gets executed for each request and after the request is served those connections (read variables) get erased from the memory.

What you can do is keep a variable alive throughout the lifecycle of the request (just like the laravel container does) and if you publish multiple messages, that connection should be alive for that request will use the same connection.

Or if you're using Laravel, I'd suggest you use https://github.com/ssi-anik/laravel-amqp

Hope this answers your question.

abublihi commented 5 months ago

Thank you very much, that answers my question.

abublihi commented 5 months ago

After some searching i have found that it's possible to make the connection alive when using Laravel Octane, as of https://github.com/vyuldashev/laravel-queue-rabbitmq/issues/460 you can see that itls possible to make the connection a live,,

So can we replicate that using this package when using Laravel Octane?

@ssi-anik

ssi-anik commented 5 months ago

Hello @abublihi Laravel Octane works differently than traditional PHP applications.

Screenshot 2024-04-04 at 5 47 51 AM

I haven't used Octane yet and am unsure if the package will work with Octane. None of the consumers of the package had informed me before about it. Could you give it a try? I will have to do some research. But I won't be able to manage time for the next couple of weeks.

abublihi commented 5 months ago

Not a problem, i will try my best to contribute on this regard.

thank you @ssi-anik