Download the PHP package chocofamilyme/laravel-pubsub without Composer
On this page you can find all versions of the php package chocofamilyme/laravel-pubsub. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chocofamilyme/laravel-pubsub
More information about chocofamilyme/laravel-pubsub
Files in chocofamilyme/laravel-pubsub
Package laravel-pubsub
Short Description AMQP wrapper for Laravel to publish and consume messages
License BSD-3-Clause
Informations about the package laravel-pubsub
Laravel PubSub Library
Laravel pub/sub library allows you to publish, consume and process rabbit events.
Installation
Upgrade v6 -> v7
Read upgrade guide here
Upgrade v5 -> v6
Read upgrade guide here
Upgrade v3 -> v4
Read upgrade guide here
Publishing the configuration and migration
Configurations
AMQP (RabbitMQ) configuration
- Set environment BROADCAST_DRIVER = rabbitmq
-
Add to config/broadcasting.php rabbitmq driver
- AMQP configuration should be inserted into config/queue.php
Params
Key | Value | Description |
---|---|---|
connection | Default PhpAmqpLib\Connection\AMQPLazyConnection::class | php-amqplib |
options | Array | See - php-amqplib |
options.message-ttl | miliseconds | Message life time |
options.publisher.queue | Array | Publisher config |
options.publisher.queue.declare | false | Should create queue before publishing |
options.publisher.queue.bind | false | Should bind queue with exchange before publishing |
options.publisher.exchange.declare | false | Should created exchange before publishing |
options.publisher.exchange.name | string | Exchange name |
Event routing configuration
Event routing configuration file is located under config/pubsub.php and contains configuration for EventRouting and storing events in database.
Usage
You can listen for RabbitMQ events and publish them.
--job=laravel flag should be used if you want to listen to inter project events but from rabbitmq queue.
--job=laravel flag should be used for intermicroservice communictaion.
Examples
Single event
Will listen to single event "gateway.user.authenticated" in default exchange and queue name. Configure the internal event routing in Event is taken from payload, when you publish the event it appends the event name automatically there.
Wildcard event
Will listen to all "gateway.user.*" events in exchange gateway and with queue name "guardqueue"
Interproject communictaion
Will listen for default laravel event, in the default case --job=laravel is set by default
php artisan event:listen flags and parameters
How to publish events
For laravel default way see the laravel documentation
If you want to publish event to RabbitMQ
We've tried to make it easy as possible for you, see how it works:
- Create event with , please aware that the name of your event class will be the event name in the message payload. It is then used for internal router
- Open fresh created event and extends from
- You will have to override values of constants couple of methods like , . These constants tell the dispatcher which exchange should be used for this event and which routing key. See? It's pretty self-descriptive.
- Since you extended from class you could override more methods which could make the event more precise, for that please see inside this class.
- After our event is ready, we now can publish it in laravel way:
Since this event extends from PublishEvent class implementing SendToRabbitMQInterface, it will be sent into rabbitmq automatically.
PS: Please note that you need to override toPayload() method returning array that would be used as message payload.
Example event class
Example setting listen
in config/pubsub.php
To save failed jobs, you need to make the following changes config in queue.php and set uuid column nullable in failed_jobs table
All versions of laravel-pubsub with dependencies
ext-json Version *
vladimir-yuldashev/laravel-queue-rabbitmq Version ^13.0