diff --git a/examples/php8-demo/README.md b/examples/php8-demo/README.md new file mode 100644 index 00000000..d31294b7 --- /dev/null +++ b/examples/php8-demo/README.md @@ -0,0 +1,83 @@ +# PHP 8 WebSocket Example (Ratchet) + +This example provides a minimal WebSocket **server** and **client** compatible with **PHP 8.3+** (including PHP 8.4), demonstrating a modern and fully working setup with Ratchet. + +It showcases: + +* A simple Ratchet WebSocket server (tested with Ratchet >= 0.4) +* A standalone PHP client using the **`ratchet/pawl`** library +* Full compatibility with Linux and macOS + +> **Note** +> The client uses `ratchet/pawl`, which is **not a dependency of Ratchet itself**. +> We keep it separate to avoid introducing new PHP version requirements into Ratchet’s core `composer.json`. + +--- + +## Requirements + +* PHP `>=7.4` (required by `ratchet/pawl`) +* Ratchet (already installed in the main project) + +--- + +## How to run + +### 1. Install Ratchet dependencies (from the project root) + +```bash +composer install +``` + +### 2. Move into the `php8-demo` folder + +```bash +cd examples/php8-demo +``` + +### 2. Install the client dependency (inside the `php8-demo` folder) + +```bash +composer install +``` + +**Note:** +This creates a dedicated `composer.lock` and `vendor` folder inside the example folder, keeping the demo self-contained and avoiding new dependencies in Ratchet itself. + +### 3. Start the WebSocket server + +```bash +php server.php +``` + +You should see: + +``` +WebSocket server started at ws://127.0.0.1:8080 +``` + +### 4. Run the PHP client + +```bash +php client.php +``` + +Expected output: + +``` +Connected to WebSocket server +Message sent: Hello from PHP 8 client! +Message received from server: Client says: Hello from PHP 8 client! +Connection closed +``` + +--- + +## Tested with + +| OS | PHP Version | +| ----- | ----------- | +| Linux | 8.3.x | +| macOS | 8.4.x | + +This example ensures full compatibility with modern PHP versions without affecting Ratchet’s backward compatibility. diff --git a/examples/php8-demo/client.php b/examples/php8-demo/client.php new file mode 100644 index 00000000..83f288e9 --- /dev/null +++ b/examples/php8-demo/client.php @@ -0,0 +1,41 @@ +then(function(Ratchet\Client\WebSocket $conn) use ($loop) { + + echo "[" . date('H:i:s') . "] Connected to WebSocket server\n"; + + // Send a test message + $message = "Hello from PHP 8 client!"; + $conn->send($message); + echo "[" . date('H:i:s') . "] Message sent: {$message}\n"; + + // When a message arrives + $conn->on('message', function($msg) use ($conn, $loop) { + echo "[" . date('H:i:s') . "] Received from server: {$msg}\n"; + + // Close connection after receiving a message + $conn->close(); + $loop->stop(); + }); + + // When the connection closes + $conn->on('close', function() { + echo "[" . date('H:i:s') . "] Connection closed\n"; + }); + + }, function(\Exception $e) use ($loop) { + echo "[" . date('H:i:s') . "] Could not connect: {$e->getMessage()}\n"; + $loop->stop(); + }); + +$loop->run(); diff --git a/examples/php8-demo/composer.json b/examples/php8-demo/composer.json new file mode 100644 index 00000000..1b5d3f55 --- /dev/null +++ b/examples/php8-demo/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "ratchet/pawl": "^0.4", + "react/event-loop": "^1.3" + } +} diff --git a/examples/php8-demo/server.php b/examples/php8-demo/server.php new file mode 100644 index 00000000..b176d2c8 --- /dev/null +++ b/examples/php8-demo/server.php @@ -0,0 +1,51 @@ +clients[$conn->resourceId] = $conn; + echo "[" . date('H:i:s') . "] New connection: ({$conn->resourceId})\n"; + } + + public function onMessage(ConnectionInterface $from, $msg) { + echo "[" . date('H:i:s') . "] Message received from {$from->resourceId}: $msg\n"; + + foreach ($this->clients as $client) { + try { + $client->send("Client {$from->resourceId} says: $msg"); + } catch (\Exception $e) { + echo "[" . date('H:i:s') . "] Error while sending message to client {$client->resourceId}: {$e->getMessage()}\n"; + } + } + } + + public function onClose(ConnectionInterface $conn) { + unset($this->clients[$conn->resourceId]); + echo "[" . date('H:i:s') . "] Connection closed: ({$conn->resourceId})\n"; + } + + public function onError(ConnectionInterface $conn, \Exception $e) { + echo "[" . date('H:i:s') . "] Error: {$e->getMessage()}\n"; + $conn->close(); + } +} + +// Running WebSocket server at ws://127.0.0.1:8080 +$server = IoServer::factory( + new HttpServer( + new WsServer(new DemoChat()) + ), + 8080 +); + +echo "[" . date('H:i:s') . "] WebSocket server started at ws://127.0.0.1:8080\n"; +$server->run();