How to Make a Live Chat Script in PHP

In this article, we’ll discuss how you can make a live chat script in PHP. Although there are different ways you could achieve this, we’ll use a socket-based implementation.

If you’re building a community site which involves user engagement, it’s useful to provide a way for users to discuss ideas in real time. When it comes to real-time interaction between users, it’s best to provide a kind of chat-room feature where they can get together and discuss their ideas.

Today, we’re going to discuss how you can make and implement live chat in your PHP website. As it’s a popular feature, there are a lot of ready-made chatscripts available in the market. You'll find a mix of both free and premium PHP live chat support scripts to choose from.

12 Best PHP Chat Scripts on CodeCanyon

Franc Lucas 25 Feb 2021

10 Best Free Live Chat PHP Scripts

Franc Lucas 22 Aug 2022

In this article, we’re going to use the Chat Using WebSocket and PHP Socket module, which is built in PHP and is completely open source.

In the next section, we’ll see how to download and configure the Chat Using WebSocket PHP and PHP Socket module.

How to Download and Configure the Chat Using WebSocket and PHP Socket Module

The Chat Using WebSocket and PHP Socket module is available on GitHub, and thus, you can either clone the repository or download the .zip package file.

If you want to clone the Chat Using WebSocket PHP and PHP Socket repository, you can use the following command on your command prompt.

git clone https://github.com/sanwebe/Chat-Using-WebSocket-and-PHP-Socket.git

On the other hand, if you want to download the .zip file, you could download it from the official repo.

The Chat-Using-WebSocket-and-PHP-Socket directory contains the package files. You need to place this directory under the document root of your web server so that you can access it via the browser.

Next, the package contains two PHP files, server.php and index.php, and we need to configure the host value in both files. Open both files in your favorite text editor and replace the localhost value with the hostname of your server. Of course, if you’re working on a local machine for testing purposes, you don’t need to change it.

With these changes, we’re done with the configuration of our module. Before we go ahead and see how to run it, we’ll go through the server.php and index.php files to understand how exactly it works in the next section.

How the Chat Using WebSocket and PHP Socket Module Works

The Chat Using WebSocket and PHP Socket module is based on PHP WebSockets. Let’s quickly see what exactly the WebSocket API is:

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply. — MDN

Once a browser opens a web socket connection with the web server, it can perform two-way communication with the web server. And thus, it can send data to the web server with the help of the send method, and it can receive data from the web server with the help of the onmessage event handler. So that’s the client-side implementation of PHP WebSockets.

On the server side, we need to use PHP socket functions to open and bind sockets, listen to incoming socket connections, and read and send data to all the available sockets. Look at a PHP sockets example to see how it actually works.

Let’s go through the important snippets in both files.

The index.php File

The index.php file is responsible for displaying the live chat code UI and handling a socket connection with the web server.

Let’s go through the following snippet, which is the most important part in the index.php file.

 language="javascript" type="text/javascript"> 
//create a new WebSocket object. 
var msgBox = $('#message-box'); 
var wsUri = "ws://localhost:9000/demo/server.php"; 
websocket = new WebSocket(wsUri); 
websocket.onopen = function(ev)  // connection is open 
msgBox.append('
Welcome to my "Demo WebSocket Chat box"!
'); //notify user
// Message received from server 
websocket.onmessage = function(ev)  
var response = JSON.parse(ev.data); //PHP sends Json data 
var res_type = response.type; //message type 
var user_message = response.message; //message text 
var user_name = response.name; //user name 
var user_color = response.color; //color 
switch(res_type) 
case 'usermsg': 
msgBox.append('
' + user_color + '">' + user_name + ' : ' + user_message + '
');
break; 
case 'system': 
msgBox.append(' ' + user_message + ' '); 
break; 
msgBox[0].scrollTop = msgBox[0].scrollHeight; //scroll message 
websocket.onerror = function(ev) msgBox.append('Error Occurred - ' + ev.data + ' '); >; 
websocket.onclose = function(ev) msgBox.append('
Connection Closed
'); >;
//Message send button 
$('#send-message').click(function() 
send_message();