Claus Witt

software and web developer

Archive for the ‘dojo’ tag

Could MemcacheQ be Used For Cometd?

without comments

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

Written by Claus Witt

April 7th, 2009 at 10:32 pm

Posted in Web development

Tagged with , ,

New Versions of Dojo and Plugd Released

without comments

I have not posted for about a week now, the main reason is that I am working on something very exciting, which will be features here in the comming days, if all works out.

Today, while reading the most recent posts on Ajaxian, I found the announcement of the Dojo 1.3 release. Read the announcement and learn about the plugd plugin.

I will be using some time to update our project, which by the way will be going into the first testphase in 13 days, to the newest version of Dojo Toolkit today.

Written by Claus Witt

April 1st, 2009 at 7:16 am

Posted in Web development

Tagged with , ,

Plugd – Dojo: the missing APIs

without comments

Just today, while reading about the release of the Dojo Toolkit 1.3 beta, I learned about the plugd addition to Dojo Toolkit. The author of plugd describes it this way.

The API’s found in plugd are loosely based on “my favorite bits of jQuery” … this is not meant to be a ‘compatibility shim’ of any kind, it is simply the addition of functions and helpers to Dojo Base. Added magic.

plugd adds several convenience functions, all located in the dojo namespace. For instance, by loading base.js into a page, the following new Dojo API’s are made available:

* dojo.show(), dojo.hide(), dojo.toggle()
* dojo.conflict()
* dojo.wrap()
* dojo.create()

And the following methods are added to dojo.NodeList (what you get back from calling dojo.query() (or $() after running dojo.conflict()):

* .wrap()
* .show(), .hide(), .toggle()
* .val()
* .append()
* .appendTo()
* .create()
* .animate()
* .destroy()
* .hover()
* .end() (and stash() for private use)

Written by Claus Witt

March 9th, 2009 at 4:00 pm

Posted in Development,Web development

Tagged with , ,

Dojo Toolkit 1.3 Beta is Out

with one comment

Alex Russel just (well a couple of days ago) anounced that the beta of the newest version of Dojo Toolkit is available. I’m looking forward to the actual release of this version, it seems very very fast.

Dojo has always (since I got around to learning about javascript frameworks anyway) been my favourite js-framework. Maybe this is just because it was the first one I learned to use. But it is built a way that seems very intuitive to me. I guess (if I had been a superstar js-developer) I would have done everything just as they have done. I am looking forward to learning even more about the new version of Dojo Toolkit, and creating even more applications using it in the near future.

Written by Claus Witt

March 9th, 2009 at 3:30 pm

Posted in Development,Web development

Tagged with ,

Upload Progress Bar using Php and Dojo

without comments

For a long time I have wanted my applications to have progress bars on uploads, but I have always thought that this was impossible when the server language was php. Unless of course you wanted to do some mixing of languages, and handle the upload via perl. I, however, wanted complete integration of the code, and wanted my mvc framework to be able to output the wanted information. A while ago my boss at Voicearchive asked me if we could get a progress bar for uploads on the system that we are developing. Once again I found myself searching for possible solutions.

This time around, however, I found what I was looking for. I found an article about PHP and jQuery upload progress bar. In this article I learned about the PECL extension uploadprogress. I began trying to make this solution work.

First thing, was that I found the extension did not work as I wanted. After a while I found that installing newest version, required installing it via “pecl install channel://pecl.php.net/uploadprogress-0.9.2″ – which gave me the latest beta release of the extension. Now it returned the correct information when I uploaded a file.

The solution consists of 5 parts.

  1. The Pecl extension
  2. The form
  3. The progressbar widget
  4. The javascript
  5. and the controller which sends the data from uploadprogress as json to the client

After the installation was managed, I changed the form to have the required hidden UPLOAD_IDENTIFIER value included.


The value of the field is set when the dialogbox containing the upload form is revealed, and saved in a variable, that can be used when the script needs to poll for the result.

lastFileProgressKey = guid();
console.log(lastFileProgressKey);
dojo.byId('addFileProgressKey').value = lastFileProgressKey;
dijit.byId('addFileDialog').show();

The guid function is just a simple function which generates a guid-like random string. The value is then set to the form, and the dialog is revealed.

When the upload button on the form is pressed it calls a event handler which handles the actual upload. Before the upload is started however the following function is called.

startUploadCheck(lastFileProgressKey);

These function do the actual work.

function updateProgressBar(response) {
	if(response) {
		totalBytes = parseInt(response.bytes_total);
		uploadedBytes = parseInt(response.bytes_uploaded);
		percentage = Math.floor(100 * uploadedBytes/ totalBytes);
		uploadProgressBar.update({ progress: percentage+'%'});
		if(lastUploadId!='') {
			setTimeout("checkUpload()", 1000);
		}
	} else {
		if(lastUploadId!='') {
			setTimeout("checkUpload()", 1000);
		}
	}
}

function checkUpload() {
	dojo.xhrGet( {
		url :'/uploadprogress/check?uploadid='+lastUploadId,
		handleAs: 'json',
		preventCache: true,
		load: updateProgressBar,
		error: function(data) {
			if(lastUploadId!='') {
				setTimeout("checkUpload()", 1000);
			}
		}
	});
}

function startUploadCheck(uploadid) {
	dijit.byId('uploadProgressDialog').show();
	lastUploadId = uploadid;
	checkUpload( uploadid );
}

function hideProgressDialog() {
	lastUploadId = '';
	dijit.byId('uploadProgressDialog').hide();
}

The dojo widget used is the ProgressBar.

The controller is the simplest part of the equation.

public function check() {
    $formUploadId = $this->request['uploadid'];
    return json_encode(uploadprogress_get_info($formUploadId));
}

All the legwork is done by uploadprogress_get_info, which then is passed through json_encode.

Written by Claus Witt

February 25th, 2009 at 1:00 pm

Posted in Development,Web development

Tagged with , ,