How to make a request on the server using the COMET method?

2

I have a system, in which it displays database files, in list form.

I query the database every 5 seconds using the Polling method.

How do I use the Comet method? Since this method is not asking for server information all the time.

    
asked by anonymous 15.09.2014 / 16:47

1 answer

1

It's simpler than I originally thought .. Basically, you have a page that does nothing, until the data you want to send is available (for example, a new message is received).

Here is a very basic example, which sends a simple string after 2-10 seconds. 1 in 3 chances of returning a 404 error (to show handling in the Javascript example that comes next)

msgsrv.php

<?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));
?>

Note: With a real site running this on a regular web server, as Apache will quickly tie up all "work segments" and leave you unable to respond to other requests .. There are ways to get around this, but it is recommended to write a "long poll server" on something like Python ( link ), which does not depend on a thread on request. [cometd] ( link ) is a popular one (which is available in several languages), and [Tornado] (

15.09.2014 / 19:04