Library for building an evented socket server.

Installs: 588 740

Dependents: 47

Stars: 108

Watchers: 17

Forks: 26

Open Issues: 12

Language: PHP

v0.4.2 2014-05-25 17:02 UTC


Build Status

Library for building an evented socket server.

The socket component provides a more usable interface for a socket-layer server or client based on the EventLoop and Stream components.


The server can listen on a port and will emit a connection event whenever a client connects.


The Connection is a readable and writable Stream. The incoming connection represents the server-side end of the connection.

It MUST NOT be used to represent an outgoing connection in a client-side context. If you want to establish an outgoing connection, use the SocketClient component instead.


Here is a server that closes the connection if you send it anything.

    $loop = React\EventLoop\Factory::create();

    $socket = new React\Socket\Server($loop);
    $socket->on('connection', function ($conn) {
        $conn->write("Hello there!\n");
        $conn->write("Welcome to this amazing server!\n");
        $conn->write("Here's a tip: don't say anything.\n");

        $conn->on('data', function ($data) use ($conn) {


You can change the host the socket is listening on through a second parameter provided to the listen method:

    $socket->listen(1337, '');

Here's a client that outputs the output of said server and then attempts to send it a string. For anything more complex, consider using the SocketClient component instead.

    $loop = React\EventLoop\Factory::create();

    $client = stream_socket_client('tcp://');
    $conn = new React\Stream\Stream($client, $loop);
    $conn->pipe(new React\Stream\Stream(STDOUT, $loop));
    $conn->write("Hello World!\n");