Download the PHP package brash/websocket-middleware without Composer
On this page you can find all versions of the php package brash/websocket-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package websocket-middleware
WebSocket Middleware for react/http
This package provides a PHP React/HTTP WebSocket Middleware.
Requires PHP 8.3+
This library provides a WebSocket message broadcasting solution using the ReactPHP library. It demonstrates how to handle incoming text messages, broadcast messages to other connected clients, and log connection details.
Requirements
- PHP 8.3 or higher
- Composer
- ReactPHP
- Brash\Websocket library
Installation
-
Install the dependencies via Composer:
- Ensure the autoloader is included:
Usage
Starting the Server
Run the WebSocket server by executing the script in your terminal:
Key Components
AbstractTextMessageHandler
The AbstractTextMessageHandler
class handles WebSocket text messages. This implementation processes new connections, receives data, and broadcasts it to other connected clients.
MiddlewareFactory
Creates the middleware required for handling WebSocket connections.
React\Http\HttpServer
Handles HTTP requests and integrates WebSocket middleware.
Implementation Details
-
Connection Handling
- The server tracks all active connections in the
$connections
array. - When a new connection is established, it is added to the array using the
onOpen
method.
- The server tracks all active connections in the
- Message Broadcasting
- When a client sends a message, it is logged using the connection’s logger.
- The message is broadcast to all other clients, except the sender.
- The sender receives a response that includes their IP address and the message content in uppercase.
Code Example
Here is the core server implementation:
Example Output
When a client connects and sends a message:
- The server logs the IP address and message data.
- All other connected clients receive the message.
- The sender receives a response in uppercase:
Config Options
It is optional to set a Config object in the MiddlewareFactory, but you can do it as:
It is optional to set an array of paths, but this can be achieved as follows:
License
This library is licensed under the MIT License. See the LICENSE
file for more details.
Contributions
Contributions are welcome! Feel free to submit issues or pull requests on GitHub.
Acknowledgments
Enjoy using the WebSocket Message Broadcasting Library!
All versions of websocket-middleware with dependencies
brash/websocket Version ^1.0
monolog/monolog Version ^3.8
psr/http-message Version ~1.1|^2.0
react/http Version ^1.11