Creating Comet Applications In 3 Minutes Date: March 16, 2008 Tags: comet , javascript , pi Comments(10)
First version of my pi library has been released.This version contains a comet class and you can make comet requests in your applications by using it.Tutorial: Getting unix time from server(Sample File, Source)
Firstly, create an html file and import pi.js:
<script type="text/javascript" src="pi.js"></script>
Then, send a request to push.php to get unix time from server:
var request = new pi.comet();
request.environment.setUrl("push.php");
request.event.push = function(RESPONSE){
document.title = "UNIX TIME AT SERVER: "+RESPONSE;
};
request.send();
Finally, create an infinite loop at push.php, import pi.pushData function to this file and print the data which will be updated each time;
require_once "pi.pushData.php";
$type = $_GET["cometType"];
$name = $_GET["cometName"];
while(true){
pushData(time(),$type,$name);
ob_flush();
flush();
sleep(1);
}
That's all! You can get more information at pi.comet's wiki page.
Comments:
Hi,
This is really great. However, I am facing two issues:
1. The push function is always called twice when data is sent, even though the server sends the data only once.
2. This is not working in IE for me - however, I need to look into this more, so don't worry about it for now.
Does anyone have an idea why the push function is called twice?
Thanks,
Steffen
Azer
Thanks for sharing your code. People like you make the world go round :D
Cheers,
Phil
Hi Sentry, I will add .Net and Rails as soon as possible.For now, you can check Python example that is different from the PHP example.Thanks..
How would one implement the infinite loop in .NET or Rails. I think that .NET would crash if I try and make an infinite loop.
cool!!!thank u for bringing them all to us!great work!thx!
@rk, it works in ie7 and ie6.
Do you know why it does not work in IE7?
Hello Azer,
That's great.
I was thinking something of similar for Passpack. I think I will use your library (after my tests :)
Thanks,
F
Wow this is amazing, well done.
Have you checked out what kind of scalability you can get? It seems to me it might be worth plugging your front end code to an event-based backend so that you aren't busy-waiting on the server.

Hi Steffen,
It will be fixed on new version.