dekalee/pubsub-swarrot-bundle
Installation:
composer require dekalee/pubsub-swarrot-bundle
Register the bundle in config/app.php under providers:
Dekalee\PubSubSwarrotBundle\DekaleePubSubSwarrotBundle::class,
Configure:
Add to config/swarrot.php (or config.yml if using legacy config):
swarrot:
provider: pub_sub
default_connection: pub_sub
connections:
pub_sub:
host: 'noneRequired' # Placeholder; actual config depends on your pub/sub system (e.g., Redis, RabbitMQ)
First Use Case: Publish a message via a Swarrot event:
use Swarrot\Event\Event;
// Dispatch an event (e.g., in a controller or service)
$event = new Event('user.created', ['user_id' => 123]);
$this->get('swarrot')->dispatch($event);
Subscribe to the event in a listener:
use Dekalee\PubSubSwarrotBundle\Subscriber\PubSubSubscriber;
class UserCreatedSubscriber extends PubSubSubscriber
{
public function onUserCreated(Event $event)
{
// Handle the event
}
protected function getSubscribedEvents()
{
return [
'user.created' => 'onUserCreated',
];
}
}
Register the subscriber in config/swarrot.php:
swarrot:
subscribers:
- App\Subscriber\UserCreatedSubscriber
Event-Driven Architecture:
order.placed) and let subscribers (e.g., analytics, notifications) react.$event = new Event('order.placed', ['order_id' => $order->id]);
$this->get('swarrot')->dispatch($event);
Connection Management:
config/swarrot.php:
connections:
redis:
host: 'redis://localhost:6379'
rabbitmq:
host: 'amqp://user:pass@rabbitmq:5672'
$this->get('swarrot')->setConnection('rabbitmq')->dispatch($event);
Subscriber Patterns:
class NotificationSubscriber extends PubSubSubscriber
{
private $notifier;
public function __construct(Notifier $notifier)
{
$this->notifier = $notifier;
}
public function onOrderPlaced(Event $event)
{
$this->notifier->send('Order #'.$event->getData()['order_id'].' placed!');
}
}
subscribers:
- { id: 'high_priority', class: App\Subscriber\HighPrioritySubscriber, priority: 10 }
- { id: 'low_priority', class: App\Subscriber\LowPrioritySubscriber, priority: -10 }
Error Handling:
try {
$this->get('swarrot')->dispatch($event);
} catch (PubSubException $e) {
Log::error('PubSub failed: '.$e->getMessage());
// Retry or fallback logic
}
Testing:
$this->get('swarrot')->setConnection('test'); // Assume 'test' is a mock connection
Connection Configuration:
host: 'noneRequired' in the default config is misleading. Replace it with your actual pub/sub system’s connection string (e.g., Redis, RabbitMQ, or AWS SQS).connections:
pub_sub:
host: 'redis://127.0.0.1:6379/0'
Subscriber Registration:
Dekalee\PubSubSwarrotBundle\Subscriber\PubSubSubscriber (or implement its interface). Forgetting this will cause silent failures.config/swarrot.php under the subscribers key.Event Serialization:
__serialize() and __unserialize() or use JsonSerializable.Performance:
Debugging:
swarrot:
debug: true
Bundle Maturity:
SwarrotBundle dependency may introduce additional configuration quirks. Refer to its documentation.Environment-Specific Configs: Use Laravel’s environment configs to manage pub/sub connections per environment:
# config/swarrot.php
connections:
pub_sub: '%env(SWARROT_PUBSUB_CONNECTION)%'
# .env
SWARROT_PUBSUB_CONNECTION=redis://localhost:6379/0
Event Namespacing:
Prefix event names to avoid collisions (e.g., app.user.created instead of user.created).
Middleware for Events: Use Swarrot’s middleware to preprocess events (e.g., logging, validation):
class LogEventMiddleware implements EventMiddlewareInterface
{
public function handle(Event $event, callable $next)
{
Log::debug('Event dispatched: '.$event->getName());
return $next($event);
}
}
Register in config/swarrot.php:
swarrot:
middleware:
- App\Middleware\LogEventMiddleware
Extending the Bundle:
PubSubProvider by binding your own implementation in a service provider:
$this->app->bind(
\Swarrot\Provider\ProviderInterface::class,
\App\Provider\CustomPubSubProvider::class
);
PubSubSubscriber class to add custom pub/sub logic (e.g., message transformation).Monitoring: Integrate with Laravel Horizon or similar tools to monitor pub/sub activity (e.g., message rates, failures).
How can I help you explore Laravel packages today?