Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Rabbitevents

nuwber/rabbitevents image

Rabbitevents stats

Downloads
135.5K
Stars
116
Open Issues
2
Forks
35

View on GitHub →

The Nuwber RabbitEvents package

RabbitEvents

Let's imagine a use case: a User made a payment. You need to handle this payment, register the user, send him emails, send analytics data to your analysis system, and so on. The modern infrastructure requires you to create microservices that do their specific job and only it: one handles payments, one is for user management, one is the mailing system, one is for analysis. How to let all of them know that a payment succeeded and handle this message? The answer is "To use RabbitEvents".

Once again, the RabbitEvents library helps you publish an event and handle it in another app. It doesn't make sense to use it in the same app because Laravel's Events work better for that.

Table of Contents

  1. Installation via Composer
  2. Upgrade from 7.x to 8.x
  3. Publisher component
  4. Listener component
  5. Examples
  6. Non-standard use

Installation via Composer

You can use Composer to install RabbitEvents into your Laravel project:

composer require nuwber/rabbitevents

Configuration

After installing RabbitEvents, publish its config and a service provider using the rabbitevents:install Artisan command:

php artisan rabbitevents:install

This command installs the config file at config/rabbitevents.php and the Service Provider file at app/providers/RabbitEventsServiceProvider.php.

The config file is very similar to the queue connection, but with the separate config, you'll never be confused if you have another connection to RabbitMQ.

<?php
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
 
return [
'default' => env('RABBITEVENTS_CONNECTION', 'rabbitmq'),
'connections' => [
'rabbitmq' => [
'driver' => 'rabbitmq',
'exchange' => env('RABBITEVENTS_EXCHANGE', 'events'),
'host' => env('RABBITEVENTS_HOST', 'localhost'),
'port' => env('RABBITEVENTS_PORT', 5672),
'user' => env('RABBITEVENTS_USER', 'guest'),
'pass' => env('RABBITEVENTS_PASSWORD', 'guest'),
'vhost' => env('RABBITEVENTS_VHOST', 'events'),
'delay_strategy' => env('RABBITEVENTS_DELAY_STRATEGY', RabbitMqDlxDelayStrategy::class),
'ssl' => [
'is_enabled' => env('RABBITEVENTS_SSL_ENABLED', false),
'verify_peer' => env('RABBITEVENTS_SSL_VERIFY_PEER', true),
'cafile' => env('RABBITEVENTS_SSL_CAFILE'),
'local_cert' => env('RABBITEVENTS_SSL_LOCAL_CERT'),
'local_key' => env('RABBITEVENTS_SSL_LOCAL_KEY'),
'passphrase' => env('RABBITEVENTS_SSL_PASSPHRASE', ''),
],
'read_timeout' => env('RABBITEVENTS_READ_TIMEOUT', 3.),
'write_timeout' => env('RABBITEVENTS_WRITE_TIMEOUT', 3.),
'connection_timeout' => env('RABBITEVENTS_CONNECTION_TIMEOUT', 3.),
'heartbeat' => env('RABBITEVENTS_HEARTBEAT', 0),
'persisted' => env('RABBITEVENTS_PERSISTED', false),
'lazy' => env('RABBITEVENTS_LAZY', true),
'qos' => [
'global' => env('RABBITEVENTS_QOS_GLOBAL', false),
'prefetch_size' => env('RABBITEVENTS_QOS_PREFETCH_SIZE', 0),
'prefetch_count' => env('RABBITEVENTS_QOS_PREFETCH_COUNT', 1),
]
],
],
'logging' => [
'enabled' => env('RABBITEVENTS_LOG_ENABLED', false),
'level' => env('RABBITEVENTS_LOG_LEVEL', 'info'),
'channel' => env('RABBITEVENTS_LOG_CHANNEL'),
],
];

Upgrade from 7.x to 8.x

PHP 8.1 required

RabbitEvents now requires PHP 8.1 or greater.

Supported Laravel versions

RabbitEvents now supports Laravel 9.0 or greater.

Removed --connection option from the rabbitevents:listen command

There's an issue #98 that still needs to be resolved. The default connection is always used instead.

RabbitEvents Publisher

The RabbitEvents Publisher component provides an API to publish events across the application structure. More information about how it works can be found on the RabbitEvents Publisher page.

RabbitEvents Listener

The RabbitEvents Listener component provides an API to handle events that were published across the application structure. More information about how it works can be found on the RabbitEvents Listener page.

Non-standard use

If you're using only one part of RabbitEvents, you should know a few things:

  1. You remember, we're using RabbitMQ as the transport layer. In the RabbitMQ Documentation, you can find examples of how to publish your messages using a routing key. This routing key is the event name, like something.happened from the examples above.

  2. Rabbit

Events expects that a message body is a JSON-encoded array. Every element of an array will be passed to a Listener as a separate variable. For example:

[
{
"key": "value"
},
"string",
123
]

There are 3 elements in this array, so 3 variables will be passed to a Listener (an array, a string, and an integer). If an associative array is being passed, the Dispatcher wraps this array by itself.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Nuwber Rabbitevents Related Articles

Redis Cluster Support for Queues in Laravel 13.5.0 image

Redis Cluster Support for Queues in Laravel 13.5.0

Read article
Flaky Test Retries in Pest v4.5.0 image

Flaky Test Retries in Pest v4.5.0

Read article
Manage Software Licenses in Laravel with Laravel Licensing image

Manage Software Licenses in Laravel with Laravel Licensing

Read article
Axios npm Package Compromised With Remote Access Trojan image

Axios npm Package Compromised With Remote Access Trojan

Read article
Laravel USPS: A Modern Wrapper for the USPS API image

Laravel USPS: A Modern Wrapper for the USPS API

Read article
Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More image

Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More

Read article
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian