Download the PHP package werkint/reactphp-socket-client without Composer
On this page you can find all versions of the php package werkint/reactphp-socket-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package reactphp-socket-client
SocketClient Component
Async Connector to open TCP/IP and SSL/TLS based connections.
Introduction
Think of this library as an async version of
fsockopen()
or
stream_socket_client()
.
Before you can actually transmit and receive data to/from a remote server, you have to establish a connection to the remote end. Establishing this connection through the internet/network takes some time as it requires several steps in order to complete:
- Resolve remote target hostname via DNS (+cache)
- Complete TCP handshake (2 roundtrips) with remote target IP:port
- Optionally enable SSL/TLS on the new resulting connection
Usage
In order to use this project, you'll need the following react boilerplate code to initialize the main loop.
ConnectorInterface
The ConnectorInterface
is responsible for providing an interface for
establishing streaming connections, such as a normal TCP/IP connection.
This is the main interface defined in this package and it is used throughout React's vast ecosystem.
Most higher-level components (such as HTTP, database or other networking service clients) accept an instance implementing this interface to create their TCP/IP connection to the underlying networking service. This is usually done via dependency injection, so it's fairly simple to actually swap this implementation against any other implementation of this interface.
The interface only offers a single method:
create()
The create(string $host, int $port): PromiseInterface<Stream, Exception>
method
can be used to establish a streaming connection.
It returns a Promise which either
fulfills with a Stream or
rejects with an Exception
:
The returned Promise SHOULD be implemented in such a way that it can be
cancelled when it is still pending. Cancelling a pending promise SHOULD
reject its value with an Exception
. It SHOULD clean up any underlying
resources and references as applicable:
Async TCP/IP connections
The React\SocketClient\TcpConnector
class implements the
ConnectorInterface
and allows you to create plaintext
TCP/IP connections to any IP-port-combination:
See also the first example.
Pending connection attempts can be cancelled by cancelling its pending promise like so:
Calling cancel()
on a pending promise will close the underlying socket
resource, thus cancelling the pending TCP/IP connection, and reject the
resulting promise.
You can optionally pass additional socket context options to the constructor like this:
Note that this class only allows you to connect to IP-port-combinations. If you want to connect to hostname-port-combinations, see also the following chapter.
DNS resolution
The DnsConnector
class implements the
ConnectorInterface
and allows you to create plaintext
TCP/IP connections to any hostname-port-combination.
It does so by decorating a given TcpConnector
instance so that it first
looks up the given domain name via DNS (if applicable) and then establishes the
underlying TCP/IP connection to the resolved target IP address.
Make sure to set up your DNS resolver and underlying TCP connector like this:
See also the first example.
Pending connection attempts can be cancelled by cancelling its pending promise like so:
Calling cancel()
on a pending promise will cancel the underlying DNS lookup
and/or the underlying TCP/IP connection and reject the resulting promise.
The legacy Connector
class can be used for backwards-compatiblity reasons.
It works very much like the newer DnsConnector
but instead has to be
set up like this:
Async SSL/TLS connections
The SecureConnector
class implements the
ConnectorInterface
and allows you to create secure
TLS (formerly known as SSL) connections to any hostname-port-combination.
It does so by decorating a given DnsConnector
instance so that it first
creates a plaintext TCP/IP connection and then enables TLS encryption on this
stream.
See also the second example.
Pending connection attempts can be cancelled by cancelling its pending promise like so:
Calling cancel()
on a pending promise will cancel the underlying TCP/IP
connection and/or the SSL/TLS negonation and reject the resulting promise.
You can optionally pass additional SSL context options to the constructor like this:
Advanced usage: Internally, the
SecureConnector
has to set the required context options on the underlying stream resource. It should therefor be used with aTcpConnector
somewhere in the connector stack so that it can allocate an empty context resource for each stream resource. Failing to do so may result in some hard to trace race conditions, because all stream resources will use a single, shared default context resource otherwise.
Connection timeouts
The TimeoutConnector
class implements the
ConnectorInterface
and allows you to add timeout
handling to any existing connector instance.
It does so by decorating any given ConnectorInterface
instance and starting a timer that will automatically reject and abort any
underlying connection attempt if it takes too long.
See also any of the examples.
Pending connection attempts can be cancelled by cancelling its pending promise like so:
Calling cancel()
on a pending promise will cancel the underlying connection
attempt, abort the timer and reject the resulting promise.
Unix domain sockets
The UnixConnector
class implements the
ConnectorInterface
and allows you to connect to
Unix domain socket (UDS) paths like this:
Connecting to Unix domain sockets is an atomic operation, i.e. its promise will
settle (either resolve or reject) immediately.
As such, calling cancel()
on the resulting promise has no effect.
Install
The recommended way to install this library is through Composer. New to Composer?
This will install the latest supported version:
More details about version upgrades can be found in the CHANGELOG.
Tests
To run the test suite, you need PHPUnit. Go to the project root and run:
The test suite also contains some optional integration tests which operate on a TCP/IP socket server and an optional TLS/SSL terminating proxy in front of it. The underlying TCP/IP socket server will be started automatically, whereas the TLS/SSL terminating proxy has to be started and enabled like this:
See also the Travis configuration for details on how to set up
the TLS/SSL terminating proxy and the required certificate file (stunnel.pem
)
if you're unsure.
All versions of reactphp-socket-client with dependencies
react/dns Version 0.4.*|0.3.*
react/event-loop Version 0.4.*|0.3.*
react/stream Version ^0.4.5
react/promise Version ^2.1 || ^1.2
react/promise-timer Version ~1.0