I have a simple chat service. I store messages, login and time etc. in Mysql database. The chat messages are displayed below with the help of Ajax & PHP
<div id="messages"> </div><!--- chats are displayed here -->
I have following Ajax code which fetches datas from Mysql in every 2 seconds. Ofcourse, everyone recommends not to do this way. It might effect server performances negatively. And its unnecessary.
$(document).ready( function() {
var destination_hashed = $("#destination_hashed").val();
var interval = setInterval( function() {
$.ajax ({
type: "POST",
url: "chat.php",
data: { destination_hashed1: destination_hashed },
success: function(data) {
$("#messages").html(data);
}
});
}, 2000);
});
In nutshell, I have two chat clients A & B. When A sends message to B, there are new rows inserted in Mysql.
So, how can I write Ajax & PHP code to fetch only when there are new rows. Rather than fetching data from Mysql every 2 seconds whether new rows inserted or not