dkhorev / laravel-ampq
Pub/Sub with RabbitMQ in Laravel.
Installs: 100
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 2
Type:module
Requires
- php: >=8.0||>=7.4
- ext-json: *
- php-amqplib/php-amqplib: ^v3.0.0
Requires (Dev)
- orchestra/testbench: ^v6.14.0
This package is auto-updated.
Last update: 2025-06-20 00:36:24 UTC
README
Pub/Sub with RabbitMQ in Laravel.
Installation
composer require dkhorev/laravel-ampq
Publish configs
php artisan vendor:publish --tag=laravel-ampq --force
Listeners
setup your listeners in laravel-ampq.cfg
'callbacks' => [
'example_stack' => [
'exchange' => [
'topic' => ExampleListenerCallback::class,
],
],
],
this will listen for message on "exchange:topic" and pass any messages to ExampleListenerCallback->__invoke()
method
every listener should implement AmpqListenCallbackContract
(__invoke() method)
use dkhorev\LaravelAmpq\Contracts\AmpqListenCallbackContract; use PhpAmqpLib\Message\AMQPMessage; class MyTopicCallback implements AmpqListenCallbackContract { public function __invoke(AMQPMessage $msg): void { // TODO: Implement __invoke() method. } }
it is recommended to only do basic validation of messages in a callback, and queue any heavy processing jobs (to redis or whatever)
Listen command
php artisan ampq:listen {connection} {listenStack}
connection
- connection name from config's servers
array
listenStack
- stack of listening channels from config's callbacks
array`
Sending messages
To send a message with this package, you can use any connection from laravel-ampq.cfg
Using "local" connection (example)
use dkhorev\LaravelAmpq\Clients\AmpqClient; use dkhorev\LaravelAmpq\Contracts\SendToTopicServiceContract; $config = config('laravel-ampq.servers.local'); $client = new AmpqClient($config['host'], $config['user'], $config['password'], (int)$config['port']); $sender = resolve(SendToTopicServiceContract::class); $sender->postTo($client, 'some-exchange', 'some-topic', ['exapmple-data' => 'hello world!']);
Creating custom client connections
add custom server to config
'servers' => [
'custom' => [
...
],
],
extend AmpqClient
from this package and create an interface, i.e. AmpqClientCustomInterface
use dkhorev\LaravelAmpq\Clients\AmpqClient; class AmpqClientCustom extends AmpqClient implements AmpqClientCustomInterface {}
in your app's AppServiceProvider
bind new interface AmpqClientCustomInterface
to resolve with custom server
$this->app->bind( AmpqClientCustomInterface::class, static function () { $config = config('laravel-ampq.servers.custom'); return new AmpqClientCustom($config['host'], $config['user'], $config['password'], $config['port']); } );
Sending messages
use dkhorev\LaravelAmpq\Contracts\SendToTopicServiceContract; $sender = resolve(SendToTopicServiceContract::class); $client = resolve(AmpqClientCustomInterface::class); $sender->postTo($client, 'some-exchange', 'some-topic', ['exapmple-data' => 'hello world!']);