Home > Backend Development > PHP Tutorial > How Does Long Polling Work and How Can It Be Implemented Using Apache, PHP, and Javascript?

How Does Long Polling Work and How Can It Be Implemented Using Apache, PHP, and Javascript?

Linda Hamilton
Release: 2024-12-26 16:46:15
Original
579 people have browsed it

How Does Long Polling Work and How Can It Be Implemented Using Apache, PHP, and Javascript?

Implementing Basic Long Polling: A Simple Guide

Long polling is a technique used to enable the server to push data to the client without the client explicitly requesting it. This is useful in scenarios where the server needs to continuously monitor data and notify the client when new data arrives.

How does Long Polling work?

In long polling, the client makes a request to the server and waits for a response. If no data is available, the server keeps the request open indefinitely, instead of closing it like in a regular HTTP request. When new data becomes available, the server sends it to the client and closes the request.

Implementing Long Polling in Apache and PHP

To implement long polling using Apache and PHP:

  1. Create a PHP script (msgsrv.php): This script will handle the client's request and send a response when data becomes available.
  2. Setup Apache to serve the PHP script: Configure your Apache server to handle requests to msgsrv.php.

Client-side Implementation using Javascript

To implement long polling on the client side using Javascript:

  1. Create an initial request to the server: The client makes a request to msgsrv.php and waits for a response.
  2. Handle successful responses: If a response is received, append it to a designated element on the page and trigger a new request after a delay.
  3. Handle errors: If the request fails, display an error message and retry after a longer delay.

Example Code

PHP Script (msgsrv.php):

if (rand(1, 3) == 1) {
    // Fake an error
    header("HTTP/1.0 404 Not Found");
    die();
}

// Send a string after a random number of seconds (2-10)
sleep(rand(2, 10));
echo("Hi! Have a random number: " . rand(1, 10));
Copy after login

Javascript Code (long_poller.htm):

<script type="text/javascript">
function waitForMsg() {
    $.ajax({
        type: "GET",
        url: "msgsrv.php",
        async: true,
        cache: false,
        timeout: 50000,

        success: function (data) {
            // Add response to a .msg div (with the "new" class)
            addmsg("new", data);
            setTimeout(waitForMsg, 1000); // Request next message after 1 second
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // Add error message
            addmsg("error", textStatus + " (" + errorThrown + ")");
            setTimeout(waitForMsg, 15000); // Retry after 15 seconds
        }
    });
};

$(document).ready(function () {
    waitForMsg(); // Start the initial request
});
</script>
Copy after login

The above is the detailed content of How Does Long Polling Work and How Can It Be Implemented Using Apache, PHP, and Javascript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template