PHP code example of react / socket

1. Go to this page and download the library: Download react/socket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

react / socket example snippets


$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();
    });
});

$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;
});

$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();
// …

$address = $connection->getRemoteAddress();
echo 'Connection with ' . $address . PHP_EOL;

$address = $connection->getRemoteAddress();
$ip = trim(parse_url($address, PHP_URL_HOST), '[]');
echo 'Connection with ' . $ip . PHP_EOL;

$address = $connection->getLocalAddress();
echo 'Connection with ' . $address . PHP_EOL;

$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'new connection' . PHP_EOL;
});

$socket->on('error', function (Exception $e) {
    echo 'error: ' . $e->getMessage() . PHP_EOL;
});

$address = $socket->getAddress();
echo 'Server listening on ' . $address . PHP_EOL;

$address = $socket->getAddress();
$port = parse_url($address, PHP_URL_PORT);
echo 'Server listening on port ' . $port . PHP_EOL;

$socket->pause();

$socket->on('connection', assertShouldNeverCalled());

$socket->pause();

Loop::addTimer(1.0, function () use ($socket) {
    $socket->resume();
});

echo 'Shutting down server socket' . PHP_EOL;
$socket->close();

$socket = new React\Socket\SocketServer('127.0.0.1:8080');

$socket = new React\Socket\SocketServer('0.0.0.0:8080');

$socket = new React\Socket\SocketServer('[::1]:8080');

$socket = new React\Socket\SocketServer('127.0.0.1:0');
$address = $socket->getAddress();

$socket = new React\Socket\SocketServer('unix:///tmp/server.sock');

$socket = new React\Socket\SocketServer('php://fd/3');

// throws InvalidArgumentException due to missing port
$socket = new React\Socket\SocketServer('127.0.0.1');

$first = new React\Socket\SocketServer('127.0.0.1:8080');

// throws RuntimeException because port is already in use
$second = new React\Socket\SocketServer('127.0.0.1:8080');

$socket = new React\Socket\SocketServer('[::1]:8080', [
    'tcp' => [
        'backlog' => 200,
        'so_reuseport' => true,
        'ipv6_v6only' => true
    ]
]);

$socket = new React\Socket\SocketServer('tls://127.0.0.1:8080', [
    'tls' => [
        'local_cert' => 'server.pem'
    ]
]);

$socket = new React\Socket\SocketServer('tls://127.0.0.1:8000', [
    'tls' => [
        'local_cert' => 'server.pem',
        'passphrase' => 'secret'
    ]
]);

$socket = new React\Socket\SocketServer('tls://127.0.0.1:8000', [
    'tls' => [
        'local_cert' => 'server.pem',
        'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER
    ]
]);

$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;
    
    $connection->write('hello there!' . PHP_EOL);
    …
});

$server = new React\Socket\TcpServer(8080);

$server = new React\Socket\TcpServer(0);
$address = $server->getAddress();

$server = new React\Socket\TcpServer('192.168.0.1:8080');

$server = new React\Socket\TcpServer('[::1]:8080');

// throws InvalidArgumentException due to missing port
$server = new React\Socket\TcpServer('127.0.0.1');

$first = new React\Socket\TcpServer(8080);

// throws RuntimeException because port is already in use
$second = new React\Socket\TcpServer(8080);

$server = new React\Socket\TcpServer('[::1]:8080', null, [
    'backlog' => 200,
    'so_reuseport' => true,
    'ipv6_v6only' => true
]);

$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;
    
    $connection->write('hello there!' . PHP_EOL);
    …
});

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, [
    'local_cert' => 'server.pem'
]);

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, [
    'local_cert' => 'server.pem',
    'passphrase' => 'secret'
]);

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, [
    'local_cert' => 'server.pem',
    'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER
]);

$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'Secure connection from' . $connection->getRemoteAddress() . PHP_EOL;
    
    $connection->write('hello there!' . PHP_EOL);
    …
});

$server->on('error', function (Exception $e) {
    echo 'Error' . $e->getMessage() . PHP_EOL;
});

$server = new React\Socket\UnixServer('/tmp/server.sock');

$first = new React\Socket\UnixServer('/tmp/same.sock');

// throws RuntimeException because socket is already in use
$second = new React\Socket\UnixServer('/tmp/same.sock');

$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'New connection' . PHP_EOL;

    $connection->write('hello there!' . PHP_EOL);
    …
});

$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});

$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});

$server = new React\Socket\LimitingServer($server, 100, true);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});

foreach ($server->getConnection() as $connection) {
    $connection->write('Hi!');
}

$connector->connect('google.com:443')->then(
    function (React\Socket\ConnectionInterface $connection) {
        // connection successfully established
    },
    function (Exception $error) {
        // failed to connect due to $error
    }
);

$promise = $connector->connect($uri);

$promise->cancel();

$connector = new React\Socket\Connector();

$connector->connect($uri)->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$connector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector->connect('unix:///tmp/demo.sock')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector = new React\Socket\Connector([
    'happy_eyeballs' => false
]);

$connector = new React\Socket\Connector([
    'dns' => '127.0.1.1'
]);

$connector->connect('localhost:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector = new React\Socket\Connector([
    'dns' => false
]);

$connector->connect('127.0.0.1:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('127.0.1.1');

$connector = new React\Socket\Connector([
    'dns' => $resolver
]);

$connector->connect('localhost:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector = new React\Socket\Connector([
    'timeout' => 10.0
]);

$connector = new React\Socket\Connector([
    'timeout' => false
]);

// only allow secure TLS connections
$connector = new React\Socket\Connector([
    'tcp' => false,
    'tls' => true,
    'unix' => false,
));

$connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

// allow insecure TLS connections
$connector = new React\Socket\Connector([
    'tcp' => [
        'bindto' => '192.168.0.1:0'
    ],
    'tls' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ],
]);

$connector->connect('tls://localhost:443')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$connector = new React\Socket\Connector([
    'tls' => [
        'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
    ]
]);

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('127.0.1.1');
$tcp = new React\Socket\HappyEyeBallsConnector(null, new React\Socket\TcpConnector(), $resolver);

$tls = new React\Socket\SecureConnector($tcp);

$unix = new React\Socket\UnixConnector();

$connector = new React\Socket\Connector([
    '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();
});

$tcpConnector = new React\Socket\TcpConnector();

$tcpConnector->connect('127.0.0.1:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$promise = $tcpConnector->connect('127.0.0.1:80');

$promise->cancel();

$tcpConnector = new React\Socket\TcpConnector(null, [
    'bindto' => '192.168.0.1:0'
]);

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8');

$dnsConnector = new React\Socket\HappyEyeBallsConnector(null, $tcpConnector, $dns);

$dnsConnector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$promise = $dnsConnector->connect('www.google.com:80');

$promise->cancel();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8');

$dnsConnector = new React\Socket\DnsConnector($tcpConnector, $dns);

$dnsConnector->connect('www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write('...');
    $connection->end();
});

$promise = $dnsConnector->connect('www.google.com:80');

$promise->cancel();

$secureConnector = new React\Socket\SecureConnector($dnsConnector);

$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");
    ...
});

$promise = $secureConnector->connect('www.google.com:443');

$promise->cancel();

$secureConnector = new React\Socket\SecureConnector($dnsConnector, null, [
    'verify_peer' => false,
    'verify_peer_name' => false
]);

$secureConnector = new React\Socket\SecureConnector($dnsConnector, null, [
    'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
]);

$timeoutConnector = new React\Socket\TimeoutConnector($connector, 3.0);

$timeoutConnector->connect('google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
    // connection succeeded within 3.0 seconds
});

$promise = $timeoutConnector->connect('google.com:80');

$promise->cancel();

$connector = new React\Socket\UnixConnector();

$connector->connect('/tmp/demo.sock')->then(function (React\Socket\ConnectionInterface $connection) {
    $connection->write("HELLO\n");
});

$connector = new React\Socket\FixedUriConnector(
    'unix:///var/run/docker.sock',
    new React\Socket\UnixConnector()
);

// destination will be ignored, actually connects to Unix domain socket
$promise = $connector->connect('localhost:80');