amphp/websocket-client
is an async WebSocket client for PHP based on Amp.
This package can be installed as a Composer dependency.
composer require amphp/websocket-client
- PHP 7.2+
More extensive code examples reside in the examples
directory.
use Amp\Websocket\Client\Connection;
use Amp\Websocket\Message;
use function Amp\delay;
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 delay(1000); // Pause the coroutine for 1 second.
if ($i < 3) {
yield $connection->send("Ping: " . ++$i);
} else {
yield $connection->send("Goodbye!");
}
}
});
amphp/websocket-client
follows the semver semantic versioning specification like all other amphp
packages.
If you discover any security related issues, please email [email protected]
instead of using the issue tracker.
The MIT License (MIT). Please see LICENSE
for more information.