Current Location: Home> Latest Articles> PHP Real-Time Chat System: Multi-User Session Management and Redis Real-Time Notifications

PHP Real-Time Chat System: Multi-User Session Management and Redis Real-Time Notifications

gitbox 2025-06-06

1. Introduction

Nowadays, social platforms such as WeChat and QQ commonly support instant messaging features. Real-time chat systems have become an indispensable part of modern websites. This article introduces a PHP-based real-time chat system capable of establishing real-time connections and transmitting messages between multiple users. The system works without page refresh and does not rely on additional client software, enhancing user experience.

2. Multi-User Session Management

Managing multi-user sessions is the core of a real-time chat system. This system uses the WebSocket protocol for connection and PHP as the server-side programming language. WebSocket is based on the TCP protocol and supports bidirectional communication between clients and servers, ensuring timely and reliable message delivery.

2.1. WebSocket Protocol

WebSocket is a TCP-based full-duplex protocol that allows clients and servers to send and receive data simultaneously. Its main features include:

  • Built on top of TCP; servers can actively push data to clients, and clients can send data to servers;
  • Initial handshake uses HTTP protocol, followed by data framed in WebSocket frames;
  • Supports full-duplex communication, allowing both sides to transmit data simultaneously.

2.2. Establishing Connection

The client first sends a WebSocket handshake request containing headers to upgrade the protocol. The server responds with headers confirming the upgrade to establish the connection.


// Establishing WebSocket connection on server side
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $port);
socket_listen($socket);
while (true) {
  // Waiting for client connection
  $clientSocket = socket_accept($socket);
  // Receiving data from client
  $data = socket_read($clientSocket, 1024);
  // Sending response data
  $response = "HTTP/1.1 101 Switching Protocols\r\n"
            . "Upgrade: websocket\r\n"
            . "Connection: Upgrade\r\n"
            . "Sec-WebSocket-Accept: " . base64_encode(sha1($key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true)) . "\r\n\r\n";
  socket_write($clientSocket, $response);
}

3. Real-Time Notifications

After the connection is established, Redis is used as the backend to support real-time notifications via its publish/subscribe mechanism, ensuring rapid message delivery to all subscribed users.

3.1. Introduction to Redis

Redis is an open-source, in-memory key-value database supporting various data types including strings, lists, sets, hashes, and sorted sets. It is widely used for caching, message queuing, and real-time data processing.

3.2. Publish/Subscribe Mechanism

In this system, user chat messages are published to a specific Redis channel. Clients subscribed to that channel receive messages in real-time, enabling instant notifications.


// Publish message to Redis channel
$redis->publish('chat:message', json_encode($message));

// Subscribe to Redis channel to receive messages and notify users in real-time
$redis->subscribe(['chat:message'], function ($redis, $channel, $message) use ($server) {
  foreach ($server->connections as $connection) {
    $connection->send($message);
  }
});

4. Conclusion

This article introduced key technologies for a PHP-based real-time chat system, including the WebSocket protocol for bidirectional communication and Redis publish/subscribe mechanism. Leveraging these technologies, the system can efficiently manage multi-user sessions and push messages instantly, greatly enhancing user interaction experience. We hope this solution aids developers in building effective real-time communication features.