10

What would be the ideal mechanism to get notifications like in Facebook in to a dashboard ?. I am thinking the best way is to do a Ajax call to a php page every 5 seconds and retrieve the notifications.

Is there any better way to do a similar change?

It should work in all mobile browsers too.

I am doing it the following way,

use $.post in jquery for get data without page refreshing.

$.post("page.php",{"act":1},function(data){
    $("#id").html(data);
});
in page.php write your query

EDIT 1

I wrote a function like this after referring to some online notes and its working in realtime.

var TimeStamp = null;
      function waitForMsg() {
         $.ajax({
            type: "GET",
            url: "getData.php?timestamp=" + TimeStamp,
            async: true,
            cache: false,
            timeout: 50000, /* Timeout in ms */
//          data: "TimeStamp=" + TimeStamp,
            success: function( data ) {
               var json = eval('(' + data + ')');

               if ( json['msg'] != "" ) {
                  alert( json['msg'] );
               }
               TimeStamp = json['timestamp'];

               setTimeout(
                   'waitForMsg()', /* Request next message */
                   1000            /* ..after 1 seconds */
               );
            },
            error: function( XMLHttpRequest, textStatus, errorThrown ) {
               alert("error:" + textStatus + "(" + errorThrown + ")");
               setTimeout(
                   'waitForMsg()', /* Try again after.. */
                    "15000");       /* milliseconds (15seconds) */
               },
            });
         }
      ;

      // calling after dom is ready
      $(document).ready(function() {
          waitForMsg();
      });

PHP file is,

<?php
   $filename = dirname(__FILE__).'/data.txt';
   $lastmodif = isset( $_GET['timestamp'] ) ? $_GET['timestamp'] : 0;
   $currentmodif = filemtime( $filename );

   while ( $currentmodif <= $lastmodif ) {
      usleep( 10000 );
      clearstatcache();
      $currentmodif = filemtime($filename);
   }

   $response = array();
   $response['msg'] = file_get_contents( $filename );
   $response['timestamp'] = $currentmodif;
   echo json_encode($response);

EDIT 2

All work well but when there is no changed happend to data.txt file i get a error message like this in 50 seconds.

error:timeout(timeout)

how can this be prevented ?

REF : Javascript Variable Scope

Community
  • 1
  • 1
dev1234
  • 5,376
  • 15
  • 56
  • 115
  • 3
    I'd think ajax posts at regular intervals would be the best way to do it. But then it raises the question that if someday you get, let's say, a billion users out of which 300 million are logged in at any given point of time, that'd mean 300 million calls to the PHP page every 5 seconds!! I'm curious to know what other approaches are there. – DS. Oct 14 '13 at 04:46
  • @DS. yes thats true, but i am looking for other alternatives. – dev1234 Oct 14 '13 at 04:48
  • 2
    @mazraara if you want to do it right look into a websocket server, socket.io is a popular one. – Pablo Oct 14 '13 at 04:50
  • 1
    @mazraara - check this out, it might help - http://stackoverflow.com/questions/16860106/real-time-notification-from-web-server-to-client-bowers – DS. Oct 14 '13 at 04:50
  • @DS. pls check the edited question above. – dev1234 Oct 14 '13 at 06:49

1 Answers1

4

From what I know, there are basically two ways to do this: polling, and websockets. Polling is either making many requests at an interval, or having a very long request the browser and server know is long (also called long polling). Then there is websockets. I haven't been in PHP land in a while, but last I checked websockets weren't really supported there. It could have changed. In Node world socket.io is a great solution that uses websockets and long polling as a backup.

A quick search has found this for websockets and php: http://socketo.me/docs/

Zeke Nierenberg
  • 2,216
  • 1
  • 19
  • 30