react/socket
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
evenement/evenement | v3.0.2 | 4.6 kB | MIT | prod | |
react/dns | v1.12.0 | - | MIT | prod | |
react/event-loop | v1.5.0 | 39.55 kB | MIT | prod | |
react/promise | v3.1.0 | - | MIT | prod | |
react/stream | v1.3.0 | 35.67 kB | MIT | prod |
React/Socket is a library for ReactPHP that provides async, streaming plaintext TCP/IP and secure TLS socket server and client connections. It is designed to simplify the process of working with networking servers and clients. With its server component, you can build networking servers that accept incoming connections from networking clients such as an HTTP server. On the other hand, its client component allows you to build networking clients that establish outgoing connections to networking servers, like an HTTP or database client.
Using react/socket is straightforward. It can be included in your project via composer. Here is a quick use case of a server that closes the connection if you send it anything:
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$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();
});
});
And here's a client that outputs the output of said server and then attempts to send it a string:
$connector = new React\Socket\Connector();
$connector->connect('127.0.0.1:8080')->then(function (React\Socket\ConnectionInterface $connection) {
$connection->pipe(new React\Stream\WritableResourceStream(STDOUT));
$connection->write("Hello World!\n");
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
The react/socket documentation can be found within the GitHub project under the heading "Connection usage". Here you would find detailed information on how to implement and use the different interfaces and methods provided by the library. It explains how to use the ConnectionInterface
, what the ServerInterface
is responsible for, and how to use the ServerInterface
to listen for connection events, error events, and how to use methods associated with it.