This script continuously makes requests to msgsrv.php, displaying responses in a div. It uses a simple rate limiter to prevent excessive requests.

Benefits of Long Polling

Long Polling offers resilience and simplicity. In case of network interruptions, the client retries automatically. It's a suitable choice for applications requiring immediate updates from the server, such as instant messaging or chat applications.

","image":"http://www.luping.net/uploads/20250501/17460749126812fd207a6ee.jpg17460749126812fd207a6f7.jpg","datePublished":"2025-05-01T23:50:42+08:00","dateModified":"2025-05-01T23:50:42+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Apache and JavaScript implement basic long polling methods

Apache and JavaScript implement basic long polling methods

Posted on 2025-05-01
Browse:721

How to Implement Basic Long Polling Using Apache and JavaScript?

Achieving Basic Long Polling Implementation

Long Polling is a polling technique that keeps connections open for an extended period, facilitating instant response when server updates become available. Its implementation can be quite straightforward.

Apache as a Request Handler

To serve requests using Apache, create a PHP script like msgsrv.php. Here's a simple example:

// Simulate data availability after a random delay
sleep(rand(2, 10));
echo("Hi! Here's a random number: " . rand(1, 10));

Run this script on Apache. Note that using a high-volume webserver like Apache may result in exhausted worker threads.

Using a JavaScript Script

In JavaScript, you can use a script like long_poller.htm to initiate polling requests:

This script continuously makes requests to msgsrv.php, displaying responses in a div. It uses a simple rate limiter to prevent excessive requests.

Benefits of Long Polling

Long Polling offers resilience and simplicity. In case of network interruptions, the client retries automatically. It's a suitable choice for applications requiring immediate updates from the server, such as instant messaging or chat applications.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3