When discussing real time communication, there aren't many solutions that can rival the power of a simple webchat. What is even better, is that you already have all the tools you need to create one - your web browser. This, coupled with the fact that this is also one of the most requested tutorials by Tutorialzine's readers, means that it is about time to start coding. We are using the to create the scrollable area with the chats entries. This plugin comes with its own stylesheet, which is the first thing we've included into the page. The markup of the chat consists of four main divs - the top bar, the chat container, the user container and the bottom bar. The latter holds the login and submit forms. The submit form is hidden by default and only shown if the user has successfully logged in the chat system. Lastly we include the JavaScript files. Starting with the jQuery library, we add the mousewheel plugin used by jScrollPanethe jScrollPane plugin itself and our script. For the purposes of this script we use two tables. The name field is defined as unique, so that no users have duplicate nick names in the chatroom. Notice that we are storing the author name and gravatar here as well. This duplication is worthwhile as it frees us from using an expensive join when requesting the latest chats - the most frequently accessed feature of the application. The definitions of these tables are available in tables. You can execute the code in phpMyAdmin to create them. Also, when setting up the chat on your own host, remember to modify ajax. The first file we are going to take a closer look at, is ajax. The switch statement routes all requests to the appropriate static method of the Chat class, which we will discuss later in this section. The constructor is private, which means that no objects can be created from the outside, and the initialization is only possible from the init static method. This way we can be sure that only one connection to the database can exists in the same time. The rest of the classes take advantage localhost 61161 chat popup form the static query method to communicate with the database. It's main purpose is to define the constructor, which takes an array with parameters, and saves only the ones that are defined in the class. It extends ChatBase, so you can easily create an object of this class by providing an array with a text, author, and gravatar elements. The gravatar property contains a md5 hash of the person's email address. This is required so we can fetch the user's gravatar from gravatar. This class also defines a save method, which the object to our database. We have the name and gravatar properties notice the protected access modifier - localhost 61161 chat popup form means that they will be accessible in the ChatBase class, so we can set them in the constructor. This shows that this person keeps a chat window open and is displayed as online in the users section. Remember the switch statement in ajax. It maps the supported actions with the corresponding methods from this class. We will be using this to validate that the user is allowed to add chats later on. You can also see how we are preparing the gravatar hash. This is done according to their and ensures that if the person has configured a Gravatar, it will be properly displayed. We are using this to delete chats older than 5 minutes and inactive users from the database. We could potentially delete those records in getChats, but that is requested once every second and the extra processing time could severely impact the performance of our app. In the frontend, we use the hour and minute values to feed the JavaScript date object, and as a result all the times are displayed in the user's local time. With this the first part of this tutorial is complete. Hi Martin, i have set up everything on my wamp local server but when add a user it give localhost 61161 chat popup form the error This Nick in use. Any idea why this is failing. There is also one more reason to choose this approach - users are purged every thirty seconds, but chats are kept for five minutes. This would normally break the join. As for the 1 request per second, in the next part of the tutorial, you will see that the frequency of the requests is gradually decreased and falls down to one every 15 seconds depending on the activity of the chat. I've used one or two of your tuts in personal projects at work in the development of a companywide intranet. I've been looking for a chat system for users as they want something other than personal messaging. This would do the trick, but the conversion to codeigniter is gonna be fun!. And possibly creating password accessible rooms to keep chats between users and not everyone. Hey Martin, Thanks for the awesome code. I would suggest to give the user the ability to choose the background color for his chat text. For instance, you could add a field at the login for the user to enter the color code, or choose from a drop down menu, which will reflect as a background for his own text. I think this is the most attractive looking web based chat I have ever see. However I have some problems installing this chat program. But in my webserver www. I have done the same thing that I did with my localhost. I think my web server do not support mysqli. Because couple of monts ago I tried make a simple database connection with mysqli but it did not work. I think I did not see myqli driver installed on my webserver. If you can give me a suggestion what to do then it would be helpful. Also, what is supposed to be in the gravatar field. Thanks : Hi, I wanna start by thanking to Martin, this is an amazing script. I put it online on my domain, you can see it here: I just added some code localhost 61161 chat popup form one of your previous tutorials to align it vertically. I have just one small problem, the comment are deleted after ~3 hours. On localhost everything is okay. On question: can I embed this function into a website where user have logged in already. Login results are save in a session variable. Is there a way to have localhost 61161 chat popup form logged in automatically in the chat session, avoiding the login form. I have tried a lot, but did not succeed. I am getting this error in the logs. It is not safe to rely on the system's timezone settings. You are required to use the date. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. So that you could simply add the methods of 'ChatUser' and 'ChatLine' in the class Chat as static methods and this will works finely. Please Martin, what's the magic behind separating theme in this way. How would I set the time to read a chatline every 10 seconds?.