Reputation: 25
I want to send alert to client who is using the website only when saome field gets updated. i know how to regularly check for database updations with silent ajax calls. I only want to trigger an ajax call when there is information updated. In other Words, the mysql server will send an alert to client that some table row is changed so that the client will trigger the ajax call and the info can be retrieved.
Upvotes: 0
Views: 1351
Reputation: 617
You can use websockets with PHP. Take a look at Thruway. Thruway uses WAMP which supports a publish and subscribe message model. You could use the AutobahnJS JavaScript library on the browser side to subscribe to events published by PHP when it makes database updates.
Disclaimer: I am a project maintainer for Thruway.
Upvotes: 1
Reputation: 1952
you can use this code:
function checkUpdate()
{
jQuery.ajax({
type: "POST",
url: "your.php",
data: "name=currentUser&", //our any other data you want to check
success: function(msg){
if(msg.indexOf('no Updates') !== -1 )
{
alert( "You have an update" );
}
});
}
You can use the above code to call and get response from your.php and will alert when the response doesnt contain "no Updates." I suggest you to do this periodically by this code every 2 minute:
window.setInterval("checkUpdate()", 2*60*1000); // our any other period you want
Upvotes: 0
Reputation: 137
If your webserver is node use websockets otherwise the only other way is to long poll (use setInterval) to query the database every 30 seconds or so
Upvotes: 0
Reputation: 31
The best way to do so is the use of realtime technologies such as nodejs, however there is a way around in php (with javascript/jquery). I once develop a chat with php and javascript. All i did is set five seconds interval in javascript to listen to the server for any new message and append it to the chat box.
Upvotes: 0