amphp/socket
Async, non-blocking socket library for AMPHP. Provides client/server abstractions over TCP, UDP, and Unix domain sockets with DNS resolution, retries, connect timeouts, cancellation, and optional TLS encryption. Implements ReadableStream/WritableStream.
amphp/socket is a perfect fit for Laravel applications targeting high-concurrency workloads (e.g., WebSockets, real-time APIs, or async task queues). Its integration with Amp’s fiber-based concurrency model aligns with Laravel’s growing support for async features (e.g., Laravel Horizon, Laravel Echo).beberlei/ratchet or reactphp/socket).HttpClient for hybrid sync/async workflows.ReactPHP is async but not fiber-based).Laravel Horizon).Illuminate\Support\Facades\URL can substitute).| Risk Area | Mitigation Strategy |
|---|---|
| Fiber Context Leaks | Use Amp\Loop::run() in Laravel’s console commands or queues to avoid global state conflicts. |
| TLS Misconfiguration | Enforce ServerTlsContext with strict peer verification (avoid self-signed in production). |
| Backpressure | Implement ReadableStream::pause() in custom handlers to prevent memory bloat. |
| Laravel’s Sync Stack | Isolate async code in separate services (e.g., SocketService) to avoid blocking HTTP requests. |
ServerSocket or SocketConnector.)amphp/byte-stream filters.ConnectException) be logged/retried? (Laravel’s Log::error() + Retry package.)| Laravel Component | Integration Point |
|---|---|
| HTTP Layer | Replace GuzzleHttp for raw socket APIs (e.g., gRPC, MQTT). |
| Queues | Use Amp\Socket\connect() in queue workers for async I/O (e.g., Kafka). |
| Broadcasting | Replace Pusher/Ably with custom WebSocket server using ServerSocket. |
| Console Commands | Offload long-running tasks (e.g., file downloads) to fibers. |
| Middleware | Add TLS termination or protocol upgrades (e.g., HTTP/2) via middleware. |
file_get_contents()) with Amp\Socket\connect() in a console command.Swoole/RoadRunner.SocketClient facade wrapping Amp\Socket for consistency with Laravel’s Http facade.// app/Services/SocketClient.php
class SocketClient {
public function connect(string $host, int $port): Socket {
return Amp\Socket\connect("$host:$port");
}
}
SocketConnector for external APIs.Laravel Echo with a custom WebSocket server using ServerSocket.monolog/socket-handler).spatie/laravel-circuit-breaker) for retries.| Dependency | Version Requirement | Laravel Conflict Risk | Resolution |
|---|---|---|---|
amphp/byte-stream |
^2.0 | None (used by Horizon) | No action needed. |
league/uri |
^7.0 | Low (Laravel uses illuminate/url) |
Use illuminate/support/Url instead. |
reactphp/socket |
Avoid (Amp is fiber-native) | High (dual async stack) | Deprecate ReactPHP in favor of Amp. |
amphp/socket and amphp/byte-stream to composer.json.SocketService class to abstract Amp calls.ServerSocket.SocketConnector implementations (e.g., Socks5SocketConnector).ServerTlsContext (consider spatie/laravel-tls for Laravel).try/catch for ConnectException, BindException, and TlsException.try {
$socket = Amp\Socket\connect("example.com:80");
} catch (ConnectException $e) {
Log::error("Socket failed: {$e->getMessage()}");
retry()->times(3)->then(fn() => $socket);
}
#amphp for real-time help.ServerSocket behind a load balancer (e.g., Nginx).Amp\Loop::memoryUsage().| Failure Scenario | Detection Method | Mitigation |
|---|---|---|
| Connection Drops | Socket::isWritable() timeout |
Implement keepalive pings. |
| TLS Handshake Failures | TlsException |
Use ServerTlsContext::withPeerFingerprint(). |
| Backpressure | High memory usage | Add ReadableStream::pause() checks. |
| DNS Resolution Failures | ConnectException |
Use RetrySocketConnector. |
| Fiber Starvation | Slow responses | Limit async operations per request. |
How can I help you explore Laravel packages today?