开发者

How to see the content of a variable in a script that acts as a listener: Facebook real time update

开发者 https://www.devze.com 2023-04-11 15:42 出处:网络
I have two files, index.php and a callback.php. Index.php is used to display data on the website. The callback.php is a script that acts like a listener. The callback.php is called by a server and the

I have two files, index.php and a callback.php. Index.php is used to display data on the website. The callback.php is a script that acts like a listener. The callback.php is called by a server and the $update variable in callback.php is updated by the server in real time. I want to see the contents of $update variable every time it updates.

Callback.php:

    if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
        $_GET['hub_verify_token'] == VERIFY_TOKEN) {
      echo $_GET['hub_challenge'];

    } else if ($method == 'POST') {
     开发者_运维知识库  $updates = json_decode(file_get_contents("php://input"), true);
       //I want to see the content of $updates
       // resend the push notification again.
        error_log('updates = ' . print_r($updates, true));
     }

How can i do this? Please let me know. I would appreciate if you have some sample code i could start with.


The technique I believe you are describing is called long-polling.

Basically, the client sends an ajax request to the server and sits there waiting for a response. Upon some event (this is where SSJS like Node comes in really handy), the server sends the client the new data. Upon receiving the data, the client immediately sends a new request and waits for the next update.

Here is the flow:

Page loads:

 __________                              _________
|          |                            |         |
| Client A | ---> Request for Data ---> | Server  |
|__________|                            |_________|

Then what? Suspense:

 __________                              _________
|          |                            |         |
| Client A | .......................... | Server  |
| waiting  |                            | waiting |
|__________|                            |_________|

Someone else updates content you want to see in real time:

                     __________
                    |          |
                    | Client B |
                    |__________|
                         |
                         | Sends update to Server
                         |___________________.
                                             |
                                             V
 __________                              _________
|          |                            |         |
| client A | ............<------------- | Server  |
| waiting  |                            | Reacts  |
|__________|                            |_________|
     |
     |
     V    
 __________                              _________
|          |                            |         |
| Client A |   ( No open connection)    | Server  |
| Updates  |                            | Idle... |
|__________|                            |_________|
     |
     |
     V
 ___________                              _________
|           |                            |         |
| Client A  |                            |         |
| Reacts to |                            | Server  |
| Update    | ---> Request for Data ---> |         |
|___________|                            |_________|

Rinse, Repeat The thing to remember here is that HTTP1.1 supports a persistent connection, meaning it won't timeout unless you want it to. I'd recommend looking into NodeJS on your server side to trigger these events.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号