### Install amphp/websocket-client Source: https://github.com/amphp/websocket-client/blob/2.x/README.md Installs the amphp/websocket-client package using Composer. This is the standard method for adding the library as a dependency to your PHP project. ```bash composer require amphp/websocket-client ``` -------------------------------- ### Receive and Process WebSocket Messages Source: https://github.com/amphp/websocket-client/blob/2.x/README.md Shows how to receive messages from a WebSocket server, buffer them into a string, and process the payload. The example includes logic to close the connection when a specific message ('100') is received. ```PHP use Amp\Websocket\Client\WebsocketHandshake; use Amp\Websocket\WebsocketCloseCode; use function Amp\Websocket\Client\connect; // Connects to the websocket endpoint at libwebsockets.org // which sends a message every 50ms. $handshake = (new WebsocketHandshake('wss://libwebsockets.org')) ->withHeader('Sec-WebSocket-Protocol', 'dumb-increment-protocol'); $connection = connect($handshake); foreach ($connection as $message) { $payload = $message->buffer(); printf("Received: %s\n", $payload); if ($payload === '100') { $connection->close(); break; } } ``` -------------------------------- ### Connect to WebSocket Server (Basic) Source: https://github.com/amphp/websocket-client/blob/2.x/README.md Establishes a basic WebSocket connection to a specified URI using the `connect` function. It then iterates over incoming messages from the server. ```PHP connect($handshake); ``` -------------------------------- ### Send Text and Binary Data Source: https://github.com/amphp/websocket-client/blob/2.x/README.md Illustrates sending text and binary messages over an established WebSocket connection. Text messages must be valid UTF-8, while binary messages can contain arbitrary data. The methods return once the data is written to the send buffer. ```PHP // Assuming $connection is an active Amp\Websocket\Client\Connection instance // Send text data $connection->sendText('Hello, WebSocket!'); // Send binary data $binaryData = hex2bin('deadbeef'); $connection->sendBinary($binaryData); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.