An Easy-to-Follow Guide on Creating a Chat Room Using jQuery/PHP- Pak Coders

An Easy-to-Follow Guide on Creating a Chat Room Using jQuery-PHP-pakcoders


Today, online chat solutions are witnessing a continuous increase in their popularity across the world. If you’ve used any of the Internet Relay Chat(IRC) clients lately, then you’d be probably well familiar with the overall performance of an online chat service. Even before AJAX was born, online chat solutions had been successful in occupying a unique place in the world of information and technology. It is an AJAX chat applicable which inherits the typical AJAX benefits like cross-platform compatibility and a seamless integration with existing web browser features. One of the greatest advantages of an AJAX chat application is that it avoids the connectivity issues which are common with a range of other web app development technology. Also, an AJAX app exclusively uses HTTP for communication with the web server.

If the above mentioned details about an AJAX chat app appear interesting and you’ve made up your mind for creating one, then you’ve reached the right post. Here, I’ll be offering you a step wise guide on creating a chat room that’s easy to get up and running on every server that runs PHP. There’s no need for any database simply because the chat would get stored inside a text file.

Here’s a look at the pre-requisites of creating a chat room using jQuery and PHP

The technologies that will be used for creating the jQuery and PHP powered chat room include the following:

  • PHP – This will handle all the server side-related stuff such as: writing new messages into the text file, reading new messages from the text file, basic security as well as retrieving ‘state’ of text file.
  • jQuery/Javascript – This will handle a range of other client-side stuff including the following: asking the server periodically whether any new messages have been posted; scrolling the chat down to the most recent messages; appending new messages to the chat; limiting the textual input in order to prevent any of the ridiculous messages; affirming basic security for the chat room.
  • Text file – This will be storing the chat.

Step 1

The basic HTML structure of the chat room will be as shown below:

The double divs which you see in the above code are just required for pulling off the unnecessary double border effect within the chat area. The textarea with an id as ‘posttext’ along with chat area div.JavaScript will be targeting the two divs.

Step 2

Create a ‘Chat’ function which serves as the parent to a set of other functions which are required for dealing with a lot of other chat-related activities. The code snippet for this function is shown below:

In the above function, updateChat will ask server whether or not there are any new lines in the text file. On finding the same, updateChat will return the lines as JSON and the new lines will be appended to chat. Then, there is sendChat which will be called every time a message is being entered into the text area and ‘return’ is clicked. The above function will pass this data to the server. Next, there is getState which will ask the server about the count of lines included within the current text file. This will enable the function to determine as to when the lines are ‘new’ or ‘old’. All this information is returned in the form of JSON and the related functions are similar to these:

All the above mentioned functions use jQuery’s AJAX abilities and initiate communication using PHP file that’s called ajax.php.

Step 3

Set up a switch statement that covers every possible function

Data that gets passed with AJAX calls is simply an arbitrary value that’s called ‘function’. This values informs the PHP file about the kind of task it needs to perform. So, it is essential to set up a switch statement that would cover every possible function. On calling getState, the text file will be read and number of lines will be returned as the output. On updating, the same text file will be read and any new lines will be returned. On sending, the message will be processed, followed by getting written into text file in the form of a new line.

The PHP for the same is displayed below:

Step 4 – Get the chat room started

Now, you’ll be required to write some Javascript code for loading jQuery and create some quick functions for gathering chat participants name for joining the chat room. Here is the Javascript associated with this step:

Bonus Tip

Now that you’re done with starting your chat room, you can call the ‘update’ function regularly for checking any new messages. For this, you can use Javascript’s setInterval() function as shown below:

That’s it!

An Easy-to-Follow Guide on Creating a Chat Room Using jQuery-PHP-pakcoders


Building a unique chat room using jQuery and PHP is indeed an excellent business decision. I’m sure this post of mine would have rendered you excellent insights on proceeding ahead with the same in utmost comfortable manner.

Skip to toolbar