Lets get started. We’ll keep the application simple, modular, and extensible. We won’t implement a login module, support for chat rooms, the online users list, and so on. By keeping it simple we’ll focus on what the goal of this article is: posting and retrieving messages without causing any page reloads. We’ll also let the user pick a color for her or his messages, because this involves an AJAX mechanism that is another good exercise.
The chat application can be tested online at http://ajaxphp.packtpub.com, and should look like Figure 8-1:
Using jQuery as a framework will simplify things: we won’t need to worry about constructing XmlHttpRequest by ourselves and implementing design patterns and best practices.
Technically, the application is split into two smaller applications that build the final solution:
Here we use a MySql database and AJAX to store and retrieve the users’ messages and pass them between the client and the server. The chat window contacts the server periodically to send and retrieve the newest posted messages from the server to each user. Our DB will also hold username and text color information used in the application.
Implementing this functionality involves creating the files and structures shown in the following figure:
The application functions following our usual coding pattern as follows:
The files to which we’re paying a little attention before starting to code are chat.js and chat.class.php. The chat.class.php file contains a server-side class named Chat which includes all the server-side functionality required to manipulate chat messages, as you can see in its diagram in Figure 8-3. This class contains methods for adding, deleting, and retrieving chat messages to and from the chat database table.
Then we have the Chat class in the chat.js file. This is a JavaScript class that contains the client-side functionality required for our chatting application, which include functions for retrieving the list of messages from the server, sending new messages, deleting messages, displaying error messages, and so on. Most of the features are backed up by the server-side components, which are called to perform the necessary work.
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…