PHPackages                             moebius/socket - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. moebius/socket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

moebius/socket
==============

A coroutine based socket client and server implementation.

1.0.0-RC1(4y ago)04MITPHP

Since Apr 7Pushed 4y ago1 watchersCompare

[ Source](https://github.com/frodeborli/moebius-socket)[ Packagist](https://packagist.org/packages/moebius/socket)[ RSS](/packages/moebius-socket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (8)Used By (0)

moebius/socket
==============

[](#moebiussocket)

An easy to use interface for working with many simultaneous and non-blocking network connections.

> Note! A default build of PHP limit the number of concurrent "polling" connections to 1024. This limitation can be overcome by running multiple server processes, or by recompiling PHP.

Architecture
------------

[](#architecture)

This library provides three core classes which you can use to create network clients or server implementations.

- `Moebius\Socket\Client` provides an API to the `stream_socket_client()` function in PHP, and can be used to create concurrent HTTP clients.
- `Moebius\Socket\Server` provides an API to the `stream_socket_server()` function, and lets you create servers that accepts connections and gives you connection instances for every new client that connects.
- `Moebius\Socket\Connection` is similar to the Client class, but connections are initiated externally and returned by the Server class.

Example client
--------------

[](#example-client)

```
use Moebius\Socket\Client;
use function M\{go, await};

/**
 * *** COROUTINE 1 ***
 */
$google = go(function() {
    $client = new Client('tcp://www.google.com:80');
    $client->write("GET / HTTP/1.0\r\n\r\n");
    while (!$client->eof()) {
        echo "< ".$client->readLine()."\n";
    }
});
/**
 * *** COROUTINE 2 ***
 */
$bing = go(function() {
    $client = new Client('tcp://www.bing.com:80');
    $client->write("GET / HTTP/1.0\r\n\r\n");
    while (!$client->eof()) {
        echo "< ".$client->readLine()."\n";
    }
});

/**
 * *** AWAIT BOTH COROUTINES ***
 */
await($google);
await($bing);
```

Example server
--------------

[](#example-server)

```
use Moebius\Socket\Server;
use function M\go;

$server = new Server('tcp://0.0.0.0:8080');
while ($connection = $server->accept()) {

    /**
     * *** LAUNCH A COROUTINE PER CONNECTION ***
     */
    go(function() use ($connection) {

        $requestLine = $connection->readLine();

        do {
            $header = $connection->readLine();
        } while ($header !== '');

        $connection->write(
