Could MemcacheQ be Used For Cometd?

I have been wondering for a couple of weeks would it be possible to use MemcacheQ as a kind of backend for Cometd? The reason I find it interesting is that sending message to the queue(s) would be as easy as

memcache_set($memcache_obj, $memcache_queue_name, $msg, 0, 0);

In other words easy as pie.

Since there are all sorts of client libraries for memcached (and thus for memcacheq as well, since it uses the same protocol). I have been tinkering a bit last weekend, with some minor tests. I based it on Comep since I knew nothing of writing daemons in php or the comet protocol for that mather. (I would just like to use it).

Now I am not sure if Comep is even compatible with Dojo’s dojox.cometd.subscribe – and I have not gotten around to test that either. But since I have not had the time to look further into this, I thought I would just post my ideas, and hopefully someone – might even be me, once things quiets down, picks up the pieces and makes something great out of it.

// Set the ip and port we will listen on
$address = '192.168.1.3';
$port = '666';
$max_clients = 10;

/* connect to memcached server */
$memcache_obj = memcache_connect('127.0.0.1', 22201);
if(!$memcache_obj) {
	die('Memcache object is false');
}
$memcache_queue_name = 'queue1';

$children = array();
// Array that will hold client information
$clients = Array();
$receiveClients = Array();
$client_ID = array();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
socket_getsockname($sock, $socket_address, $socket_port);

echo "COMET-Server startet on Port: ".$socket_port."\n";

// Start listening for connections
socket_listen($sock);
$lastPingTime = 0;
// Loop continuously
while (true) {
    // Setup clients listen socket for reading
    $read = array();
    $read[0] = $sock;
    for ($i = 0; $i < $max_clients; $i++) {
        if ($client[$i]['sock']  != null) $read[$i + 1] = $client[$i]['sock'] ;
    }
    // Set up a blocking call to socket_select()
    $ready = socket_select($read,$A=null,$B=null,1);

    /* if a new connection is being made add it to the client array */
    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++) {
            if ($client[$i]['sock'] == null) {
                $client[$i]['sock'] = socket_accept($sock);
				echo 'New client';
                break;
            } else if ($i == $max_clients - 1) {

			}
        }
        if (--$ready <= 0)
            continue;
    } // end if in_array
    $value = json_encode(memcache_get($memcache_obj, $memcache_queue_name));
	if($value && $value!="false") {
    	echo 'Sending value to queue: ' . $value . "\n";
		for ($i = 0; $i < $max_clients; $i++) { // for each client
	        if (in_array($client[$i]['sock'] , $read)) {
				socket_write($client[$i]['sock'], "HTTP/1.0 200 OK\r\n\r\n");
				socket_write($client[$i]['sock'], $value . "\r\n\r\n");
	        } else {
	            // Close the socket
	            if(!is_null($client[$i]['sock'])) {
					socket_close($client[$i]['sock']);
				}
	            unset($client[$i]["sock"]);
	        }
	    }
	}

} // end while
// Close the master sockets
socket_close($sock);
by Claus Witt

April 7, 2009  Tags: , ,   Posted in: Web development