amphp/socket
Non-blocking, event-driven socket library for PHP using Amp. Provides async TCP/UDP sockets, client/server connections, DNS and TLS support, timeouts, cancellation, and backpressure-friendly streams—ideal for high-concurrency network services and daemons.
Start by installing the package via Composer: composer require amphp/socket. Then, ensure the Amp event loop is initialized — usually via Amp\Loop::run(). The first practical use case is building a simple async TCP server: use ServerListenContext to bind a port and handle incoming connections with accept() inside the loop. For clients, use connect() on a hostname/port and pipe data via ReadableResourceStream/WritableResourceStream. Minimal boilerplate is required: just create the socket context, await connections/data, and let Amp manage non-blocking I/O.
amphp/artifacts or custom framing logic (e.g., length-prefixed messages) to implement custom protocols like WebSocket or Redis-like stacks. Use StreamSocket for bidirectional I/O and on('close') hooks for cleanup.connect() calls in parallel using Amp\Promise\all() or foreach loops with await inside Loop::run(). Leverage cancellation tokens via Amp\Promise\cancel() to abort timed-out connections.SslServerContext or SslClientContext using Amp\Socket\connectTls()/Amp\Socket\listenTls(). Always validate certificates in production (e.g., verify peer CN/SAN via context_options).ReadableResourceStream::read() in a loop, and pause/resume streams via pause()/resume() based on downstream buffer levels to prevent memory overflow.Loop::run() or using blocking I/O (e.g., echo, var_dump, sleep()) inside async code will halt the event loop — use Loop::defer() or Loop::delay() instead.close() or cancel() long-running operations (e.g., accept()) can leak resources — always tie cancel() to request lifecycle or timeouts.connectTls() skips peer verification in some environments — explicitly configure verify_peer/verify_peer_name in the context options for production.ServerListenContext fails with "Address already in use", ensure no previous instance is still running (especially during development) and check SO_REUSEADDR via stream context options.StreamSocket or wrap streams (e.g., with Amp\ByteStream\Filter) to implement custom middleware like logging, rate limiting, or compression — but avoid blocking operations in stream filters.How can I help you explore Laravel packages today?