infoesportes / messaging-laravel
Laravel adapter for InfoEsportes messaging microservice with RabbitMQ, Horizon integration for Email, SMS, and WhatsApp
Requires
- php: ^8.1
- illuminate/console: ^11.0|^12.0
- illuminate/queue: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
- infoesportes/messaging-rabbitmq: ^1.0
Requires (Dev)
- laravel/pint: ^1.0
- mockery/mockery: ^1.5
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
README
Laravel adapter for InfoEsportes messaging microservice with RabbitMQ. Provides Horizon integration for Email, SMS, and WhatsApp messaging.
Features
- 🚀 RabbitMQ Integration - Topic exchange with routing keys
- 📧 Email Support - Transactional and marketing emails
- 📱 SMS Support - OTP and alerts
- 💬 WhatsApp Support - Notifications and support messages
- 🔄 Laravel Horizon - Queue monitoring and management
- ⚡ Background Jobs - Async processing with Laravel queues
- 🎯 Routing Keys - Flexible message routing
- 🛡️ Reliability - Quorum queues for high availability
Architecture
CodeIgniter/Laravel → Plain JSON → RabbitMQ
↓
Consumer (ACK immediately)
↓
Dispatch Laravel Job
↓
Redis/Database Queue (Horizon)
↓
ProcessEmailJob/ProcessSMSJob/ProcessWhatsAppJob
Installation
composer require infoesportes/messaging-laravel
Publish Configuration
php artisan vendor:publish --tag=messaging-config
Environment Variables
Add to your .env file:
RABBITMQ_HOST=localhost RABBITMQ_PORT=5672 RABBITMQ_USER=guest RABBITMQ_PASSWORD=guest RABBITMQ_VHOST=/ RABBITMQ_EXCHANGE=messages.topic RABBITMQ_PREFETCH_COUNT=10 RABBITMQ_CONSUMER_TAG=laravel_consumer MESSAGING_QUEUE_CONNECTION=redis MESSAGING_QUEUE_NAME=messaging
Usage
Publishing Messages
Using Facade
use Infoesportes\Messaging\Laravel\Facades\Messaging; // Email - Transactional Messaging::publishEmail('transactional', [ 'to' => 'user@example.com', 'subject' => 'Your Invoice', 'body' => 'Invoice details...', ]); // Email - Marketing Messaging::publishEmail('marketing', [ 'to' => 'user@example.com', 'subject' => 'Newsletter', 'template' => 'newsletter', ]); // SMS - OTP Messaging::publishSms('otp', [ 'to' => '+5511999999999', 'code' => '123456', ]); // SMS - Alert Messaging::publishSms('alert', [ 'to' => '+5511999999999', 'message' => 'Urgent alert!', ]); // WhatsApp - Notification Messaging::publishWhatsApp('notification', [ 'to' => '+5511999999999', 'message' => 'You have a new message', ]); // WhatsApp - Support Messaging::publishWhatsApp('support', [ 'to' => '+5511999999999', 'message' => 'Support ticket created', ]);
Using Service Container
$messaging = app('messaging'); $messaging->publish('email.transactional', [ 'to' => 'user@example.com', 'subject' => 'Custom Email', ]);
Consuming Messages
Start the consumer for each queue type:
# Consume email messages php artisan messaging:consume email # Consume SMS messages php artisan messaging:consume sms # Consume WhatsApp messages php artisan messaging:consume whatsapp # With timeout (in seconds) php artisan messaging:consume email --timeout=3600
Supervisor Configuration
Create /etc/supervisor/conf.d/messaging-consumer.conf:
[program:messaging-email-consumer] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan messaging:consume email autostart=true autorestart=true user=www-data numprocs=2 redirect_stderr=true stdout_logfile=/var/log/messaging-email-consumer.log [program:messaging-sms-consumer] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan messaging:consume sms autostart=true autorestart=true user=www-data numprocs=1 redirect_stderr=true stdout_logfile=/var/log/messaging-sms-consumer.log [program:messaging-whatsapp-consumer] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan messaging:consume whatsapp autostart=true autorestart=true user=www-data numprocs=1 redirect_stderr=true stdout_logfile=/var/log/messaging-whatsapp-consumer.log
Then reload supervisor:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start messaging-email-consumer:* sudo supervisorctl start messaging-sms-consumer:* sudo supervisorctl start messaging-whatsapp-consumer:*
Routing Keys
The package uses the following routing key structure:
email.transactional- Invoices, receipts, confirmationsemail.marketing- Newsletters, promotions
SMS
sms.otp- One-time passwords, verification codessms.alert- Urgent alerts, notifications
whatsapp.notification- General notificationswhatsapp.support- Support messages, tickets
Job Processing
The consumer dispatches jobs to Laravel's queue system. Customize the job classes:
ProcessEmailJob
namespace Infoesportes\Messaging\Laravel\Jobs; use Illuminate\Support\Facades\Mail; class ProcessEmailJob implements ShouldQueue { public function handle(): void { // Implement your email sending logic Mail::to($this->data['to']) ->send(new YourMailable($this->data)); } }
ProcessSMSJob
public function handle(): void { // Implement your SMS provider logic $smsProvider = app(SMSProviderInterface::class); $smsProvider->send($this->data['to'], $this->data['message']); }
ProcessWhatsAppJob
public function handle(): void { // Implement your WhatsApp provider logic $whatsappProvider = app(WhatsAppProviderInterface::class); $whatsappProvider->send($this->data['to'], $this->data['message']); }
Testing
composer test
Code Style
composer format
Configuration
See config/messaging.php for all available options.
Key configuration sections:
- RabbitMQ Connection - Host, port, credentials
- Exchange Configuration - Topic exchange settings
- Queue Configuration - Queue names, routing keys, durability
- Consumer Configuration - Prefetch count, consumer tag
- Job Mapping - Map queue types to job classes
Horizon Integration
This package works seamlessly with Laravel Horizon. Jobs dispatched from the RabbitMQ consumer appear in Horizon for monitoring:
- Install Horizon:
composer require laravel/horizon - Publish config:
php artisan horizon:install - Start Horizon:
php artisan horizon - View dashboard:
http://your-app.test/horizon
Requirements
- PHP 8.1 or higher
- Laravel 10.x or 11.x
- RabbitMQ 3.8 or higher
- Redis (for queue driver)
Credits
License
The MIT License (MIT). Please see License File for more information.