0

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);
        }
    });

        $.ajax({
            type: "GET",
            url: "PHPFile.php",

            success:function(result){
                if (result == 5){
                alert('test');  
            }


        };
              });

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.

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
JackSmith
  • 85
  • 1
  • 10
  • You should take a look WebSocket like socketio with nodejs. The client is connected to the server so they can communicate with events only when needed :) Example: The server update some data, so he emit one event, the related clients are notified and so you can perform some actions client side – Mcsky Oct 26 '17 at 07:17
  • webSocket in my situation is not the best, since we are working with data belongs to specific user. webSocket doesn't deal with PHP files with same session as user – JackSmith Oct 26 '17 at 07:21
  • You can build a system of one room by user so you can notify specific user. You can check at the socket connection the cookie given by the client by calling your php and check if the session is valid. I don't see a limitation using socket to your case, it's just a little of work but it's totally adapt to your needs – Mcsky Oct 26 '17 at 09:50

1 Answers1

0

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 event.

Iurii Drozdov
  • 1,685
  • 1
  • 12
  • 23
  • Thanks for your answer, I will take a look and reply. – JackSmith Oct 26 '17 at 07:49
  • It's somehow what i'm looking for but i'm pretty sure it's not the same method that facebook uses. Because it leaves a pending connection to server file, also our php file keep making queries to mysql. What about making like callback function so when a value changed in database we handle these changes. – JackSmith Oct 26 '17 at 08:25
  • check this https://stackoverflow.com/questions/9579843/does-facebook-use-comet-or-long-polling-for-getting-new-stream-and-messages – Iurii Drozdov Oct 26 '17 at 08:30
  • helpful but it's since 9 years ago. – JackSmith Oct 26 '17 at 08:50
  • https://www.quora.com/Why-does-Facebook-use-long-polling-instead-of-WebSocket-in-order-to-instant-chat post is from 2014 but last answer is from 2016 – Iurii Drozdov Oct 26 '17 at 08:59
  • you should use long polling if you don't want to use websockets – Iurii Drozdov Oct 26 '17 at 08:59