Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live Notification Jquery

Tags:

jquery

php

live

Can someone lead me down the right way to make a live notifications

e.g Knowing when a new Row in Added in Mysql

know if a php file has changed ???

how should i go about it?

like image 518
Rickstar Avatar asked Dec 29 '22 07:12

Rickstar


2 Answers

You could routinely check the server for updates using setInterval(), or you could employ long-polling with javascript. The benefit of setInterval() is that it doesn't keep connections opened on your server for too long, but you may have updates during the 'downtime' between server-calls. Long-polling will give you near-instant updates, as it waits with the connection opened until it receives new information. But obviously, the down side is that you've got connections staying opened all over the place.

Routine Checks...

setInterval(function(){
  $.get("updates.php", {}, function(results){
    if ($(results).length) {
      $("results").each(function(){
        // do something with update messages
      });
    }
  });
}, 30000); // Every 30 seconds.

Long Polling with PHP/jQuery Example:

You can find an example of long polling with PHP and jQuery at http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/

like image 172
Sampson Avatar answered Dec 31 '22 19:12

Sampson


You can use db triggers to watch for changes in certain tables and insert notification data into a new table. Then. query that db table periodically with Jquery and ajax.

Workflow:

  1. Create trigger that watched table users for inserts, updates, and deleted
  2. Once users is altered, the trigger inserts a new record into notifications detailing what was changed
  3. Using a periodical updater, check the notifications table for new records and display them to the user.

This simple workflow might not be as easy to implement as you would hope but it would get the job done in an efficient manner.

like image 25
Mike B Avatar answered Dec 31 '22 21:12

Mike B