user472521
user472521

Reputation:

Need explanation of comet program

I am new to Comet programming. I searched through and write the code like this

<script type="text/javascript" charset="utf-8">           

        function waitForMsg(){                
            $.ajax({
                type: "GET",
                url: "getMessage.php",
                async: true, 
                cache: false,
                timeout:50000, 
                success: function(data){                        
                    $('#messages).append(data);
                    setTimeout(
                    'waitForMsg()',
                    1000
                );
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    addmsg("error", textStatus + " (" + errorThrown + ")");
                    setTimeout(
                    'waitForMsg()', 
                    "15000");
                },
            });
        };

        $(document).ready(function(){                
            waitForMsg();               

        });
    </script>

I am getting update from getMessage.php when ever i am passing a message to getMessage.php

But my doubt is i used

setTimeout('waitForMsg()',1000);

What this means. If we are calling waitForMsg() for every 1 sec then what is the difference between Ajax and Comet programming.

Upvotes: 0

Views: 523

Answers (2)

J.C. Yamokoski
J.C. Yamokoski

Reputation: 1014

Regarding your example, the difference between Ajax and Comet depends on how getMessage.php responds to the request from the client.

Using Ajax, getMessage.php would return immediately even if there are no new messages to return. The client would then wait the specified interval and then send another request, again either receiving a new message or nothing at all. End result is that the quickest the client will ever update with a new message is the specified interval.

Using Comet, on the other hand, getMessage.php would not return until it has something to return. Meaning on initial request from the client it would check for new messages, if none are available it would sleep for a certain amount of time and check again. This process would continue until a new message is available or a timeout occurs, at which point the client would send another request to the server and the process starts over. End result in this case is that the client would appear to update instantaneously.

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284806

There are multiple kinds of COMET. You're using AJAX polling, but there are other techniques, like forever-frame, that do not involve polling.

Forever-frame uses a hidden infinitely long iframe that the server pushes script tags to as needed.

Also note that some uses of AJAX polling (e.g. as explained by the Wikipedia) do not include a setTimeout, since they immediately start a new AJAX request in the success handler.

Also (with the AJAX polling example), you can just use:

setTimeout(waitForMsg, 1000);

There is no need for a string.

Upvotes: 0

Related Questions