amphp/amp
AMPHP (AMP) accelerates PHP concurrency with fibers, eliminating callbacks and generators. Built on PHP 8.1’s cooperative coroutines, it lets you run async tasks like sync code—ideal for I/O-bound apps. Use Amp\async() for parallel execution and Future::await() to handle results seamlessly. No event...
amphp/amp aligns perfectly with modern PHP architectures requiring high concurrency (e.g., microservices, real-time APIs, or I/O-bound workloads). Its fiber-based coroutines enable cooperative multitasking without threads, reducing complexity while improving scalability.amp’s async primitives. However, amp can be integrated as a background worker (e.g., for async jobs, WebSockets, or long-running tasks) while preserving Laravel’s synchronous core.revolt/event-loop for scheduling, which adds ~100KB to the footprint. This is negligible for dedicated async services but may be overkill for lightweight Laravel apps.queue:work with amp-powered workers (e.g., using amphp/parallel for CPU-bound tasks or amphp/http-client for external API calls).amphp/http-server for WebSocket endpoints (e.g., Laravel Echo alternatives) or replace pusher-php-server with amp-based solutions.amphp/mysql/amphp/postgres drivers can replace Eloquent queries in async contexts (e.g., background data syncs).amp only for I/O-bound operations (e.g., HTTP calls, DB queries) while keeping business logic synchronous via Future::await().sleep(), legacy DB drivers) will stall the entire process. Requires strict avoidance of blocking code.Amp\Future or Amp\Promise for data passing).amp’s DI is fiber-aware but incompatible with Laravel’s container (e.g., no async singleton resolution).Future wrappers; synchronous middleware will block fibers.amp-aware test runners (e.g., Amp\Test utilities).amp needed for user-facing async (e.g., WebSockets) or background tasks (e.g., cron jobs, data pipelines)?file_get_contents(), curl_exec()) that must be rewritten?amp run in the same process as Laravel (risking fiber leaks) or as a separate service (e.g., via Docker)?Amp\Cancellation timeouts)?queue:work with amp-based consumers (e.g., Amp\async() + amphp/parallel).amphp/http-server for async endpoints (e.g., GraphQL subscriptions, SSE).amphp/http-client for non-blocking requests.amphp/mysql/amphp/postgres for async queries (e.g., in workers, not controllers).Amp\Test\Loop for async tests.Xdebug with fiber-aware extensions (e.g., ext-fiber).Amp\currentFiber()) and cancellation rates.Phase 1: Isolated Async Components
amp to a new service (e.g., laravel-async-worker) handling background tasks.Amp\async() for non-critical I/O (e.g., logging, analytics).Log::channel()->info() with Amp\async(fn() => Log::info()).Phase 2: Hybrid Integration
amp workers:
// app/Console/Commands/ProcessAsyncJobs.php
Amp\async(function () {
while (true) {
$job = dispatch_fresh_job();
$job->handle(); // Sync or async, but non-blocking
}
});
amphp/http-client in workers.Phase 3: Async Endpoints
amphp/http-server for real-time features (e.g., WebSockets):
$server = new Amp\Http\Server();
$server->request('GET', '/ws', fn($request) => new Amp\Http\WebSocket($request));
amp via a reverse proxy (e.g., Nginx).Phase 4: Full Async Rewrite (Optional)
Future objects (e.g., for async responses).Amp\Future\await() in middleware for non-blocking auth/validation.| Laravel Component | Compatibility | Workaround |
|---|---|---|
| Eloquent | ❌ (Blocking) | Use amphp/mysql in workers only. |
| Queues | ✅ (Partial) | Replace workers with amp-powered ones. |
| HTTP (Guzzle) | ❌ (Blocking) | Migrate to amphp/http-client. |
| Middleware | ⚠️ (Async-only) | Use Future-wrapped middleware. |
| Blade/Templating | ✅ | Render templates synchronously. |
| Cache (Redis) | ✅ (Non-blocking) | Use amphp/redis if needed. |
queue:work with amp-based consumers first (lowest risk).amphp/http-server for WebSockets/SSE alongside Laravel’s HTTP server.amp equivalents.Amp\Cancellation timeouts to handle slow operations.Future::await() calls can leak fibers, causing memory bloat.Amp\Cancellation timeouts for all async operations.Amp\currentFiber() + gc_enabled() checks).amp and revolt are actively maintained, but breaking changes may require rewrites (e.g., fiber API shifts).Amp\Trace or Xdebug with fiber support.Future rejections) must be caught and logged explicitly. Avoid silent failures.try {
$result = $future->await();
} catch (Throwable $e) {
Sentry\captureException($e);
$deferred->error(new RuntimeException("Async failed: {$e->getMessage()}"));
}
Future combinators, and event loops.await() futures").amp’s API is stable, but migrating to other async PHP frameworks (e.g., Swoole) later may be costly.How can I help you explore Laravel packages today?