amphp/websocket
is an async WebSocket client for PHP based on Amp.
This package can be installed as a Composer dependency.
composer require amphp/websocket
- PHP 7.0+
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!");
}
}
});
amphp/websocket
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.