Current Location: Home> Latest Articles> PHP WebSocket Development Example: Real-time Communication Feature Implementation Detailed Tutorial

PHP WebSocket Development Example: Real-time Communication Feature Implementation Detailed Tutorial

M66 2025-06-16

1. Introduction

WebSocket is a full-duplex communication protocol based on TCP, allowing persistent connections between clients and servers for real-time, bidirectional data transfer. In web development, WebSocket can be used to implement features like real-time chat and live data displays. This article will explain in detail how to develop WebSocket functionality using PHP, and demonstrate the process with practical examples.

2. Environment Setup

To use WebSocket functionality in PHP, the following requirements must be met:

  1. PHP version >= 5.3
  2. Install the Swoole extension for PHP: pecl install swoole (make sure pecl is installed)
  3. Specify a port for the WebSocket server (commonly port 8080 or another unused port)

3. Development Process

The following is the detailed process for implementing specific features:

Creating a WebSocket Server

First, we need to create a WebSocket server that listens on a specified port and handles client connections, messages, etc. This can be done using the SwooleWebSocketServer class provided by the Swoole extension. Below is a simple example:

<?php
$server = new SwooleWebSocketServer('0.0.0.0', 8080);

$server->on('open', function ($server, $request) {
    echo "new connection: {$request->fd}\n";
});

$server->on('message', function ($server, $frame) {
    $server->push($frame->fd, "Server received: {$frame->data}");
});

$server->on('close', function ($server, $fd) {
    echo "connection closed: {$fd}\n";
});

$server->start();

The above code creates a WebSocket server that listens on port 8080. When a new connection is made, it prints the connection ID; when a message is received, it sends the message back to the client; when the connection is closed, it prints the connection ID.

Handling Specific Features

To handle specific features on the WebSocket server, you can develop according to business needs. Below is an example of implementing a simple real-time chat feature:

<?php
$server = new SwooleWebSocketServer('0.0.0.0', 8080);
$connections = [];

$server->on('open', function ($server, $request) use (&$connections) {
    $connections[$request->fd] = $request->fd;
    echo "new connection: {$request->fd}\n";
});

$server->on('message', function ($server, $frame) use (&$connections) {
    foreach ($connections as $fd) {
        $server->push($fd, "User {$frame->fd} says: {$frame->data}");
    }
});

$server->on('close', function ($server, $fd) use (&$connections) {
    unset($connections[$fd]);
    echo "connection closed: {$fd}\n";
});

$server->start();

In this code, when a connection is opened, its ID is stored in an array. When a message is received, it is broadcast to all connected clients. When a connection is closed, the corresponding ID is removed from the array.

4. Using WebSocket Features

Once the development environment is set up, you can use a browser or other tools to connect via WebSocket and send messages. Below is an example of using JavaScript to connect and send messages:

var ws = new WebSocket('ws://localhost:8080');

ws.onopen = function() {
    console.log('connected');
};

ws.onmessage = function(event) {
    console.log('received: ' + event.data);
};

ws.onclose = function() {
    console.log('disconnected');
};

ws.send('Hello, WebSocket!');

This code creates a WebSocket connection and handles events for when the connection opens, when a message is received, and when the connection closes. The message is sent to the server using the send method.

5. Conclusion

This article has detailed the process of using PHP to develop WebSocket functionality, with a practical example of implementing a real-time chat feature. By using WebSocket technology, developers can enable real-time, bidirectional communication in web applications, improving the user experience. We hope this guide helps readers understand and apply WebSocket technology in their own PHP projects.