tourze/workerman-messenger-bundle

Workerman + Symfony Messenger

Installs: 9

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/tourze/workerman-messenger-bundle

0.0.1 2025-04-21 14:08 UTC

This package is auto-updated.

Last update: 2025-10-31 16:51:52 UTC


README

PHP Version License Build Status Code Coverage

English | 中文

A Symfony Bundle that integrates Workerman with Symfony Messenger, providing seamless message processing statistics and event handling in high-performance Workerman environments.

Installation

composer require tourze/workerman-messenger-bundle

Quick Start

  1. Enable the Bundle

Add the bundle to your config/bundles.php:

<?php

return [
    // ... other bundles
    Tourze\WorkermanMessengerBundle\WorkermanMessengerBundle::class => ['all' => true],
];
  1. Configure Symfony Messenger (if not already configured)
# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            'App\Message\YourMessage': async
  1. Run with Workerman
<?php
// workerman.php

use Workerman\Worker;
use App\Kernel;

$kernel = new Kernel('prod', false);
$kernel->boot();

$worker = new Worker();
$worker->onMessage = function($connection, $data) use ($kernel) {
    // Your message processing logic here
    // Statistics will be automatically tracked by the bundle
};

Worker::runAll();

Features

  • Automatic Statistics Tracking: Automatically increments Workerman statistics when Symfony Messenger processes messages
  • Environment Detection: Only activates when running in Workerman environment
  • Zero Configuration: Works out of the box with sensible defaults
  • Event-Driven Architecture: Uses Symfony Event Dispatcher for loose coupling

How It Works

The bundle provides an event subscriber that listens to Symfony Messenger events:

  • WorkerMessageHandledEvent: Increments total_request counter
  • WorkerMessageFailedEvent: Increments send_fail counter

The statistics are only updated when running in a Workerman environment, making it safe to use in both traditional web and Workerman contexts.

Code Example

<?php

use Symfony\Component\Messenger\MessageBusInterface;
use App\Message\ProcessDataMessage;

class DataProcessor
{
    public function __construct(
        private MessageBusInterface $bus
    ) {}

    public function processData(array $data): void
    {
        // Dispatch message - statistics will be automatically tracked
        $this->bus->dispatch(new ProcessDataMessage($data));
    }
}

Testing

# Run tests
./vendor/bin/phpunit packages/workerman-messenger-bundle/tests

# Run static analysis
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/workerman-messenger-bundle

Requirements

  • PHP ^8.3
  • Symfony ^7.3
  • Workerman ^5.1

License

This package is open-sourced software licensed under the MIT license.