amphp/websocket-client

Async WebSocket client for PHP based on Amp.

v0.2.4 2019-02-06 15:35 UTC

README

Build Status CoverageStatus License

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

Installation

This package can be installed as a Composer dependency.

composer require amphp/websocket-client

Requirements

  • PHP 7.1+

Documentation & Examples

More extensive code examples reside in the examples directory.

use Amp\Delayed;
use Amp\Websocket\Client\Connection;
use Amp\Websocket\Message;
use function Amp\Websocket\Client\connect;

// Connects to the Kaazing echoing websocket demo.
Amp\Loop::run(function () {
    /** @var Connection $connection */
    $connection = yield connect('ws://demos.kaazing.com/echo');
    yield $connection->send("Hello!");

    $i = 0;

    while ($message = yield $connection->receive()) {
        /** @var Message $message */
        $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-client 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.