Phomet: A PHP Cometd publisher

Created a small class last night that will allow you to connect, and then publish to a comet server. Code available here. Usage is simple.

$oPhomet = new Bayeux(’http://whereis/my/cometd’);

$oPhomet->publish(’/some/channel’, array(’user’ => ‘derper’, ‘message’ => ‘wee’));

The deconstructor takes care of disconnected you from the server, but if you wish, you can use ->diconnect() before.

This was never really meant to do much beyond simple publishing from PHP. The main limitation that prevent this from working in PHP is, once you have a connection left open (which PHP can do), there is not practical way to allow one connection to communicate with another.

The way Java servlets work allows one thread to wake up the rest and alert them to new changes, propagating to each client. PHP does not allow this.

The closest I have come to Comet in PHP was using a web server called Nanoserv, its implemented in 100% PHP and has the ability to fork off clients but still allow them to communicate with one another with 1 second latency. The experiments I did with it showed it could be done, but I did not continue because the Nanoserv project seems dead.

Leave a Comment