Home » Php » Best way to save performance when getting realtime data

Best way to save performance when getting realtime data

Posted by: admin October 26, 2017 Leave a comment


Let’s assume PHPFile.php return random integer(X) every 2 seconds.

Now if X == 5 I would like to show an alert (client-side).

the following code will do that job :

$(function worker(){

    $.ajaxSetup ({
        cache: false,
        complete: function() {
          // Schedule the next request when the current one's complete
          setTimeout(worker, 2000);

            type: "GET",
            url: "PHPFile.php",

                if (result == 5){


Now the problem is when we call this every 2 seconds it’s like destroying the memory, especially if PHPFile.php(in example) has big size data.

30 Request every one minute and its counting.

enter image description here

So I wondering what is the best way to update client browser if a change occur, without refreshing the browser.

Maybe it’s like facebook notification system.


You might want to take a look at implementation of Long Polling with PHP or Comet with PHP.

Long polling is a variation of the traditional polling technique and
allows emulation of an information push from a server to a client.
With long polling, the client requests information from the server in
a similar way to a normal poll. However, if the server does not have
any information available for the client, instead of sending an empty
response, the server holds the request and waits for some information
to be available. Once the information becomes available (or after a
suitable timeout), a complete response is sent to the client. The
client will normally then immediately re-request information from the
server, so that the server will almost always have an available
waiting request that it can use to deliver data in response to an