Home >PHP Framework >Workerman >How can I use Workerman to build a WebSocket server for real-time communication?
This article demonstrates building real-time applications using Workerman's PHP WebSocket server. It details server creation, message handling, error management, and efficient concurrent connection handling via worker processes and asynchronous oper
Workerman provides a straightforward way to create robust WebSocket servers for real-time applications. The core process involves using its built-in WebSocket support. Here's a step-by-step guide:
composer require workerman/workerman
websocket_server.php
). This file will contain your server logic. A basic example looks like this:<code class="php"><?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; use Workerman\Connection\ConnectionInterface; use Workerman\Protocols\Http\Request; $ws_worker = new Worker('websocket://0.0.0.0:8080'); $ws_worker->count = 4; // Adjust based on your needs; number of worker processes $ws_worker->onMessage = function(ConnectionInterface $connection, $data) { // Process incoming messages $connection->send('Hello from Workerman! You sent: ' . $data); // Broadcast to all connected clients (optional): // foreach($ws_worker->connections as $client){ // $client->send($data); // } }; $ws_worker->onClose = function(ConnectionInterface $connection) { // Handle client disconnections echo "Connection closed\n"; }; $ws_worker->onError = function(ConnectionInterface $connection, $code, $msg) { // Handle errors echo "Error: $msg\n"; }; Worker::runAll();</code>
websocket_server.php
in your terminal and run the command php websocket_server.php start
. This starts the WebSocket server. You can then connect to it using a WebSocket client (like a browser with JavaScript or a dedicated WebSocket client).onMessage
callback function receives incoming messages from clients. You can process these messages and send responses back using $connection->send()
. The example above echoes the received message back to the client. Remember to implement your application logic within this function.onClose
and onError
callbacks handle client disconnections and server errors, allowing for graceful handling and logging.Workerman offers several advantages over other frameworks for building real-time WebSocket applications:
Workerman's inherent asynchronous nature allows it to handle multiple concurrent connections efficiently. The key aspects are:
$ws_worker->count
property in the example code controls the number of worker processes. Increasing this number (within reason, based on your server resources) allows the server to handle more concurrent connections. Each process handles a subset of the connections.Several common pitfalls can arise when developing WebSocket servers with Workerman:
onMessage
callback. This can block the event loop and hinder the server's ability to handle other connections. Use asynchronous operations or offload long-running tasks to separate processes or threads.onError
callback to catch and log errors effectively. This helps in debugging and maintaining the server's stability.php-fpm
's error logs or system logs for more insights. If using a development environment, you can start the server with the -d
flag to run in the background and see logs more easily.Troubleshooting Steps:
xdebug
) to step through your code and identify the source of errors.By carefully considering these points and implementing appropriate error handling, you can build reliable and scalable WebSocket servers using Workerman.
The above is the detailed content of How can I use Workerman to build a WebSocket server for real-time communication?. For more information, please follow other related articles on the PHP Chinese website!