Developing a PHP WebSocket Server for Real‑Time Multi‑User Collaboration
This article explains how to build a PHP WebSocket server using the Ratchet library, provides step‑by‑step code examples for server setup and a simple real‑time chat client, and demonstrates how to enable multi‑user online collaboration with WebSocket technology.
With the growth of the Internet, real‑time multi‑user collaboration has become a common requirement. WebSocket, a full‑duplex communication protocol, offers an efficient solution for such scenarios. This article introduces the basics of WebSocket and shows how to develop a WebSocket server in PHP.
1. WebSocket Overview
WebSocket is a TCP‑based protocol that establishes a persistent, bidirectional connection between client and server. Compared with the traditional HTTP request‑response model, WebSocket provides real‑time push capability, lower overhead than polling, and true full‑duplex communication.
Real‑time: server can push messages to clients instantly.
High efficiency: less communication overhead than long‑polling.
Full‑duplex: simultaneous send and receive.
2. Building a PHP WebSocket Server
In PHP, the Ratchet library (built on ReactPHP) simplifies WebSocket server development. Install Ratchet via Composer:
composer require cboden/ratchetCreate a server script that implements MessageComponentInterface and defines the lifecycle methods onOpen , onMessage , onClose , and onError . A minimal example is shown below:
<?php
require 'vendor/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($client !== $from) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, Exception $e) {
$conn->close();
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();Start the server from the command line:
php your_server_file.php3. Implementing Multi‑User Collaboration
With the server running, you can create a simple HTML page that connects to the WebSocket endpoint and broadcasts messages to all connected clients, effectively forming a real‑time chat room.
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<input type="text" id="message" placeholder="Enter message" />
<button id="send">Send</button>
<div id="chat"></div>
<script>
var conn = new WebSocket('ws://localhost:8080');
conn.onmessage = function(e) {
$('#chat').append('<p>' + e.data + '</p>');
};
$('#send').click(function() {
var message = $('#message').val();
conn.send(message);
$('#message').val('');
});
</script>
</body>
</html>Save the HTML file and open it in a browser; each opened page establishes a WebSocket connection to the server, enabling real‑time message exchange among multiple users.
Conclusion
The article demonstrated how to set up a PHP WebSocket server with Ratchet and how to use it to build a basic multi‑user collaboration feature. While the example is simple, the same principles can be extended to more complex real‑time applications.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.