amphp/websocket

Async WebSocket client for PHP based on Amp.

v0.2.2 2017-12-18 23:19 UTC

README

Build Status CoverageStatus License

amphp/websocket is an async WebSocket client for PHP based on Amp.

Installation

This package can be installed as a Composer dependency.

composer require amphp/websocket

Requirements

  • PHP 7.0+

Documentation & Examples

More extensive code examples reside in the examples directory.

use Amp\Delayed;
use Amp\Websocket;

// Connects to the websocket endpoint in demo.php provided with Aerys (https://github.com/amphp/aerys).
Amp\Loop::run(function () {
    /** @var \Amp\Websocket\Connection $connection */
    $connection = yield Websocket\connect("ws://localhost:1337/ws");
    yield $connection->send("Hello!");

    $i = 0;

    while ($message = yield $connection->receive()) {
        $payload = yield $message->buffer();
        printf("Received: %s\n", $payload);

        if ($payload === "Goodbye!") {
            $connection->close();
            break;
        }

        yield new Delayed(1000);

        if ($i < 3) {
            yield $connection->send("Ping: " . ++$i);
        } else {
            yield $connection->send("Goodbye!");
        }
    }
});

Versioning

amphp/websocket follows the semver semantic versioning specification like all other amphp packages.

Security

If you discover any security related issues, please email contact@amphp.org instead of using the issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.