Search by

ez-php / websocket-client

AU9500

RFC 6455 WebSocket client for ez-php — HTTP upgrade handshake, masked frame encoder, plain-TCP and TLS (wss://) transports.

Package info

github.com/ez-php/websocket-client

pkg:composer/ez-php/websocket-client

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

2.5.0 2026-09-20 02:41 UTC

This package is auto-updated.

Last update: 2026-09-20 02:56:45 UTC


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's CLOSE as a Message (isClose(), closeCode(), closeReason()).
  • Protocol violations by the server close the connection with the proper status code and throw ConnectionException. A refused upgrade throws EzPhp\WebSocket\HandshakeException.
  • wss:// verifies the certificate and host name by default. Override via Client::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