Rev 915 | Blame | Compare with Previous | Last modification | View Log | RSS feed
# Socket[](https://travis-ci.org/reactphp/socket)Async, streaming plaintext TCP/IP and secure TLS socket server and clientconnections for [ReactPHP](https://reactphp.org/).The socket library provides re-usable interfaces for a socket-layerserver and client based on the [`EventLoop`](https://github.com/reactphp/event-loop)and [`Stream`](https://github.com/reactphp/stream) components.Its server component allows you to build networking servers that accept incomingconnections from networking clients (such as an HTTP server).Its client component allows you to build networking clients that establishoutgoing connections to networking servers (such as an HTTP or database client).This library provides async, streaming means for all of this, so you canhandle multiple concurrent connections without blocking.**Table of Contents*** [Quickstart example](#quickstart-example)* [Connection usage](#connection-usage)* [ConnectionInterface](#connectioninterface)* [getRemoteAddress()](#getremoteaddress)* [getLocalAddress()](#getlocaladdress)* [Server usage](#server-usage)* [ServerInterface](#serverinterface)* [connection event](#connection-event)* [error event](#error-event)* [getAddress()](#getaddress)* [pause()](#pause)* [resume()](#resume)* [close()](#close)* [Server](#server)* [Advanced server usage](#advanced-server-usage)* [TcpServer](#tcpserver)* [SecureServer](#secureserver)* [UnixServer](#unixserver)* [LimitingServer](#limitingserver)* [getConnections()](#getconnections)* [Client usage](#client-usage)* [ConnectorInterface](#connectorinterface)* [connect()](#connect)* [Connector](#connector)* [Advanced client usage](#advanced-client-usage)* [TcpConnector](#tcpconnector)* [HappyEyeBallsConnector](#happyeyeballsconnector)* [DnsConnector](#dnsconnector)* [SecureConnector](#secureconnector)* [TimeoutConnector](#timeoutconnector)* [UnixConnector](#unixconnector)* [FixUriConnector](#fixeduriconnector)* [Install](#install)* [Tests](#tests)* [License](#license)## Quickstart exampleHere is a server that closes the connection if you send it anything:```php$loop = React\EventLoop\Factory::create();$socket = new React\Socket\Server('127.0.0.1:8080', $loop);$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {$connection->write("Hello " . $connection->getRemoteAddress() . "!\n");$connection->write("Welcome to this amazing server!\n");$connection->write("Here's a tip: don't say anything.\n");$connection->on('data', function ($data) use ($connection) {$connection->close();});});$loop->run();```See also the [examples](examples).Here's a client that outputs the output of said server and then attempts tosend it a string:```php$loop = React\EventLoop\Factory::create();$connector = new React\Socket\Connector($loop);$connector->connect('127.0.0.1:8080')->then(function (React\Socket\ConnectionInterface $connection) use ($loop) {$connection->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));$connection->write("Hello World!\n");});$loop->run();```## Connection usage### ConnectionInterfaceThe `ConnectionInterface` is used to represent any incoming and outgoingconnection, such as a normal TCP/IP connection.An incoming or outgoing connection is a duplex stream (both readable andwritable) that implements React's[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).It contains additional properties for the local and remote address (client IP)where this connection has been established to/from.Most commonly, instances implementing this `ConnectionInterface` are emittedby all classes implementing the [`ServerInterface`](#serverinterface) andused by all classes implementing the [`ConnectorInterface`](#connectorinterface).Because the `ConnectionInterface` implements the underlying[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface)you can use any of its events and methods as usual:```php$connection->on('data', function ($chunk) {echo $chunk;});$connection->on('end', function () {echo 'ended';});$connection->on('error', function (Exception $e) {echo 'error: ' . $e->getMessage();});$connection->on('close', function () {echo 'closed';});$connection->write($data);$connection->end($data = null);$connection->close();// …```For more details, see the[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).#### getRemoteAddress()The `getRemoteAddress(): ?string` method returns the full remote address(URI) where this connection has been established with.```php$address = $connection->getRemoteAddress();echo 'Connection with ' . $address . PHP_EOL;```If the remote address can not be determined or is unknown at this time (such asafter the connection has been closed), it MAY return a `NULL` value instead.Otherwise, it will return the full address (URI) as a string value, suchas `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,`unix://example.sock` or `unix:///path/to/example.sock`.Note that individual URI components are application specific and dependon the underlying transport protocol.If this is a TCP/IP based connection and you only want the remote IP, you mayuse something like this:```php$address = $connection->getRemoteAddress();$ip = trim(parse_url($address, PHP_URL_HOST), '[]');echo 'Connection with ' . $ip . PHP_EOL;```#### getLocalAddress()The `getLocalAddress(): ?string` method returns the full local address(URI) where this connection has been established with.```php$address = $connection->getLocalAddress();echo 'Connection with ' . $address . PHP_EOL;```If the local address can not be determined or is unknown at this time (such asafter the connection has been closed), it MAY return a `NULL` value instead.Otherwise, it will return the full address (URI) as a string value, suchas `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443`,`unix://example.sock` or `unix:///path/to/example.sock`.Note that individual URI components are application specific and dependon the underlying transport protocol.This method complements the [`getRemoteAddress()`](#getremoteaddress) method,so they should not be confused.If your `TcpServer` instance is listening on multiple interfaces (e.g. usingthe address `0.0.0.0`), you can use this method to find out which interfaceactually accepted this connection (such as a public or local interface).If your system has multiple interfaces (e.g. a WAN and a LAN interface),you can use this method to find out which interface was actuallyused for this connection.## Server usage### ServerInterfaceThe `ServerInterface` is responsible for providing an interface for acceptingincoming streaming connections, such as a normal TCP/IP connection.Most higher-level components (such as a HTTP server) accept an instanceimplementing this interface to accept incoming streaming connections.This is usually done via dependency injection, so it's fairly simple to actuallyswap this implementation against any other implementation of this interface.This means that you SHOULD typehint against this interface instead of a concreteimplementation of this interface.Besides defining a few methods, this interface also implements the[`EventEmitterInterface`](https://github.com/igorw/evenement)which allows you to react to certain events.#### connection eventThe `connection` event will be emitted whenever a new connection has beenestablished, i.e. a new client connects to this server socket:```php$server->on('connection', function (React\Socket\ConnectionInterface $connection) {echo 'new connection' . PHP_EOL;});```See also the [`ConnectionInterface`](#connectioninterface) for more detailsabout handling the incoming connection.#### error eventThe `error` event will be emitted whenever there's an error accepting a newconnection from a client.```php$server->on('error', function (Exception $e) {echo 'error: ' . $e->getMessage() . PHP_EOL;});```Note that this is not a fatal error event, i.e. the server keeps listening fornew connections even after this event.#### getAddress()The `getAddress(): ?string` method can be used toreturn the full address (URI) this server is currently listening on.```php$address = $server->getAddress();echo 'Server listening on ' . $address . PHP_EOL;```If the address can not be determined or is unknown at this time (such asafter the socket has been closed), it MAY return a `NULL` value instead.Otherwise, it will return the full address (URI) as a string value, suchas `tcp://127.0.0.1:8080`, `tcp://[::1]:80`, `tls://127.0.0.1:443``unix://example.sock` or `unix:///path/to/example.sock`.Note that individual URI components are application specific and dependon the underlying transport protocol.If this is a TCP/IP based server and you only want the local port, you mayuse something like this:```php$address = $server->getAddress();$port = parse_url($address, PHP_URL_PORT);echo 'Server listening on port ' . $port . PHP_EOL;```#### pause()The `pause(): void` method can be used topause accepting new incoming connections.Removes the socket resource from the EventLoop and thus stop acceptingnew connections. Note that the listening socket stays active and is notclosed.This means that new incoming connections will stay pending in theoperating system backlog until its configurable backlog is filled.Once the backlog is filled, the operating system may reject furtherincoming connections until the backlog is drained again by resumingto accept new connections.Once the server is paused, no futher `connection` events SHOULDbe emitted.```php$server->pause();$server->on('connection', assertShouldNeverCalled());```This method is advisory-only, though generally not recommended, theserver MAY continue emitting `connection` events.Unless otherwise noted, a successfully opened server SHOULD NOT startin paused state.You can continue processing events by calling `resume()` again.Note that both methods can be called any number of times, in particularcalling `pause()` more than once SHOULD NOT have any effect.Similarly, calling this after `close()` is a NO-OP.#### resume()The `resume(): void` method can be used toresume accepting new incoming connections.Re-attach the socket resource to the EventLoop after a previous `pause()`.```php$server->pause();$loop->addTimer(1.0, function () use ($server) {$server->resume();});```Note that both methods can be called any number of times, in particularcalling `resume()` without a prior `pause()` SHOULD NOT have any effect.Similarly, calling this after `close()` is a NO-OP.#### close()The `close(): void` method can be used toshut down this listening socket.This will stop listening for new incoming connections on this socket.```phpecho 'Shutting down server socket' . PHP_EOL;$server->close();```Calling this method more than once on the same instance is a NO-OP.### ServerThe `Server` class is the main class in this package that implements the[`ServerInterface`](#serverinterface) and allows you to accept incomingstreaming connections, such as plaintext TCP/IP or secure TLS connection streams.Connections can also be accepted on Unix domain sockets.```php$server = new React\Socket\Server(8080, $loop);```As above, the `$uri` parameter can consist of only a port, in which case theserver will default to listening on the localhost address `127.0.0.1`,which means it will not be reachable from outside of this system.In order to use a random port assignment, you can use the port `0`:```php$server = new React\Socket\Server(0, $loop);$address = $server->getAddress();```In order to change the host the socket is listening on, you can provide an IPaddress through the first parameter provided to the constructor, optionallypreceded by the `tcp://` scheme:```php$server = new React\Socket\Server('192.168.0.1:8080', $loop);```If you want to listen on an IPv6 address, you MUST enclose the host in squarebrackets:```php$server = new React\Socket\Server('[::1]:8080', $loop);```To listen on a Unix domain socket (UDS) path, you MUST prefix the URI with the`unix://` scheme:```php$server = new React\Socket\Server('unix:///tmp/server.sock', $loop);```If the given URI is invalid, does not contain a port, any other scheme or if itcontains a hostname, it will throw an `InvalidArgumentException`:```php// throws InvalidArgumentException due to missing port$server = new React\Socket\Server('127.0.0.1', $loop);```If the given URI appears to be valid, but listening on it fails (such as if portis already in use or port below 1024 may require root access etc.), it willthrow a `RuntimeException`:```php$first = new React\Socket\Server(8080, $loop);// throws RuntimeException because port is already in use$second = new React\Socket\Server(8080, $loop);```> Note that these error conditions may vary depending on your system and/orconfiguration.See the exception message and code for more details about the actual errorcondition.Optionally, you can specify [TCP socket context options](https://www.php.net/manual/en/context.socket.php)for the underlying stream socket resource like this:```php$server = new React\Socket\Server('[::1]:8080', $loop, array('tcp' => array('backlog' => 200,'so_reuseport' => true,'ipv6_v6only' => true)));```> Note that available [socket context options](https://www.php.net/manual/en/context.socket.php),their defaults and effects of changing these may vary depending on your systemand/or PHP version.Passing unknown context options has no effect.The `backlog` context option defaults to `511` unless given explicitly.For BC reasons, you can also pass the TCP socket context options as a simplearray without wrapping this in another array under the `tcp` key.You can start a secure TLS (formerly known as SSL) server by simply prependingthe `tls://` URI scheme.Internally, it will wait for plaintext TCP/IP connections and then performs aTLS handshake for each connection.It thus requires valid [TLS context options](https://www.php.net/manual/en/context.ssl.php),which in its most basic form may look something like this if you're using aPEM encoded certificate file:```php$server = new React\Socket\Server('tls://127.0.0.1:8080', $loop, array('tls' => array('local_cert' => 'server.pem')));```> Note that the certificate file will not be loaded on instantiation but when anincoming connection initializes its TLS context.This implies that any invalid certificate file paths or contents will only causean `error` event at a later time.If your private key is encrypted with a passphrase, you have to specify itlike this:```php$server = new React\Socket\Server('tls://127.0.0.1:8000', $loop, array('tls' => array('local_cert' => 'server.pem','passphrase' => 'secret')));```By default, this server supports TLSv1.0+ and excludes support for legacySSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version youwant to negotiate with the remote side:```php$server = new React\Socket\Server('tls://127.0.0.1:8000', $loop, array('tls' => array('local_cert' => 'server.pem','crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER)));```> Note that available [TLS context options](https://www.php.net/manual/en/context.ssl.php),their defaults and effects of changing these may vary depending on your systemand/or PHP version.The outer context array allows you to also use `tcp` (and possibly more)context options at the same time.Passing unknown context options has no effect.If you do not use the `tls://` scheme, then passing `tls` context optionshas no effect.Whenever a client connects, it will emit a `connection` event with a connectioninstance implementing [`ConnectionInterface`](#connectioninterface):```php$server->on('connection', function (React\Socket\ConnectionInterface $connection) {echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;$connection->write('hello there!' . PHP_EOL);…});```See also the [`ServerInterface`](#serverinterface) for more details.> Note that the `Server` class is a concrete implementation for TCP/IP sockets.If you want to typehint in your higher-level protocol implementation, you SHOULDuse the generic [`ServerInterface`](#serverinterface) instead.### Advanced server usage#### TcpServerThe `TcpServer` class implements the [`ServerInterface`](#serverinterface) andis responsible for accepting plaintext TCP/IP connections.```php$server = new React\Socket\TcpServer(8080, $loop);```As above, the `$uri` parameter can consist of only a port, in which case theserver will default to listening on the localhost address `127.0.0.1`,which means it will not be reachable from outside of this system.In order to use a random port assignment, you can use the port `0`:```php$server = new React\Socket\TcpServer(0, $loop);$address = $server->getAddress();```In order to change the host the socket is listening on, you can provide an IPaddress through the first parameter provided to the constructor, optionallypreceded by the `tcp://` scheme:```php$server = new React\Socket\TcpServer('192.168.0.1:8080', $loop);```If you want to listen on an IPv6 address, you MUST enclose the host in squarebrackets:```php$server = new React\Socket\TcpServer('[::1]:8080', $loop);```If the given URI is invalid, does not contain a port, any other scheme or if itcontains a hostname, it will throw an `InvalidArgumentException`:```php// throws InvalidArgumentException due to missing port$server = new React\Socket\TcpServer('127.0.0.1', $loop);```If the given URI appears to be valid, but listening on it fails (such as if portis already in use or port below 1024 may require root access etc.), it willthrow a `RuntimeException`:```php$first = new React\Socket\TcpServer(8080, $loop);// throws RuntimeException because port is already in use$second = new React\Socket\TcpServer(8080, $loop);```> Note that these error conditions may vary depending on your system and/orconfiguration.See the exception message and code for more details about the actual errorcondition.Optionally, you can specify [socket context options](https://www.php.net/manual/en/context.socket.php)for the underlying stream socket resource like this:```php$server = new React\Socket\TcpServer('[::1]:8080', $loop, array('backlog' => 200,'so_reuseport' => true,'ipv6_v6only' => true));```> Note that available [socket context options](https://www.php.net/manual/en/context.socket.php),their defaults and effects of changing these may vary depending on your systemand/or PHP version.Passing unknown context options has no effect.The `backlog` context option defaults to `511` unless given explicitly.Whenever a client connects, it will emit a `connection` event with a connectioninstance implementing [`ConnectionInterface`](#connectioninterface):```php$server->on('connection', function (React\Socket\ConnectionInterface $connection) {echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;$connection->write('hello there!' . PHP_EOL);…});```See also the [`ServerInterface`](#serverinterface) for more details.#### SecureServerThe `SecureServer` class implements the [`ServerInterface`](#serverinterface)and is responsible for providing a secure TLS (formerly known as SSL) server.It does so by wrapping a [`TcpServer`](#tcpserver) instance which waits for plaintextTCP/IP connections and then performs a TLS handshake for each connection.It thus requires valid [TLS context options](https://www.php.net/manual/en/context.ssl.php),which in its most basic form may look something like this if you're using aPEM encoded certificate file:```php$server = new React\Socket\TcpServer(8000, $loop);$server = new React\Socket\SecureServer($server, $loop, array('local_cert' => 'server.pem'));```> Note that the certificate file will not be loaded on instantiation but when anincoming connection initializes its TLS context.This implies that any invalid certificate file paths or contents will only causean `error` event at a later time.If your private key is encrypted with a passphrase, you have to specify itlike this:```php$server = new React\Socket\TcpServer(8000, $loop);$server = new React\Socket\SecureServer($server, $loop, array('local_cert' => 'server.pem','passphrase' => 'secret'));```By default, this server supports TLSv1.0+ and excludes support for legacySSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version youwant to negotiate with the remote side:```php$server = new React\Socket\TcpServer(8000, $loop);$server = new React\Socket\SecureServer($server, $loop, array('local_cert' => 'server.pem','crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER));```> Note that available [TLS context options](https://www.php.net/manual/en/context.ssl.php),their defaults and effects of changing these may vary depending on your systemand/or PHP version.Passing unknown context options has no effect.Whenever a client completes the TLS handshake, it will emit a `connection` eventwith a connection instance implementing [`ConnectionInterface`](#connectioninterface):```php$server->on('connection', function (React\Socket\ConnectionInterface $connection) {echo 'Secure connection from' . $connection->getRemoteAddress() . PHP_EOL;$connection->write('hello there!' . PHP_EOL);…});```Whenever a client fails to perform a successful TLS handshake, it will emit an`error` event and then close the underlying TCP/IP connection:```php$server->on('error', function (Exception $e) {echo 'Error' . $e->getMessage() . PHP_EOL;});```See also the [`ServerInterface`](#serverinterface) for more details.Note that the `SecureServer` class is a concrete implementation for TLS sockets.If you want to typehint in your higher-level protocol implementation, you SHOULDuse the generic [`ServerInterface`](#serverinterface) instead.> Advanced usage: Despite allowing any `ServerInterface` as first parameter,you SHOULD pass a `TcpServer` instance as first parameter, unless youknow what you're doing.Internally, the `SecureServer` has to set the required TLS context options onthe underlying stream resources.These resources are not exposed through any of the interfaces defined in thispackage, but only through the internal `Connection` class.The `TcpServer` class is guaranteed to emit connections that implementthe `ConnectionInterface` and uses the internal `Connection` class in order toexpose these underlying resources.If you use a custom `ServerInterface` and its `connection` event does notmeet this requirement, the `SecureServer` will emit an `error` event andthen close the underlying connection.#### UnixServerThe `UnixServer` class implements the [`ServerInterface`](#serverinterface) andis responsible for accepting connections on Unix domain sockets (UDS).```php$server = new React\Socket\UnixServer('/tmp/server.sock', $loop);```As above, the `$uri` parameter can consist of only a socket path or socket pathprefixed by the `unix://` scheme.If the given URI appears to be valid, but listening on it fails (such as if thesocket is already in use or the file not accessible etc.), it will throw a`RuntimeException`:```php$first = new React\Socket\UnixServer('/tmp/same.sock', $loop);// throws RuntimeException because socket is already in use$second = new React\Socket\UnixServer('/tmp/same.sock', $loop);```> Note that these error conditions may vary depending on your system and/orconfiguration.In particular, Zend PHP does only report "Unknown error" when the UDS pathalready exists and can not be bound. You may want to check `is_file()` on thegiven UDS path to report a more user-friendly error message in this case.See the exception message and code for more details about the actual errorcondition.Whenever a client connects, it will emit a `connection` event with a connectioninstance implementing [`ConnectionInterface`](#connectioninterface):```php$server->on('connection', function (React\Socket\ConnectionInterface $connection) {echo 'New connection' . PHP_EOL;$connection->write('hello there!' . PHP_EOL);…});```See also the [`ServerInterface`](#serverinterface) for more details.#### LimitingServerThe `LimitingServer` decorator wraps a given `ServerInterface` and is responsiblefor limiting and keeping track of open connections to this server instance.Whenever the underlying server emits a `connection` event, it will check itslimits and then either- keep track of this connection by adding it to the list ofopen connections and then forward the `connection` event- or reject (close) the connection when its limits are exceeded and willforward an `error` event instead.Whenever a connection closes, it will remove this connection from the list ofopen connections.```php$server = new React\Socket\LimitingServer($server, 100);$server->on('connection', function (React\Socket\ConnectionInterface $connection) {$connection->write('hello there!' . PHP_EOL);…});```See also the [second example](examples) for more details.You have to pass a maximum number of open connections to ensurethe server will automatically reject (close) connections once this limitis exceeded. In this case, it will emit an `error` event to inform aboutthis and no `connection` event will be emitted.```php$server = new React\Socket\LimitingServer($server, 100);$server->on('connection', function (React\Socket\ConnectionInterface $connection) {$connection->write('hello there!' . PHP_EOL);…});```You MAY pass a `null` limit in order to put no limit on the number ofopen connections and keep accepting new connection until you run out ofoperating system resources (such as open file handles). This may beuseful if you do not want to take care of applying a limit but still wantto use the `getConnections()` method.You can optionally configure the server to pause accepting newconnections once the connection limit is reached. In this case, it willpause the underlying server and no longer process any new connections atall, thus also no longer closing any excessive connections.The underlying operating system is responsible for keeping a backlog ofpending connections until its limit is reached, at which point it willstart rejecting further connections.Once the server is below the connection limit, it will continue consumingconnections from the backlog and will process any outstanding data oneach connection.This mode may be useful for some protocols that are designed to wait fora response message (such as HTTP), but may be less useful for otherprotocols that demand immediate responses (such as a "welcome" message inan interactive chat).```php$server = new React\Socket\LimitingServer($server, 100, true);$server->on('connection', function (React\Socket\ConnectionInterface $connection) {$connection->write('hello there!' . PHP_EOL);…});```##### getConnections()The `getConnections(): ConnectionInterface[]` method can be used toreturn an array with all currently active connections.```phpforeach ($server->getConnection() as $connection) {$connection->write('Hi!');}```## Client usage### ConnectorInterfaceThe `ConnectorInterface` is responsible for providing an interface forestablishing streaming connections, such as a normal TCP/IP connection.This is the main interface defined in this package and it is used throughoutReact's vast ecosystem.Most higher-level components (such as HTTP, database or other networkingservice clients) accept an instance implementing this interface to create theirTCP/IP connection to the underlying networking service.This is usually done via dependency injection, so it's fairly simple to actuallyswap this implementation against any other implementation of this interface.The interface only offers a single method:#### connect()The `connect(string $uri): PromiseInterface<ConnectionInterface,Exception>` methodcan be used to create a streaming connection to the given remote address.It returns a [Promise](https://github.com/reactphp/promise) which eitherfulfills with a stream implementing [`ConnectionInterface`](#connectioninterface)on success or rejects with an `Exception` if the connection is not successful:```php$connector->connect('google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {// connection successfully established},function (Exception $error) {// failed to connect due to $error});```See also [`ConnectionInterface`](#connectioninterface) for more details.The returned Promise MUST be implemented in such a way that it can becancelled when it is still pending. Cancelling a pending promise MUSTreject its value with an `Exception`. It SHOULD clean up any underlyingresources and references as applicable:```php$promise = $connector->connect($uri);$promise->cancel();```### ConnectorThe `Connector` class is the main class in this package that implements the[`ConnectorInterface`](#connectorinterface) and allows you to create streaming connections.You can use this connector to create any kind of streaming connections, suchas plaintext TCP/IP, secure TLS or local Unix connection streams.It binds to the main event loop and can be used like this:```php$loop = React\EventLoop\Factory::create();$connector = new React\Socket\Connector($loop);$connector->connect($uri)->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});$loop->run();```In order to create a plaintext TCP/IP connection, you can simply pass a hostand port combination like this:```php$connector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```> If you do no specify a URI scheme in the destination URI, it will assume`tcp://` as a default and establish a plaintext TCP/IP connection.Note that TCP/IP connections require a host and port part in the destinationURI like above, all other URI components are optional.In order to create a secure TLS connection, you can use the `tls://` URI schemelike this:```php$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```In order to create a local Unix domain socket connection, you can use the`unix://` URI scheme like this:```php$connector->connect('unix:///tmp/demo.sock')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```> The [`getRemoteAddress()`](#getremoteaddress) method will return the targetUnix domain socket (UDS) path as given to the `connect()` method, includingthe `unix://` scheme, for example `unix:///tmp/demo.sock`.The [`getLocalAddress()`](#getlocaladdress) method will most likely return a`null` value as this value is not applicable to UDS connections here.Under the hood, the `Connector` is implemented as a *higher-level facade*for the lower-level connectors implemented in this package. This means italso shares all of their features and implementation details.If you want to typehint in your higher-level protocol implementation, you SHOULDuse the generic [`ConnectorInterface`](#connectorinterface) instead.As of `v1.4.0`, the `Connector` class defaults to using the[happy eyeballs algorithm](https://en.wikipedia.org/wiki/Happy_Eyeballs) toautomatically connect over IPv4 or IPv6 when a hostname is given.This automatically attempts to connect using both IPv4 and IPv6 at the same time(preferring IPv6), thus avoiding the usual problems faced by users with imperfectIPv6 connections or setups.If you want to revert to the old behavior of only doing an IPv4 lookup andonly attempt a single IPv4 connection, you can set up the `Connector` like this:```php$connector = new React\Socket\Connector($loop, array('happy_eyeballs' => false));```Similarly, you can also affect the default DNS behavior as follows.The `Connector` class will try to detect your system DNS settings (and usesGoogle's public DNS server `8.8.8.8` as a fallback if unable to determine yoursystem settings) to resolve all public hostnames into underlying IP addresses bydefault.If you explicitly want to use a custom DNS server (such as a local DNS relay ora company wide DNS server), you can set up the `Connector` like this:```php$connector = new React\Socket\Connector($loop, array('dns' => '127.0.1.1'));$connector->connect('localhost:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```If you do not want to use a DNS resolver at all and want to connect to IPaddresses only, you can also set up your `Connector` like this:```php$connector = new React\Socket\Connector($loop, array('dns' => false));$connector->connect('127.0.0.1:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```Advanced: If you need a custom DNS `React\Dns\Resolver\ResolverInterface` instance, youcan also set up your `Connector` like this:```php$dnsResolverFactory = new React\Dns\Resolver\Factory();$resolver = $dnsResolverFactory->createCached('127.0.1.1', $loop);$connector = new React\Socket\Connector($loop, array('dns' => $resolver));$connector->connect('localhost:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```By default, the `tcp://` and `tls://` URI schemes will use timeout value thatrespects your `default_socket_timeout` ini setting (which defaults to 60s).If you want a custom timeout value, you can simply pass this like this:```php$connector = new React\Socket\Connector($loop, array('timeout' => 10.0));```Similarly, if you do not want to apply a timeout at all and let the operatingsystem handle this, you can pass a boolean flag like this:```php$connector = new React\Socket\Connector($loop, array('timeout' => false));```By default, the `Connector` supports the `tcp://`, `tls://` and `unix://`URI schemes. If you want to explicitly prohibit any of these, you can simplypass boolean flags like this:```php// only allow secure TLS connections$connector = new React\Socket\Connector($loop, array('tcp' => false,'tls' => true,'unix' => false,));$connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```The `tcp://` and `tls://` also accept additional context options passed tothe underlying connectors.If you want to explicitly pass additional context options, you can simplypass arrays of context options like this:```php// allow insecure TLS connections$connector = new React\Socket\Connector($loop, array('tcp' => array('bindto' => '192.168.0.1:0'),'tls' => array('verify_peer' => false,'verify_peer_name' => false),));$connector->connect('tls://localhost:443')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```By default, this connector supports TLSv1.0+ and excludes support for legacySSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version youwant to negotiate with the remote side:```php$connector = new React\Socket\Connector($loop, array('tls' => array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)));```> For more details about context options, please refer to the PHP documentationabout [socket context options](https://www.php.net/manual/en/context.socket.php)and [SSL context options](https://www.php.net/manual/en/context.ssl.php).Advanced: By default, the `Connector` supports the `tcp://`, `tls://` and`unix://` URI schemes.For this, it sets up the required connector classes automatically.If you want to explicitly pass custom connectors for any of these, you can simplypass an instance implementing the `ConnectorInterface` like this:```php$dnsResolverFactory = new React\Dns\Resolver\Factory();$resolver = $dnsResolverFactory->createCached('127.0.1.1', $loop);$tcp = new React\Socket\HappyEyeBallsConnector($loop, new React\Socket\TcpConnector($loop), $resolver);$tls = new React\Socket\SecureConnector($tcp, $loop);$unix = new React\Socket\UnixConnector($loop);$connector = new React\Socket\Connector($loop, array('tcp' => $tcp,'tls' => $tls,'unix' => $unix,'dns' => false,'timeout' => false,));$connector->connect('google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});```> Internally, the `tcp://` connector will always be wrapped by the DNS resolver,unless you disable DNS like in the above example. In this case, the `tcp://`connector receives the actual hostname instead of only the resolved IP addressand is thus responsible for performing the lookup.Internally, the automatically created `tls://` connector will always wrap theunderlying `tcp://` connector for establishing the underlying plaintextTCP/IP connection before enabling secure TLS mode. If you want to use a customunderlying `tcp://` connector for secure TLS connections only, you mayexplicitly pass a `tls://` connector like above instead.Internally, the `tcp://` and `tls://` connectors will always be wrapped by`TimeoutConnector`, unless you disable timeouts like in the above example.### Advanced client usage#### TcpConnectorThe `TcpConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to create plaintextTCP/IP connections to any IP-port-combination:```php$tcpConnector = new React\Socket\TcpConnector($loop);$tcpConnector->connect('127.0.0.1:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});$loop->run();```See also the [examples](examples).Pending connection attempts can be cancelled by cancelling its pending promise like so:```php$promise = $tcpConnector->connect('127.0.0.1:80');$promise->cancel();```Calling `cancel()` on a pending promise will close the underlying socketresource, thus cancelling the pending TCP/IP connection, and reject theresulting promise.You can optionally pass additional[socket context options](https://www.php.net/manual/en/context.socket.php)to the constructor like this:```php$tcpConnector = new React\Socket\TcpConnector($loop, array('bindto' => '192.168.0.1:0'));```Note that this class only allows you to connect to IP-port-combinations.If the given URI is invalid, does not contain a valid IP address and portor contains any other scheme, it will reject with an`InvalidArgumentException`:If the given URI appears to be valid, but connecting to it fails (such as ifthe remote host rejects the connection etc.), it will reject with a`RuntimeException`.If you want to connect to hostname-port-combinations, see also the following chapter.> Advanced usage: Internally, the `TcpConnector` allocates an empty *context*resource for each stream resource.If the destination URI contains a `hostname` query parameter, its value willbe used to set up the TLS peer name.This is used by the `SecureConnector` and `DnsConnector` to verify the peername and can also be used if you want a custom TLS peer name.#### HappyEyeBallsConnectorThe `HappyEyeBallsConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to create plaintextTCP/IP connections to any hostname-port-combination. Internally it implements thehappy eyeballs algorithm from [`RFC6555`](https://tools.ietf.org/html/rfc6555) and[`RFC8305`](https://tools.ietf.org/html/rfc8305) to support IPv6 and IPv4 hostnames.It does so by decorating a given `TcpConnector` instance so that it firstlooks up the given domain name via DNS (if applicable) and then establishes theunderlying TCP/IP connection to the resolved target IP address.Make sure to set up your DNS resolver and underlying TCP connector like this:```php$dnsResolverFactory = new React\Dns\Resolver\Factory();$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);$dnsConnector = new React\Socket\HappyEyeBallsConnector($loop, $tcpConnector, $dns);$dnsConnector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});$loop->run();```See also the [examples](examples).Pending connection attempts can be cancelled by cancelling its pending promise like so:```php$promise = $dnsConnector->connect('www.google.com:80');$promise->cancel();```Calling `cancel()` on a pending promise will cancel the underlying DNS lookupsand/or the underlying TCP/IP connection(s) and reject the resulting promise.> Advanced usage: Internally, the `HappyEyeBallsConnector` relies on a `Resolver` tolook up the IP addresses for the given hostname.It will then replace the hostname in the destination URI with this IP's andappend a `hostname` query parameter and pass this updated URI to the underlyingconnector.The Happy Eye Balls algorithm describes looking the IPv6 and IPv4 address forthe given hostname so this connector sends out two DNS lookups for the A andAAAA records. It then uses all IP addresses (both v6 and v4) and tries toconnect to all of them with a 50ms interval in between. Alterating between IPv6and IPv4 addresses. When a connection is established all the other DNS lookupsand connection attempts are cancelled.#### DnsConnectorThe `DnsConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to create plaintextTCP/IP connections to any hostname-port-combination.It does so by decorating a given `TcpConnector` instance so that it firstlooks up the given domain name via DNS (if applicable) and then establishes theunderlying TCP/IP connection to the resolved target IP address.Make sure to set up your DNS resolver and underlying TCP connector like this:```php$dnsResolverFactory = new React\Dns\Resolver\Factory();$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);$dnsConnector = new React\Socket\DnsConnector($tcpConnector, $dns);$dnsConnector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write('...');$connection->end();});$loop->run();```See also the [examples](examples).Pending connection attempts can be cancelled by cancelling its pending promise like so:```php$promise = $dnsConnector->connect('www.google.com:80');$promise->cancel();```Calling `cancel()` on a pending promise will cancel the underlying DNS lookupand/or the underlying TCP/IP connection and reject the resulting promise.> Advanced usage: Internally, the `DnsConnector` relies on a `React\Dns\Resolver\ResolverInterface`to look up the IP address for the given hostname.It will then replace the hostname in the destination URI with this IP andappend a `hostname` query parameter and pass this updated URI to the underlyingconnector.The underlying connector is thus responsible for creating a connection to thetarget IP address, while this query parameter can be used to check the originalhostname and is used by the `TcpConnector` to set up the TLS peer name.If a `hostname` is given explicitly, this query parameter will not be modified,which can be useful if you want a custom TLS peer name.#### SecureConnectorThe `SecureConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to create secureTLS (formerly known as SSL) connections to any hostname-port-combination.It does so by decorating a given `DnsConnector` instance so that it firstcreates a plaintext TCP/IP connection and then enables TLS encryption on thisstream.```php$secureConnector = new React\Socket\SecureConnector($dnsConnector, $loop);$secureConnector->connect('www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");...});$loop->run();```See also the [examples](examples).Pending connection attempts can be cancelled by cancelling its pending promise like so:```php$promise = $secureConnector->connect('www.google.com:443');$promise->cancel();```Calling `cancel()` on a pending promise will cancel the underlying TCP/IPconnection and/or the SSL/TLS negotiation and reject the resulting promise.You can optionally pass additional[SSL context options](https://www.php.net/manual/en/context.ssl.php)to the constructor like this:```php$secureConnector = new React\Socket\SecureConnector($dnsConnector, $loop, array('verify_peer' => false,'verify_peer_name' => false));```By default, this connector supports TLSv1.0+ and excludes support for legacySSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version youwant to negotiate with the remote side:```php$secureConnector = new React\Socket\SecureConnector($dnsConnector, $loop, array('crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT));```> Advanced usage: Internally, the `SecureConnector` relies on setting up therequired *context options* on the underlying stream resource.It should therefor be used with a `TcpConnector` somewhere in the connectorstack so that it can allocate an empty *context* resource for each streamresource and verify the peer name.Failing to do so may result in a TLS peer name mismatch error or some hard totrace race conditions, because all stream resources will use a single, shared*default context* resource otherwise.#### TimeoutConnectorThe `TimeoutConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to add timeouthandling to any existing connector instance.It does so by decorating any given [`ConnectorInterface`](#connectorinterface)instance and starting a timer that will automatically reject and abort anyunderlying connection attempt if it takes too long.```php$timeoutConnector = new React\Socket\TimeoutConnector($connector, 3.0, $loop);$timeoutConnector->connect('google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {// connection succeeded within 3.0 seconds});```See also any of the [examples](examples).Pending connection attempts can be cancelled by cancelling its pending promise like so:```php$promise = $timeoutConnector->connect('google.com:80');$promise->cancel();```Calling `cancel()` on a pending promise will cancel the underlying connectionattempt, abort the timer and reject the resulting promise.#### UnixConnectorThe `UnixConnector` class implements the[`ConnectorInterface`](#connectorinterface) and allows you to connect toUnix domain socket (UDS) paths like this:```php$connector = new React\Socket\UnixConnector($loop);$connector->connect('/tmp/demo.sock')->then(function (React\Socket\ConnectionInterface $connection) {$connection->write("HELLO\n");});$loop->run();```Connecting to Unix domain sockets is an atomic operation, i.e. its promise willsettle (either resolve or reject) immediately.As such, calling `cancel()` on the resulting promise has no effect.> The [`getRemoteAddress()`](#getremoteaddress) method will return the targetUnix domain socket (UDS) path as given to the `connect()` method, prependedwith the `unix://` scheme, for example `unix:///tmp/demo.sock`.The [`getLocalAddress()`](#getlocaladdress) method will most likely return a`null` value as this value is not applicable to UDS connections here.#### FixedUriConnectorThe `FixedUriConnector` class implements the[`ConnectorInterface`](#connectorinterface) and decorates an existing Connectorto always use a fixed, preconfigured URI.This can be useful for consumers that do not support certain URIs, such aswhen you want to explicitly connect to a Unix domain socket (UDS) pathinstead of connecting to a default address assumed by an higher-level API:```php$connector = new React\Socket\FixedUriConnector('unix:///var/run/docker.sock',new React\Socket\UnixConnector($loop));// destination will be ignored, actually connects to Unix domain socket$promise = $connector->connect('localhost:80');```## InstallThe recommended way to install this library is [through Composer](https://getcomposer.org).[New to Composer?](https://getcomposer.org/doc/00-intro.md)This project follows [SemVer](https://semver.org/).This will install the latest supported version:```bash$ composer require react/socket:^1.6```See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.This project aims to run on any platform and thus does not require any PHPextensions and supports running on legacy PHP 5.3 through current PHP 7+ and HHVM.It's *highly recommended to use PHP 7+* for this project, partly due to its vastperformance improvements and partly because legacy PHP versions require severalworkarounds as described below.Secure TLS connections received some major upgrades starting with PHP 5.6, withthe defaults now being more secure, while older versions required explicitcontext options.This library does not take responsibility over these context options, so it'sup to consumers of this library to take care of setting appropriate contextoptions as described above.PHP < 7.3.3 (and PHP < 7.2.15) suffers from a bug where feof() mightblock with 100% CPU usage on fragmented TLS records.We try to work around this by always consuming the complete receivebuffer at once to avoid stale data in TLS buffers. This is known towork around high CPU usage for well-behaving peers, but this maycause very large data chunks for high throughput scenarios. The buggybehavior can still be triggered due to network I/O buffers ormalicious peers on affected versions, upgrading is highly recommended.PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing bigchunks of data over TLS streams at once.We try to work around this by limiting the write chunk size to 8192bytes for older PHP versions only.This is only a work-around and has a noticable performance penalty onaffected versions.This project also supports running on HHVM.Note that really old HHVM < 3.8 does not support secure TLS connections, as itlacks the required `stream_socket_enable_crypto()` function.As such, trying to create a secure TLS connections on affected versions willreturn a rejected promise instead.This issue is also covered by our test suite, which will skip related testson affected versions.## TestsTo run the test suite, you first need to clone this repo and then install alldependencies [through Composer](https://getcomposer.org):```bash$ composer install```To run the test suite, go to the project root and run:```bash$ php vendor/bin/phpunit```The test suite also contains a number of functional integration tests that relyon a stable internet connection.If you do not want to run these, they can simply be skipped like this:```bash$ php vendor/bin/phpunit --exclude-group internet```## LicenseMIT, see [LICENSE file](LICENSE).