Search code examples
javascriptapachepush-notificationcometlong-polling

Fake a push notification with ajax/setInterval?


I'm wondering about the pros and cons of handling the auto-updating of a <div> on server vs. client side. I'm using Apache with PHP but was just thinking of faking a push notification in Javascript like this:

setInterval(queryDatabaseForUnreadMessages, 60000);

function queryDatabaseForUnreadMessages(){
   $.ajax({
       url: "/messages/queryDatabaseForUnreadMessages",
       success:function(data){
          $('div#littleRedCircle').html(data);
       }
   });
}

I'd just like to set up a notification like Stackoverflow has done (little red circle with a number in it) to let people know they've received a new message if one exists. Is that simple AJAX/setInterval combo a bad idea?


Solution

  • From IETF DOC

    Long Polling in Contrast with Pull

    What are the issues with Long Polling though? From DOC

    1. Header Overhead
    2. Maximal Latency
    3. Connection Establishment
    4. Allocated Resources
    5. Graceful Degradation
    6. Timeouts & Caching

    As I mentioned in my comment

    Long Polling is realtime whereas Pull is near realtime [determined by the poll interval]

    Pull takes the client's bandwidth for granted :P

    As mentioned in the DOC both the techniques makes use of persistence connection of HTTP 1.1 well.

    Pull is easy to implement and well supported across browsers. While Push lacks that but libraries are there to rescue ;).