Skip to content

Commit 1b736a1

Browse files
PHP 8 WebSocket demo fully working with textalk/websocket client
1 parent 9ac6a41 commit 1b736a1

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

examples/php8-demo/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# PHP 8 WebSocket Example (Ratchet)
2+
3+
This example shows a minimal WebSocket **server** and **client** compatible with **PHP 8.3+** (including PHP 8.4).
4+
5+
It demonstrates:
6+
7+
- A simple Ratchet WebSocket server (tested with Ratchet >= 0.4)
8+
- A standalone PHP client using the `textalk/websocket` library
9+
- Full compatibility with Linux and macOS
10+
11+
> **Note**
12+
> The client uses `textalk/websocket`, which is **not a dependency of Ratchet itself**.
13+
> We intentionally keep it separate to avoid introducing new PHP version requirements into Ratchet’s core `composer.json`.
14+
15+
---
16+
17+
## Requirements
18+
19+
- PHP `^7.4|^8.0` (required by `textalk/websocket`)
20+
- Ratchet (already installed in the main project)
21+
22+
---
23+
24+
## How to run
25+
26+
### 1. Install Ratchet dependencies (from the project root)
27+
28+
```
29+
composer install
30+
```
31+
32+
### 2. Install the client dependency (inside the `php8-demo` example folder)
33+
34+
```
35+
composer require textalk/websocket
36+
```
37+
38+
**Note:**
39+
40+
This should create a specific `composer.json` file *inside* the `php8-demo` folder. This keeps the example self-contained and avoids adding new dependencies to Ratchet itself.
41+
42+
### 3. Start the WebSocket server
43+
44+
```
45+
php server.php
46+
```
47+
48+
You should see:
49+
50+
```
51+
WebSocket server started at ws://127.0.0.1:8080
52+
```
53+
54+
### 4. Run the PHP client
55+
56+
```
57+
php client.php
58+
```
59+
60+
Expected output:
61+
62+
```
63+
Connected to WebSocket server
64+
Message sent
65+
Message received from server: Client <number> says: Hello from PHP 8 client!
66+
Connection closed
67+
```
68+
69+
---
70+
71+
## Tested with
72+
73+
| OS | PHP Version |
74+
|---------|-------------|
75+
| Linux | 8.3.x |
76+
| macOS | 8.4.x |
77+
78+
This example ensures full compatibility with modern PHP versions without affecting Ratchet’s backward compatibility.

examples/php8-demo/client.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require __DIR__ . '/vendor/autoload.php';
4+
5+
use WebSocket\Client;
6+
7+
try {
8+
$client = new Client("ws://127.0.0.1:8080", [
9+
'timeout' => 5,
10+
'headers' => ['Origin' => 'http://127.0.0.1']
11+
]);
12+
13+
echo "[" . date('H:i:s') . "] Connected to WebSocket server\n";
14+
15+
// Send a message
16+
$client->send("Hello from PHP 8 client!");
17+
echo "[" . date('H:i:s') . "] Message sent\n";
18+
19+
// Get a response (timeout 5s)
20+
$response = $client->receive();
21+
echo "[" . date('H:i:s') . "] Message received from server: $response\n";
22+
23+
$client->close();
24+
echo "[" . date('H:i:s') . "] Connection closed\n";
25+
26+
} catch (\Exception $e) {
27+
echo "[" . date('H:i:s') . "] Client error: " . $e->getMessage() . "\n";
28+
}

examples/php8-demo/server.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
require __DIR__ . '/../../vendor/autoload.php';
4+
5+
use Ratchet\MessageComponentInterface;
6+
use Ratchet\ConnectionInterface;
7+
use Ratchet\Server\IoServer;
8+
use Ratchet\WebSocket\WsServer;
9+
use Ratchet\Http\HttpServer;
10+
11+
class DemoChat implements MessageComponentInterface {
12+
protected array $clients = [];
13+
14+
public function onOpen(ConnectionInterface $conn) {
15+
$this->clients[$conn->resourceId] = $conn;
16+
echo "[" . date('H:i:s') . "] New connection: ({$conn->resourceId})\n";
17+
}
18+
19+
public function onMessage(ConnectionInterface $from, $msg) {
20+
echo "[" . date('H:i:s') . "] Message received from {$from->resourceId}: $msg\n";
21+
22+
foreach ($this->clients as $client) {
23+
try {
24+
$client->send("Client {$from->resourceId} says: $msg");
25+
} catch (\Exception $e) {
26+
echo "[" . date('H:i:s') . "] Error while sending message to client {$client->resourceId}: {$e->getMessage()}\n";
27+
}
28+
}
29+
}
30+
31+
public function onClose(ConnectionInterface $conn) {
32+
unset($this->clients[$conn->resourceId]);
33+
echo "[" . date('H:i:s') . "] Connection closed: ({$conn->resourceId})\n";
34+
}
35+
36+
public function onError(ConnectionInterface $conn, \Exception $e) {
37+
echo "[" . date('H:i:s') . "] Error: {$e->getMessage()}\n";
38+
$conn->close();
39+
}
40+
}
41+
42+
// Running WebSocket server at ws://127.0.0.1:8080
43+
$server = IoServer::factory(
44+
new HttpServer(
45+
new WsServer(new DemoChat())
46+
),
47+
8080
48+
);
49+
50+
echo "[" . date('H:i:s') . "] WebSocket server started at ws://127.0.0.1:8080\n";
51+
$server->run();

0 commit comments

Comments
 (0)