phrity / websocket
WebSocket client and server
Installs: 1 007 250
Dependents: 26
Suggesters: 0
Security: 0
Stars: 116
Watchers: 4
Forks: 18
Open Issues: 2
Requires
- php: ^8.1
- phrity/net-stream: ^2.1
- phrity/net-uri: ^2.1
- psr/http-message: ^1.1 | ^2.0
- psr/log: ^1.0 | ^2.0 | ^3.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^10.0 | ^11.0
- phrity/net-mock: ^2.1
- phrity/util-errorhandler: ^1.1
- squizlabs/php_codesniffer: ^3.5
- dev-v3.2-main
- dev-v3.1-main
- dev-v2.2-main
- 3.2.0
- 3.1.0
- 3.0.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.1
- 0.1.0
This package is auto-updated.
Last update: 2024-10-31 17:04:03 UTC
README
This library contains WebSocket client and server for PHP.
Replaces textalk/websocket
.
The client and server provides methods for reading and writing to WebSocket streams.
This repo replaces the abandoned textalk/websocket
repo
and is maintained by Sören Jensen, who has been maintaining the original since v1.3
.
Documentation
- Documentation
- Client - The WebSocket client
- Server - The WebSocket server
- Changelog - The changelog of this repo
- Contributing - Contributors and requirements
- Examples - Examples
- v2 -> v3 - How to migrate from v2 to v3
Installing
Preferred way to install is with Composer.
composer require phrity/websocket
Client
The client can read and write on a WebSocket stream. It internally supports Upgrade handshake and implicit close and ping/pong operations.
Set up a WebSocket Client for request/response strategy.
$client = new WebSocket\Client("wss://echo.websocket.org/"); $client // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) ; // Send a message $client->text("Hello WebSocket.org!"); // Read response (this is blocking) $message = $client->receive(); echo "Got message: {$message->getContent()} \n"; // Close connection $client->close();
Set up a WebSocket Client for continuous subscription
$client = new WebSocket\Client("wss://echo.websocket.org/"); $client // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) // Listen to incoming Text messages ->onText(function (WebSocket\Client $client, WebSocket\Connection $connection, WebSocket\Message\Message $message) { // Act on incoming message echo "Got message: {$message->getContent()} \n"; // Possibly respond to server $client->text("I got your your message"); }) ->start();
Server
The server is a multi connection, listening server. It internally supports Upgrade handshake and implicit close and ping/pong operations.
Set up a WebSocket Server for continuous listening
$server = new WebSocket\Server(); $server // Add standard middlewares ->addMiddleware(new WebSocket\Middleware\CloseHandler()) ->addMiddleware(new WebSocket\Middleware\PingResponder()) // Listen to incoming Text messages ->onText(function (WebSocket\Server $server, WebSocket\Connection $connection, WebSocket\Message\Message $message) { // Act on incoming message echo "Got message: {$message->getContent()} \n"; // Possibly respond to client $connection->text("I got your your message"); }) ->start();