Php blocked during ajax long polling

just come into contact with real-time communication, I know that using websocket is more efficient, but I want to know the implementation process of polling, step by step

the timer setInterval for short polling has been implemented, but when the background enters the endless loop module during long polling, the php page of the entire website becomes unresponsive, such as refreshing the page and submitting messages. The specific code is as follows:

chat.php:

<div class="chat_content_input">
            <div>
                <textarea name="chat_input" class="chat_input"
                          style="width: 570px;height: 120px;margin: 5px;resize: none"></textarea>
            </div>
            <div style="text-align: right">
                <button class="chat_post"></button>
            </div>
</div>

chat.js:

//
            var setting = {
                type: "POST",
                dataType: "html",
                url: "./util/action.php?action=message_pull",
                data: {uid_get: $("-sharpchat_content").attr("uid")},//uid
                success: function (msg) {
                    if (msg.length > 0) {
                        $(".chat_content_list_table").append(msg);//
                    }
                    $.ajax(setting);//
                }
            };
            $.ajax(setting);
The corresponding function in

action.php:

function message_pull($conn)
{
    session_start();
    session_write_close();//session
    $uid_post = $_SESSION["userinfo"][0]["id"];//uid
    $uid_get = $_POST["uid_get"];//uid
    $message_list = "";

    //
    while (true) {
        //
        foreach (select($conn, "message", "(uid_get=$uid_post AND uid_post=$uid_get AND read_flag<>1)") as $message) {
            update($conn, "message", "read_flag=1", "id={$message["id"]}");//
            $message_list .= "<tr class="content_list_post" style="text-align: right;font-size: 18px"><td>" . $message["content"] . "</td></tr><tr><td style="text-align: right;font-size: 8px">" . $message["post_time"] . "</td></tr>";
        }
        if (strlen($message_list) > 0) {
            echo $message_list;//
            break;//
        }else 
            sleep(1);//1s
    }
    mysqli_close($conn);
}

after testing, I found that once the php background enters the while loop, it will be blocked. But I saw that many demo on the Internet wrote this, so I felt very confused. Thank you for your advice

.

reference link: https://www.cnblogs.com/zhenb.

Mar.09,2021

although do not understand php, but literally, this will certainly die. Long polling means setting timeout to high, not without timeout . As your code shows, if there is no new message , the while (true) will be stuck all the time, there should be no new message and the timer will be cut off.


should be caused by php's session. Session is exclusive, so when you block other requests from the same client during a long training rotation, you can add session_write_close () before the cycle to try, so there should be no blocking.


the live chat I wrote with long polling, you can refer to
http://blog.51cto.com/phpme/1.


it doesn't work if you've tried to add session_write_close (); or session_commit ();, for session locks.

this long polling card gives me the illusion that php can only be processed in a single thread.


once you request an action.php, a php while will be created and the php will run in an endless loop. Of course, it will die. It is the ajax that requests the php, not the php loop, to send a message to the foreground. Your while is completely meaningless. It is only correct when you ask the background to query the data or change the status.


the example you see is supposed to distinguish between server and worker . Your while applies to the worker process; the interface fits to server . In addition, you don't do long polling for ajax . But rely on the front-end timer, uninterrupted to request the back-end. The backend just renders the data.


post stores the database in a table. When the pull function has data, it interrupts and blocks. You have no data to update the pull function. It must be blocked

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb0e8d-480d6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb0e8d-480d6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?