Introduction to WebSocket in PHP: from theory to practice
Modern web applications increasingly require instant data transfer between client and server. The traditional HTTP request-response cycle, based on long polling or periodic AJAX requests, no longer handles real-time tasks. This is where the WebSocket protocol comes to the rescue — a technology that allows establishing a permanent two-way connection between the browser and server.
In this article, we will explore how to create real-time applications in PHP using WebSocket. You will learn how to install the Ratchet server library, write a simple chat and notification system, and what pitfalls await you. The material is aimed at developers familiar with the basics of PHP and JavaScript.
What is WebSocket and why is it important for PHP?
WebSocket is a protocol that works over TCP. It is initiated as an HTTP request (handshake), after which the connection is "upgraded" to a full two-way channel. Unlike HTTP:
- The server can send data to the client without a preliminary request.
- The connection remains open, saving resources on repeated handshakes.
- Data transmission latency is minimal — milliseconds versus seconds with long polling.
PHP has traditionally been considered a language unsuitable for real-time tasks due to its "one request — one process" model. However, modern libraries such as Ratchet and ReactPHP allow running long-lived PHP processes that work as independent WebSocket servers. This opens up possibilities for:
- Online chats and messengers.
- Notification systems (new emails, orders, alerts).
- Collaborative document editing.
- Games and trading platforms.
Installing and configuring a WebSocket server in PHP with Ratchet
To work with WebSocket in PHP, we will use the Ratchet library (based on ReactPHP). Installation is done via Composer.
Step 1. Create a project and install dependencies
mkdir websocket-phpcd websocket-phpcomposer require cboden/ratchetAfter installation, Ratchet and ReactPHP will appear in the vendor folder.
Step 2. Create a WebSocket server class
Create a file server.php:
<?phpuse Ratchet\\MessageComponentInterface;use Ratchet\\ConnectionInterface;
class ChatServer implements MessageComponentInterface { protected $clients;
public function __construct() { $this->clients = new \\SplObjectStorage; }
public function onOpen(ConnectionInterface $conn) { $this->clients->attach($conn); echo "New connection: {$conn->resourceId}"; }
public function onMessage(ConnectionInterface $from, $msg) { foreach ($this->clients as $client) { if ($from !== $client) { $client->send($msg); } } }
public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); echo "Disconnection: {$conn->resourceId}"; }
public function onError(ConnectionInterface $conn, \\Exception $e) { echo "Error: {$e->getMessage()}"; $conn->close(); }}Step 3. Start the server
Add the startup code to server.php:
use Ratchet\\Server\\IoServer;use Ratchet\\Http\\HttpServer;use Ratchet\\WebSocket\\WsServer;
require __DIR__ . '/vendor/autoload.php';
$server = IoServer::factory( new HttpServer( new WsServer( new ChatServer() ) ), 8080);
echo "WebSocket server started on port 8080";$se