I have developed a random chatting system like “omegle.com” using jQuery, PHP/MySQL but I want to improve the site. At the current stage, it sends a request to PHP file for displaying new messages. The jQuery code is below
function load_msg(){
$.ajax({
url: 'show_msg.php',
success: function(result){
$("#wait").css("display","none");
$("#chat_canvas").html(result);
}
});
}
setInterval("load_msg()",3000);
But it displays all messages, I want to be able to display only new message.But I could not understanding how to do that.
I am giving an example,
suppose I have written in the text area “Hello xyz”.
it will send the message “Hello xyz” to a php file, and the php file will store it in database. Next this message is displayed via sending a request to a php file and it returns the result. As a result it displays “Hello xyz”. This is ok. But If I send “How are u? “, then it will load both
“Hello xyz” and
“How are u?”. I dont want to load “Hello xyz” over again,coz it has been already loaded before. I want to load only new message, that has not been already loaded. How can I do that???
I am sorry for my poor english, english is not my mother tongue.
You could add another column to the table that specifies (by boolean value) whether or not the message has been displayed yet. For example, when the second PHP file makes a request to get the messages, it could set that column to 1 (where 1 = displayed) for each column it just sent to the client, and ignore every column that is already set to 1.
To elaborate, you may have a query like this that updates that row in the database where all terms are met (to make sure it’s being displayed to the correct client). Afterwards, you could get all affected rows and then return them on the page (which is then retrieved by the Javascript and displayed to the client).
The important part is to get all of the affected, preferrably using
PDOormysqli.