Long polling - Message system

旧城冷巷雨未停 提交于 2019-12-20 09:39:41

问题


I'm looking into doing some long polling with jQuery and PHP for a message system. I'm curious to know the best/most efficient way to achieve this. I'm basing is off this Simple Long Polling Example.

If a user is sitting on the inbox page, I want to pull in any new messages. One idea that I've seen is adding a last_checked column to the message table. The PHP script would look something like this:

query to check for all null `last_checked` messages
if there are any...
while(...) {
    add data to array
    update `last_checked` column to current time
}
send data back

I like this idea but I'm wondering what others think of it. Is this an ideal way to approach this? Any information will be helpful!

To add, there are no set number of uses that could be on the site so I'm looking for an efficient way to do it.


回答1:


Yes the way that you describe it is how the Long Polling Method is working generally. Your sample code is a little vague, so i would like to add that you should do a sleep() for a small amount of time inside the while loop and each time compare the last_checked time (which is stored on server side) and the current time (which is what is sent from the client's side).

Something like this:

$current = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$last_checked = getLastCheckedTime(); //returns the last time db accessed

while( $last_checked <= $current) {
    usleep(100000);
    $last_checked = getLastCheckedTime();
}

$response = array();
$response['latestData'] = getLatestData() //fetches all the data you want based on time
$response['timestamp'] = $last_checked;
echo json_encode($response);    

And at your client's side JS you would have this:

function longPolling(){
        $.ajax({
          type : 'Get',
          url  : 'data.php?timestamp=' + timestamp,
          async : true,
          cache : false,

          success : function(data) {
                var jsonData = eval('(' + data + ')');
                //do something with the data, eg display them
                timestamp  = jsonData['timestamp'];
                setTimeout('longPolling()', 1000);
          },
          error : function(XMLHttpRequest, textstatus, error) { 
                    alert(error);
                    setTimeout('longPolling()', 15000);
          }     
       });
}



回答2:


Instead of adding new column as last_checked you can add as last_checked_time. So that you can get the data from last_checked_time to the current_time.

(i.e) DATA BETWEEN  `last_checked_time` AND `current_time`



回答3:


If you only have one user, that's fine. If you don't, you'll run into complications. You'll also run one hell of a lot of SELECT queries by doing this.

I've been firmly convinced for a while that PHP and long polling just do not work natively due to PHP not having any cross-client event-driven possibilities. This means you'll need to check your database every second/2s/5s instead of relying on events.

If you still want to do this, however, I would make your messaging system write a file [nameofuser].txt in a directory whenever the user has a message, and check for message existence using this trigger. If the file exists and is not empty, fire off the request to get the message, process, feed back and then delete the text file. This will reduce your SQL overhead, while (if you're not careful) increasing your disk IO.

Structure-wise, an associative table is by far the best. Make a new table dedicated to checking the status, with three columns: user_id message_id read_at. The usage should be obvious. Any combination not in there is unread.




回答4:


Instead of creating a column named last_checked, you could create a column called: checked. If you save all messages in the database, you could update the field in the database. Example:

  1. User 1 sends User 2 a message.
  2. PHP receives the message using the long-polling system and saves the message in a table.
  3. User 2, when online, would send a signal to the server, notifying the server that User 1 is ready to receive messages
  4. The server checks the table for all messages that are not 'checked' and returns them.


来源:https://stackoverflow.com/questions/15743388/long-polling-message-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!