- How do I replace PHP-FPM with Swoole in my Laravel app using this bundle?
- Edit your `public/index.php` to include the bundle’s entrypoint (`require_once dirname(__DIR__).'/vendor/cesurapp/swoole-bundle/src/Runtime/entrypoint.php'`), then configure Swoole in `config/packages/swoole.yaml`. The bundle replaces the built-in HTTP server with Swoole’s high-performance coroutine model. Ensure your Laravel app is Symfony 8+ compatible, as this bundle is designed for Symfony frameworks.
- Does this bundle support WebSockets in Laravel, and how do I enable them?
- Yes, the bundle supports WebSockets out of the box. Enable them by setting `SERVER_HTTP_SOCKET=true` in your `.env` and configuring a WebSocket handler class in `swoole.yaml` under `websocket_handler`. Swoole’s built-in WebSocket support is optimized for low-latency real-time applications like chat or live updates. Note that WebSocket connections are server-bound, so horizontal scaling requires additional coordination.
- Can I use this bundle with Laravel’s built-in queue system (e.g., Redis or database queues)?
- The bundle includes a background task system with retry logic, but it’s not a direct replacement for Laravel queues. For complex queue setups, consider using Swoole’s task workers for short-lived jobs (under 10s) while offloading longer tasks to Laravel’s queue system. The bundle’s `task_worker` can be configured in `swoole.yaml` to handle synchronous or asynchronous tasks, but it lacks Laravel’s queue workers’ advanced features like delayed jobs.
- What Laravel versions or Symfony-based frameworks does this bundle support?
- This bundle is designed for Symfony 8+ and works seamlessly with Laravel 9+ (since Laravel 9 is Symfony 6+ based). If you’re using an older Laravel version (e.g., 8.x), ensure it’s compatible with Symfony 6+ components. The bundle leverages Symfony’s dependency injection, configuration, and event systems, so it won’t work with vanilla Laravel without Symfony integration.
- How do I configure cron jobs with Swoole in Laravel? The bundle mentions cron workers.
- Enable cron workers by setting `cron_worker: true` in `swoole.yaml` and `SERVER_WORKER_CRON=true` in `.env`. Cron jobs run simultaneously across all servers by default, but the bundle uses file-based locking to prevent duplicates. For multi-server setups (e.g., Kubernetes), ensure only one instance runs cron jobs to avoid conflicts. Configure job schedules in `swoole.yaml` under `failed_task_retry` and `failed_task_attempt` for retry logic.
- Will this bundle work in a Docker or Kubernetes environment? Are there any pitfalls?
- Yes, but you’ll need to configure Swoole’s process model for orchestration. Key considerations include health checks (Swoole lacks built-in support), graceful shutdowns, and session management for WebSockets. In Kubernetes, use sticky sessions or external solutions (e.g., Redis pub/sub) for WebSocket scaling. Avoid running multiple cron workers on the same job in multi-container setups, as file-based locking may cause issues.
- How do I monitor Swoole’s performance in production? Can I integrate it with Prometheus?
- The bundle doesn’t include built-in Prometheus support, but you can manually instrument Swoole metrics using its `Server` class methods (e.g., `stats()`, `coroutine_stats()`). For observability, log Swoole-specific metrics (e.g., task queue depth, coroutine count) via Symfony’s Monolog or export them to Prometheus using custom collectors. Tools like Grafana can then visualize these metrics alongside your Laravel app’s performance data.
- Are there alternatives to this bundle for high-performance Laravel apps? What are the trade-offs?
- Alternatives include `spatie/laravel-swoole` (Laravel-specific) or running Swoole as a reverse proxy (e.g., with Nginx). The `cesurapp/swoole-bundle` is Symfony-focused but works with Laravel 9+. Trade-offs include learning Swoole’s coroutine model (e.g., `go()`, `yield`) and managing state in shared memory. For pure Laravel, `spatie/laravel-swoole` may offer tighter integration, while reverse proxy setups provide more isolation but add complexity.
- How do I handle failed tasks or retries in this bundle? Does it support exponential backoff?
- Failed tasks are stored in the database with retry logic, configurable via `failed_task_retry` (e.g., `@EveryMinute10`) and `failed_task_attempt` (default: 2) in `swoole.yaml`. The bundle doesn’t support exponential backoff natively, but you can customize retry behavior by extending the `TaskWorker` class or using a middleware. For advanced retry strategies, consider pairing it with Laravel’s queue system or a library like `spatie/laravel-backup` for retries.
- Can I use Swoole’s process workers for long-running tasks (e.g., processing large files or database migrations)?
- Swoole’s process workers are optimized for short-lived tasks (under 10 seconds) and I/O-bound operations. For long-running tasks (e.g., large file processing or migrations), use Laravel’s queue system or external tools like Supervisor to manage background processes. Process workers in this bundle are designed for async I/O tasks (e.g., HTTP requests, WebSockets) and may not handle CPU-bound or blocking operations efficiently.