chrome-php/wrench

A simple PHP WebSocket implementation

Maintainers

👁 graham-campbell

Package info

github.com/chrome-php/wrench

pkg:composer/chrome-php/wrench

Statistics

Installs: 4 255 954

Dependents: 4

Suggesters: 1

Stars: 67

Open Issues: 1

v1.8.0 2025-12-27 11:29 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

Replaces

None

MIT bd67a315cf2143ba30598339ad1b6c346db96c99

websocketWebSocketshybi


README

👁 Latest Stable Version
👁 License

A simple PHP WebSocket implementation.

Installation

The library can be installed with Composer and is available on Packagist under chrome-php/wrench:

$ composer require chrome-php/wrench

PHP 7.4-8.5 are currently supported, only.

Usage

This creates a server on 127.0.0.1:8000 with one Application that listens for WebSocket requests to ws://localhost:8000/echo and ws://localhost:8000/chat:

Server

// An example application, that just echoes the received
// data back to the connection that sent it
$app = new class implements \Wrench\Application\DataHandlerInterface
{
 public function onData(string $data, \Wrench\Connection $connection): void
 {
 $connection->send($data);
 }
};

// A websocket server, listening on port 8000
$server = new \Wrench\BasicServer('ws://localhost:8000', [
 'allowed_origins' => [
 'mysite.com',
 'mysite.dev.localdomain'
 ],
]);

$server->registerApplication('echo', $app);
$server->registerApplication('chat', new \My\ChatApplication());
$server->setLogger($monolog); // PSR3
$server->run();

Client

// A client side example, that sends a string and will receive
// the data back to the connection that sent it
$client = new Client('ws://localhost:8000', 'http://localhost:8000');
$client->connect();
$client->sendData('hello');
$response = $client->receive()[0]->getPayload();
$client->disconnect();

Contributing

See CONTRIBUTING.md for contribution details.

License

This project is licensed under the The MIT License (MIT).