Kirankumar Kirankumar - 1 year ago 511
MySQL Question

Set ajax call only when the new record inserted to database

I wants to get updated comment from chat list to the page without refreshing the page, Thats I have used ajax call for the list but I have to call this function for every 5 seconds to check whether new chat is inserted or not to the database,

url: baseUrl+"chat",
cache: false,
data: dataString,
crossDomain: true,
success: function(data){
var getData = JSON.parse(data);
if(getData.status == "success")
$("chatList").append("<span class='black'>"+getData["chat"][i].name+" : <span class='blue'>"+getData["chat"][i].comment+"</span>");

So I wants to know if there is any easy way to do this or from PHP MySQL it is possible to send page a new comment inserted notification ?

Answer Source

You can use a websocket, for example. That will allow you to send notifications from the server to the client. So, when you recieve data from your chat (cient) in your API (server), after updating the database, you will have to send a 'notification' to your client.

When your client get the notification, you can make your AJAX call :

socket.on('notification', function(){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download