phrity/websocket
WebSocket client and multi-connection server for PHP, supporting ws/wss, listener callbacks, close and ping/pong handling, optional deflate compression, message fragmentation, and custom opcodes.
This library contains WebSocket client and server for PHP.
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.
ws (TCP) and wss (SSL) supportPreferred way to install is with Composer.
composer require phrity/websocket
The client can read and write on a WebSocket stream.
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();
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();
The server is a multi connection, listening server.
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();
How can I help you explore Laravel packages today?