sulu/messenger
Symfony Messenger add-on for Sulu providing stamps and middlewares to configure the Sulu message bus. Includes UnpackExceptionMiddleware to surface real handler errors and LockMiddleware to prevent concurrent access. Usable standalone in any Symfony app.
Decoupling Business Logic from Immediate Execution: Adopt a message-driven architecture (MDA) to replace direct API calls or cron jobs with async message handlers. Ideal for Laravel applications with:
User::updateProfile() API endpoint with a ProfileUpdatedMessage handled asynchronously.Concurrency and Race Condition Prevention:
Implement distributed locking via LockMiddleware to protect critical resources in high-traffic Laravel apps, such as:
Product entity during stock updates to avoid race conditions when multiple users request the same item.Database Consistency Without Global Transactions:
Use DoctrineFlushMiddleware to opt-in flush operations for Doctrine entities, avoiding:
UserOrder is fully processed, ensuring related OrderItem entities are saved without forcing a full transaction.Improved Error Handling and Observability:
Leverage UnpackExceptionMiddleware to surface raw exceptions from failed handlers, enabling:
HandlerFailedException to 422 Unprocessable Entity in Laravel APIs).PaymentFailedException in a handler and return a user-friendly error message via a Laravel controller.Roadmap for Scalable Architecture:
OrderCreatedEvent, PaymentProcessedEvent) for auditability.UserService publishes UserUpdatedMessage for NotificationService to consume).Build vs. Buy:
spatie/laravel-messenger or direct Symfony components.spatie/laravel-messenger or direct symfony/messenger integration) and PHP 8.2+ (Laravel 10+).DoctrineFlushMiddleware.Illuminate\Queue) for simple background jobs.league/amqp or php-amqplib for advanced AMQP-based messaging.Illuminate\Queue\InteractsWithQueue with custom retry logic.symfony/messenger-bundle (if using Symfony) or spatie/laravel-queue-scheduler.failed() method for job retries.symfony/messenger-bundle’s retry features.DoctrineFlushMiddleware won’t work with Laravel’s Eloquent. Use Eloquent’s built-in flush or transactions instead."This package helps us build scalable, resilient Laravel applications by adopting a message-driven architecture. Here’s why it’s worth investing in:
It’s a low-risk choice: MIT-licensed, tested in production by Sulu CMS, and integrates seamlessly with our existing Laravel/Symfony stack. We can start with simple use cases (e.g., async notifications) and expand to critical systems later."
"This is a Symfony Messenger extension that adds three powerful middlewares to Laravel’s queue/job system:
LockStamp('product-123')). Supports TTL and auto-release—ideal for inventory or payment systems.EnableFlushStamp (requires Doctrine ORM). Avoids global transactions or N+1 queries.Why use it?
spatie/laravel-messenger or direct Symfony components.Proposal: Start by replacing a high-failure-rate API endpoint with a message handler + queue. For example:
User::updateProfile() endpoint blocks requests.ProfileUpdatedMessage to a queue, with locking to prevent concurrent updates.Next steps:
spatie/laravel-messenger (if not already using Symfony Messenger).composer require sulu/messenger.Potential challenges:
spatie/laravel-messenger bridges the gap)."*"This package improves system reliability and observability by:
Impact:
Recommendation: Pilot in a high-risk area (e.g., payment processing) to validate locking/flushing benefits."*
How can I help you explore Laravel packages today?