|
| 1 | +# AMQPSwoole Broker |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `AMQPSwoole` class is a specialized AMQP broker implementation designed for use in Swoole environments. It extends the base `AMQP` class and resolves compatibility issues that occur when using the standard AMQP broker in Swoole coroutines. |
| 6 | + |
| 7 | +## Why AMQPSwoole is Needed |
| 8 | + |
| 9 | +When using the standard `AMQP` broker in Swoole environments, you may encounter errors like: |
| 10 | + |
| 11 | +``` |
| 12 | +Fatal error: Uncaught Swoole\Error: API must be called in the coroutine |
| 13 | +``` |
| 14 | + |
| 15 | +This happens because: |
| 16 | +- The standard AMQP broker uses `AMQPStreamConnection` with `StreamIO` |
| 17 | +- `StreamIO` calls `stream_select()` which is not allowed outside Swoole coroutines |
| 18 | +- This causes fatal errors during connection cleanup and heartbeat operations |
| 19 | + |
| 20 | +## Solution |
| 21 | + |
| 22 | +The `AMQPSwoole` class solves this by: |
| 23 | +- Using `AMQPSwooleConnection` instead of `AMQPStreamConnection` |
| 24 | +- Leveraging `SwooleIO` which is designed for Swoole environments |
| 25 | +- Properly handling coroutine-based I/O operations |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Basic Usage |
| 30 | + |
| 31 | +```php |
| 32 | +use Utopia\Queue\Broker\AMQPSwoole; |
| 33 | +use Utopia\Queue\Queue; |
| 34 | + |
| 35 | +// Create broker instance |
| 36 | +$broker = new AMQPSwoole( |
| 37 | + host: 'localhost', |
| 38 | + port: 5672, |
| 39 | + user: 'guest', |
| 40 | + password: 'guest' |
| 41 | +); |
| 42 | + |
| 43 | +// Create queue |
| 44 | +$queue = new Queue('my-queue'); |
| 45 | + |
| 46 | +// Enqueue a message |
| 47 | +$broker->enqueue($queue, ['message' => 'Hello World']); |
| 48 | +``` |
| 49 | + |
| 50 | +### Consumer Usage |
| 51 | + |
| 52 | +```php |
| 53 | +use Utopia\Queue\Consumer; |
| 54 | +use Utopia\Queue\Message; |
| 55 | + |
| 56 | +$broker->consume( |
| 57 | + $queue, |
| 58 | + function (Message $message) { |
| 59 | + // Process the message |
| 60 | + $payload = $message->getPayload(); |
| 61 | + echo "Processing: " . json_encode($payload) . PHP_EOL; |
| 62 | + |
| 63 | + // Return result (optional) |
| 64 | + return new \Utopia\Queue\Result\Commit(); |
| 65 | + }, |
| 66 | + function (Message $message) { |
| 67 | + echo "Message processed successfully" . PHP_EOL; |
| 68 | + }, |
| 69 | + function (?Message $message, \Throwable $error) { |
| 70 | + echo "Error processing message: " . $error->getMessage() . PHP_EOL; |
| 71 | + } |
| 72 | +); |
| 73 | +``` |
| 74 | + |
| 75 | +## Configuration |
| 76 | + |
| 77 | +The `AMQPSwoole` class accepts the same configuration parameters as the base `AMQP` class: |
| 78 | + |
| 79 | +- `host`: RabbitMQ server hostname |
| 80 | +- `port`: RabbitMQ server port (default: 5672) |
| 81 | +- `httpPort`: RabbitMQ management HTTP port (default: 15672) |
| 82 | +- `user`: Username for authentication |
| 83 | +- `password`: Password for authentication |
| 84 | +- `vhost`: Virtual host (default: '/') |
| 85 | +- `heartbeat`: Heartbeat interval in seconds (default: 0) |
| 86 | +- `connectTimeout`: Connection timeout in seconds (default: 3.0) |
| 87 | +- `readWriteTimeout`: Read/write timeout in seconds (default: 3.0) |
| 88 | + |
| 89 | +## Testing |
| 90 | + |
| 91 | +The AMQPSwoole broker includes comprehensive tests that verify: |
| 92 | +- Basic message enqueueing and processing |
| 93 | +- Concurrency handling in Swoole environments |
| 94 | +- Error handling and retry mechanisms |
| 95 | +- Queue size reporting |
| 96 | + |
| 97 | +Run tests with: |
| 98 | +```bash |
| 99 | +composer test |
| 100 | +``` |
| 101 | + |
| 102 | +## Migration from AMQP to AMQPSwoole |
| 103 | + |
| 104 | +If you're currently using the `AMQP` broker in a Swoole environment and experiencing errors, migration is simple: |
| 105 | + |
| 106 | +```php |
| 107 | +// Before |
| 108 | +$broker = new \Utopia\Queue\Broker\AMQP($host, $port, $httpPort, $user, $password); |
| 109 | + |
| 110 | +// After |
| 111 | +$broker = new \Utopia\Queue\Broker\AMQPSwoole($host, $port, $httpPort, $user, $password); |
| 112 | +``` |
| 113 | + |
| 114 | +All other APIs remain exactly the same. |
0 commit comments