react/socket
Async TCP/IP and TLS socket servers and clients for ReactPHP. Provides streaming, non-blocking connections built on EventLoop and Stream, with reusable interfaces and helpers for TCP, Unix sockets, secure servers, and connectors.
Start by installing the package via Composer: composer require react/socket. The SocketServer class is the primary entry point for building socket servers — pass a bound address like '127.0.0.1:8080' or '0.0.0.0:8080' to listen publicly. Use the connection event to handle incoming clients, receiving a ConnectionInterface instance that supports standard async streaming operations (write, end, close, pipe, and events like data, end, error, close). For clients, instantiate Connector and call connect('host:port') to get a promise resolving to a ConnectionInterface. Check the examples/ directory for minimal, working demos.
SocketServer in LimitingServer to restrict simultaneous connections, using getConnections() for introspection.SecureServer (or TLS URLs like tls://0.0.0.0:443) with stream_context_create() options for certificates.DnsConnector before TcpConnector in the Connector pipeline to enforce DNS validation or fallback.ConnectionInterface’s pipe() to relay data streams (e.g., proxy servers), or use buffering strategies with ReadableStreamInterface / WritableStreamInterface interfaces.EventLoop to schedule pauses/resumes (pause()/resume()) for traffic shaping or maintenance windows.HappyEyeBallsConnector for IPv4/IPv6 dual-stack fallback, and wrap with TimeoutConnector to avoid hanging on DNS resolution or connection delays.'[::1]:8080'); missing brackets cause InvalidArgumentException.SO_REUSEADDR via stream_context_create(['socket' => ['so_reuseaddr' => true]]) and pass as context to avoid Address already in use errors during rapid restarts.error event fires for non-fatal issues (e.g., failed connection attempts), not server shutdown — always attach handlers to avoid silent failures.SocketServer or Connector, pass an optional stream_context to configure TLS (e.g., verify peers, SNI), bind to specific interfaces, or tweak timeouts.unix://; ensure the process has read/write permissions on the socket file/directory.SecureConnector and SecureServer skip peer verification for dev safety. Always enable verification (verify_peer => true) in production and provide cafile or capath.ServerInterface/ConnectorInterface in your own APIs to enable easy mocking, swapping, and future upgrades (e.g., v3).How can I help you explore Laravel packages today?