ez-php / websocket-client
RFC 6455 WebSocket client for ez-php — HTTP upgrade handshake, masked frame encoder, plain-TCP and TLS (wss://) transports.
2.5.0
2026-09-20 02:41 UTC
Requires
- php: ^8.5
- ext-openssl: *
- ez-php/websocket: ^2.0
Requires (Dev)
- ez-php/docker: ^2.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
RFC 6455 WebSocket client for PHP 8.5 — HTTP upgrade handshake, masked frames, plain TCP (ws://) and TLS (wss://). Reuses the frame parser and opcodes of ez-php/websocket; no framework dependency.
Installation
composer require ez-php/websocket-client
Requires PHP 8.5 and ext-openssl.
Usage
use EzPhp\WebsocketClient\Client; $client = Client::connect('wss://example.com/socket', headers: ['Authorization' => 'Bearer …']); $client->send('hello'); // TEXT $client->sendBinary($bytes); // BINARY $message = $client->receive(timeout: 5.0); // ?Message — null when nothing arrived in time if ($message?->isText()) { echo $message->payload; } $client->ping(); // pongs are consumed by receive() $client->close(1000, 'bye'); // closing handshake, idempotent
receive()answers server pings, reassembles fragmented messages, and returns the server'sCLOSEas aMessage(isClose(),closeCode(),closeReason()).- Protocol violations by the server close the connection with the proper status code and throw
ConnectionException. A refused upgrade throwsEzPhp\WebSocket\HandshakeException. wss://verifies the certificate and host name by default. Override viaClient::connect(..., sslOptions: ['cafile' => '/path/ca.pem']).- Subprotocols:
Client::connect($url, protocols: ['chat']), then$client->subprotocol(). - The API is blocking and one instance serves one connection.
License
MIT