<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://organizeseries.com/"
	>

<channel>
	<title>SitePen Blog &#187; Persevere</title>
	<atom:link href="http://www.sitepen.com/blog/category/persevere/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepen.com/blog</link>
	<description>SitePen Services and notes about Dojo, Persevere, CometD, JavaScript, and the Web</description>
	<lastBuildDate>Wed, 15 May 2013 22:02:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Dojo WebSocket with AMD</title>
		<link>http://www.sitepen.com/blog/2012/11/05/dojo-websocket-with-amd/</link>
		<comments>http://www.sitepen.com/blog/2012/11/05/dojo-websocket-with-amd/#comments</comments>
		<pubDate>Mon, 05 Nov 2012 19:23:29 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[comet]]></category>
		<category><![CDATA[Cometd]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=5475</guid>
		<description><![CDATA[<p>Dojo has an API for Comet-style real-time communication based on the WebSocket API. WebSocket provides a bi-directional connection to servers that is ideal for pushing messages from a server to a client in real-time. Dojo&#8217;s dojox/socket module provides access to this API with automated fallback to HTTP-based long-polling for browsers (or servers) that do not [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>Dojo has an API for Comet-style real-time communication based on the WebSocket API. WebSocket provides a bi-directional connection to servers that is ideal for pushing messages from a server to a client in real-time. Dojo&#8217;s  <code>dojox/socket</code> module provides access to this API with automated fallback to HTTP-based long-polling for browsers (or servers) that do not support the new WebSocket API. This allows you start using this API with Dojo now.</p>
<p><span id="more-5475"></span></p>
<p>The <code>dojox/socket</code> module is designed to be simple, lightweight, and protocol agnostic. In the past Dojo has provided protocol specific modules like CometD and RestChannels, but there are numerous other Comet protocols out there, and <code>dojox/socket</code> provides the flexibility to work with virtually any of them, with a simple foundational interface. The <code>dojox/socket</code> module simply passes strings over the HTTP or WebSocket connection, making it compatible with any system.</p>
<p>The simplest way to start a <code>dojox/socket</code> is to simply call it with a URL path:</p>
<pre lang="javascript">
require(["dojox/socket"], function (Socket) {
	// Create socket instance
	var socket = new Socket("/comet");
});
</pre>
<p>The socket module will then connect to the origin server using WebSocket, or HTTP as a fallback. We can now listen for message events from the server:</p>
<pre lang="javascript">
socket.on("message", function(event){
	var data = event.data;
	// do something with the data from the server
});
</pre>
<p>Here we use the <code>socket.on()</code> event registration method (inspired by <a href="http://github.com/LearnBoost/Socket.IO">socket.io</a> and NodeJS&#8217;s registration method) to listen to &#8220;message events&#8221; and retrieve data when they occur. This method is also aliased to the deprecated Dojo style <code>socket.connect()</code>.</p>
<p>We can also use <code>send()</code> to send data to the server. If you have just started the connection, you should wait for the <code>open</code> event to ensure the connection is ready to send data:</p>
<pre lang="javascript">
socket.on("open", function(event){
  socket.send("hi server");
});
</pre>
<p>Finally, we can listen for the connection being closed by the server or network by listening for the <code>close</code> event. And we can initiate the close of a connection from the client by calling <code>socket.close()</code>.</p>
<p>The <code>dojox/socket</code> method can also be called with standard Dojo IO arguments to initiate the communication with the server. This makes it easy to provide any necessary headers for the requests. For example:</p>
<pre lang="javascript">
var socket = new Socket({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json"
	}});
</pre>
<p>This will automatically translate the relative URL path to a WebSocket URL (using <code>ws://</code> scheme) or an HTTP URL depending on the browser capability.</p>
<p>For some applications, the server may only support HTTP/long-polling (without real WebSocket support). We can also explicitly create a long-poll based connection:</p>
<pre lang="javascript">
var socket = new Socket.LongPoll({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json"
	}});
</pre>
<p>We can also provide alternate transports in the socket arguments object. This would allow us to use the <code>get()</code> method in <code>dojo/io/script</code> to connect to a server. However, a more robust solution is to use the <a href="http://www.sitepen.com/blog/2008/07/31/cross-site-xhr-plugin-registry/"><code>dojox/io/xhrPlugins</code></a> for cross-domain long-polling, which will work properly with <code>dojox/socket</code>.</p>
<h2>Auto-Reconnect</h2>
<p>In addition to <code>dojox/socket</code>, we have also added a <code>dojox/socket/Reconnect</code> module. This wraps a socket, adding auto-reconnection support. When a socket is closed by network or server problems, this module will automatically attempt to reconnect to the server on a periodic basis, with a back-off algorithm to minimize resource consumption. We can upgrade a socket to auto-reconnect by this simple code fragment:</p>
<pre lang="javascript">
require(["dojox/socket", "dojox/socket/Reconnect"], 
	function (Socket, Reconnect) {
	// Create socket instance
	var socket = new Reconnect(new Socket("/comet"));
});
</pre>
<h2>Using Dojo WebSocket with Object Stores</h2>
<p>One of the other big enhancements in Dojo is the <a href="http://dojotoolkit.org/reference-guide/1.8/dojo/store.html#dojo-store">Dojo object store API</a> (which supercedes the Dojo Data API), based on the HTML5 IndexedDB object store API. Dojo comes with several store wrappers, and the <code>Observable</code> store provides notification events that work very well with Comet driven updates. <code>Observable</code> is a store wrapper. To use it, we first create a store, and then wrap it with Observable:</p>
<pre lang="javascript">
require([
	"dojo/store/JsonRest",
	"dojo/store/Observable"
], function(JsonRest, Observable){
	var store = Observable(new JsonRest({data:myData}));
});
</pre>
<p>This store will now provide an <code>observe()</code> method on query results that widgets can use to react to changes in the data. We can notify the store of changes from the server by calling the <code>notify()</code> method on the store:</p>
<pre lang="javascript"> 
socket.on("message", function(event){
  var existingId = event.data.id;
  var object = event.data.object;
  store.notify(object, existingId);
});
</pre>
<p>We can signal a new object by calling <code>store.notify()</code> and omitting the id, and a deleted object by omitting the object (undefined). A changed/updated object should include both.</p>
<h3>Handling Long-Polling from your Server</h3>
<p>Long-polling style connection emulation can require some care on the server-side. For many applications, the server may have sufficient information from request cookies (or other ambient data) to determine what messages to send the browser. However, other applications may vary on what information should be sent to the browser during the life of the application. Different topics may be subscribed to and unsubscribed from. In these situations, the server may need to correlate different HTTP requests with a single connection and its associated state. While there are numerous protocols, one could do this very easily be defining a unique connection and adding that as a header for the socket (the headers are added to each request in the long-poll cycles). For example, we could do:</p>
<pre lang="javascript">
require([
	"dojox/socket"
], function(Socket){
	var socket = Socket.LongPoll({
		url:"/comet",
		headers: {
			"Accept": "application/json",
			"Content-Type": "application/json",
			"Client-Id": Math.random()
		}});
});
</pre>
<p>In addition, <code>dojox/socket</code> includes a <code>Pragma: long-poll</code> to indicate the first request in a series of long-poll requests to help a server ensure that the connection setup and timeout is properly handled.</p>
<p>We can easily use <code>dojox/socket</code> with other protocols as well:</p>
<h3>CometD</h3>
<p>To initiate a Comet connection with a <a href="http://cometd.org/">CometD</a> server, we can do a CometD handshake, connection, and subscription:</p>
<pre lang="javascript">
var socket = new Socket("/cometd");
function send(data){
  return socket.send(json.stringify(data));
}
socket.on("connect", function(){
  // send a handshake
  send([
    {
       "channel": "/meta/handshake",
       "version": "1.0",
       "minimumVersion": "1.0beta",
       "supportedConnectionTypes": ["long-polling"] // or ["callback-polling"] for x-domain
     }
  ])
  socket.on("message", function(data){
    // wait for the response so we can connect with the provided client id
    data = json.parse(data);
    if(data.error){
      throw new Error(error);
    }
    // get the client id for all future messages
    clientId = data.clientId;
    // send a connect message
    send([
      {
         "channel": "/meta/connect",
         "clientId": clientId,
         "connectionType": "long-polling"
       },
       {  // also send a subscription message
         "channel": "/meta/subscribe",
         "clientId": clientId,
         "subscription": "/foo/**"
       }
    ]);
    socket.on("message", function(){
      // handle messages from the server
    });
  });
});
</pre>
<h3>Socket.IO</h3>
<p><a href="http://socket.io/">Socket.IO</a> provides a lower-level interface like <code>dojox/socket</code>, providing simple text-based message passing. Here is an example of how to connect to a Socket.IO server:</p>
<pre lang="javascript">
require([
	"dojo/request", "dojox/socket"
], function(request, Socket){
	var
		args = {},
		ws = typeof WebSocket != "undefined",
		url =  ws ? "/socket.io/websocket" : "/socket.io/xhr-polling";
		
	var socket = new Socket(args = {
		url:url,
		headers:{
			"Content-Type":"application/x-www-urlencoded"
		},
		transport: function(args, message){
			args.data = message; // use URL-encoding to send the message instead of a raw body
			request.post(url, args);
		}
	});
	var sessionId;
	socket.on("message", function(){
		if (!sessionId){
			sessionId = message;
			url += '/' + sessionId;
		}else if(message.substr(0, 3) == '~h~'){
			// a heartbeat
		}
	});
});
</pre>
<h3>Comet Session Protocol</h3>
<p>And here is an example of connecting to a <a href="http://orbited.org/csp/">Comet Session Protocol</a> server (the following example was tested with <a href="http://orbited.org/">Orbited</a>, but could work with <a href="http://hookbox.org/">Hookbox</a>, APE, and others):</p>
<pre lang="javascript">
require([
	"dojo/json", "dojox/socket"
], function(json, Socket){
	var args, socket = new Socket(args = {
		url: "/csp/handshake"
	});
	function send(data){
		return socket.send(json.stringify(data));
	}
	var sessionId = Math.random().toString().substring(2);
	socket.on("connect", function(){
		send({session:sessionId});
		socket.on("message", function(){
			args.url = "/csp/comet";
			send({session:sessionId});
		});
	});
});
</pre>
<h3>Tunguska</h3>
<p><a href="http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/">Tunguska</a> provides a Comet-based interface for subscribing to data changes. This is a very simple protocol which allows us to communicate with a Tunguska server:</p>
<pre lang="javascript">
var socket = new Socket({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json",
		"Client-Id": Math.random()
	}});  
function send(data){
	return socket.send(json.stringify(data));
}
socket.on("connect", function(){
	// now subscribe to all changes for MyTable
	send([{"to":"/MyTable/*", "method":"subscribe"}]);
});
</pre>
<h2>Conclusion</h2>
<p>Dojo&#8217;s socket API is a flexible simple module for connecting to a variety of servers and building powerful, efficient real-time applications without constraints. This adds to the array of awesome features in Dojo.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2012/11/05/dojo-websocket-with-amd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Now Supporting all Major Toolkits!</title>
		<link>http://www.sitepen.com/blog/2012/07/19/now-supporting-all-major-toolkits/</link>
		<comments>http://www.sitepen.com/blog/2012/07/19/now-supporting-all-major-toolkits/#comments</comments>
		<pubDate>Thu, 19 Jul 2012 15:20:25 +0000</pubDate>
		<dc:creator>Dylan Schiemann</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[AMD]]></category>
		<category><![CDATA[Cometd]]></category>
		<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[dgrid]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[Support]]></category>
		<category><![CDATA[backbone]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[support]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=4951</guid>
		<description><![CDATA[<p>We have been providing JavaScript and Dojo support to freelancers, start-ups and Fortune 500 companies for nearly a decade. As we intently watch enterprise organizations everywhere begin to roll out AMD (read about why AMD matters) and the associated code improvements, we are thrilled with the industry&#8217;s direction toward toolkit interoperability! Why? Because! Our masterful [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>We have been providing JavaScript and Dojo support to freelancers, start-ups and Fortune 500 companies for nearly a decade.  As we intently watch enterprise organizations everywhere begin to roll out AMD <a href="http://www.sitepen.com/blog/2012/07/10/amd-for-the-business-side/">(read about why AMD matters)</a> and the associated code improvements, we are thrilled with the industry&#8217;s direction toward toolkit interoperability!  Why?  Because! Our masterful engineering team, consisting of influential members of various open source communities, positions SitePen perfectly to offer full-on, front-end web development support to the world! </p>
<p>Getting right to the point, (<a href="http://www.prnewswire.com/news-releases/sitepen-expands-service-offering-to-popular-javascript-toolkits-163018596.html">The Official Point!</a>), we are pleased to announce the expansion of SitePen Support to officially include more than fifteen popular open-source JavaScript toolkits! </p>
<p><strong>Now supporting the following JavaScript toolkits:</strong></p>
<table style="width:600px;">
<tr>
<td>
<ul>
<li>Dojo</li>
<li>Persevere packages</li>
<li>dgrid</li>
<li>Curl.js</li>
<li>CometD</li>
<li>Twine</li>
</ul>
</td>
<td>
<ul>
<li>jQuery</li>
<li>Backbone</li>
<li>underscore</li>
<li>RequireJS</li>
<li>PhoneGap/Cordova</li>
<li>MooTools</li>
</ul>
</td>
<td>
<ul>
<li>jQueryUI</li>
<li>Wire</li>
<li>Socket.IO</li>
<li>Express</li>
</ul>
</td>
</tr>
</table>
<p>In addition to toolkits, we will continue to support your custom JavaScript source code, as well as key underlying technologies and formats, including JSON, HTML5, WebSockets, SVG/Canvas, Mobile Web, Server-Side JavaScript, AMD, Node.js and many more.</p>
<p>Our expertise with Dojo and advanced JavaScript is relevant for a wide-range of desktop and mobile web application projects and our approach to SitePen Support has always been flexible with the priority being to improve our customers&#8217; web apps.  We strive to support our customers in every way possible and we continue to be Dojo experts. In addition, we&#8217;re now committed to providing your organization with the front-end development expertise that will optimize your application regardless of which toolkits and technologies your company is comfortable using.  You have our word!</p>
<p>Learn More About <a href="http://www.sitepen.com/support/index.html">SitePen Support</a> or <a href="http://www.sitepen.com/site/contact.html">Contact Us </a>to get started today!</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2012/07/19/now-supporting-all-major-toolkits/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ComposeJS: Robust, Lightweight Object Composition</title>
		<link>http://www.sitepen.com/blog/2011/09/28/composejs-robust-lightweight-object-composition/</link>
		<comments>http://www.sitepen.com/blog/2011/09/28/composejs-robust-lightweight-object-composition/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 21:51:53 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=2083</guid>
		<description><![CDATA[<p>ComposeJS is a JavaScript package/module for object-oriented programming available in the Dojo Foundation package repository. JavaScript itself is already a highly object-oriented programming language, and the prototype-based inheritance system is very powerful. Rather than simply porting a &#8220;class&#8221; system from another language, the core philosophy of ComposeJS is to leverage JavaScript paradigms and enhance it [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://github.com/kriszyp/compose">ComposeJS</a> is a JavaScript package/module for object-oriented programming available in the <a href="http://www.sitepen.com/blog/2011/07/25/dojo-foundation-packages/">Dojo Foundation package repository</a>. JavaScript itself is already a highly object-oriented programming language, and the prototype-based inheritance system is very powerful. Rather than simply porting a &#8220;class&#8221; system from another language, the core philosophy of ComposeJS is to leverage JavaScript paradigms and enhance it with clean, terse syntax and modern composition and resolution concepts for simple, high-performance, and robust object constructors. ComposeJS uses concepts from class inheritance, multiple inheritance, mixins, <a href="http://scg.unibe.ch/archive/papers/Duca06bTOPLASTraits.pdf">traits</a>, and aspect-oriented programming to compose functionality in the most efficient manner possible. </p>
<p><span id="more-2083"></span></p>
<p>ComposeJS is designed to work across multiple JavaScript platforms, client and server, including Node.js and browsers, and can be loaded directly as a script or as a module (with Dojo, RequireJS, etc.). ComposeJS has clean, simple syntax in all of these environments. Generating constructors (or classes) is easy:</p>
<pre lang="javascript">
MyClass = Compose(BaseClass, {
  someMethod: function(){
  }
});
</pre>
<p>This is just a quick example of using ComposeJS. The API is highly flexible and permutative, allowing for multiple mixins, doing constructor delegation, handling conflict resolution, and more. The <a href="https://github.com/kriszyp/compose">ComposeJS documentation provides ample examples and explanation</a>. It is easy to compose structures from multiple entities, for example:</p>
<pre lang="javascript">
MySubClass = MyClass.extend(SomeMixin, SomeOtherMixin, {...});
</pre>
<p>However, I want to focus on the design concepts that drove the development of ComposeJS.</p>
<h3>Simple Design Aligned with Native JavaScript</h3>
<p>ComposeJS is designed to have an extremely simple, consistent API. The ComposeJS function can take any number of arguments, and each are treated the same. Each argument can be an object or a function. Each object&#8217;s properties and methods are available on instances of the ComposeJS generated class, and each function acts as a constructor, called on a instantiation (with its prototype mixed in). These means that the API is very easy to remember. ComposeJS can also work with constructors created from any source. You can use ComposeJS to compose classes from native classes (like Error), ComposeJS classes, classes created from Dojo, MooTools, etc., or constructors created with plain JavaScript. Since ComposeJS is based on core JavaScript principles, ComposeJS is extremely flexible.</p>
<p>ComposeJS also includes helper functions and extensions to fulfill a wide variety of programming needs. <code>Compose.create</code> can be used to instantly create an object, providing quick object delegation. <code>Compose.apply</code> can be applied to an existing object, allowing you to copy and mix in properties from one object to another. ComposeJS also includes decorator construction that allows you to create your own extensions for ComposeJS for more sophisticated object composition techniques.</p>
<h3>Design Influences</h3>
<p>Let&#8217;s look at some of the primary computer science concepts that have been developed over the years to deal with object composition:</p>
<ul>
<li>Inheritance is the fundamental concept of inheriting functionality from other classes. Inheritance systems provide a means for overriding existing functionality. ComposeJS utilizes basic JavaScript (prototype-based) inheritance, making it easy to extend and override existing components.
</li>
<li>Multiple inheritance is an extension of inheritance to compose functionality from multiple classes. The original design has largely been superseded by mixins and traits due to the fragility of only being able to compose with hard references to super classes.
</li>
<li>Mixins are an improvement over multiple inheritance, allowing for more flexible composition of components, without relying on shared inheritance chains. ComposeJS utilizes the concept of mixins to combine functionality from different components.
</li>
<li>Traits address the issues of composition of mixins when different parts of mixins need to be individually selected, providing a much more robust solution in the face of conflicting methods.
</li>
<li>Aspects provide fine-grained composition of functionality for individual methods.</li>
</ul>
<p>ComposeJS uses mixin-style ordered prioritization for default method resolution, but provides the conflict resolution capabilities of traits with method aliasing and exclusion control via the from() decorator. This allows you to enjoy the convenience of the mixins, plus the precise composition control of traits. Basically, you can assign a set of mixins and their order defines the priority of method override. When this is not fine-grained enough, you can explicitly rename methods and choose which method overrides others. When methods are in ambiguous placements in the hierarchy, they will resolve to a conflicted state to indicate that explicit selection is needed.</p>
<p>ComposeJS also strikes a pragmatic balance between the magically implicit <a href="http://www.python.org/download/releases/2.3/mro/">C3 method resolution order</a> (that few mortals can remember), and excessively explicit conflict resolution mandates of traits (where order is normally ignored). ComposeJS follows the simple rule that when explicit order has been supplied without conflicting order elsewhere, method resolution is determined, otherwise a conflict must be resolved.</p>
<p>ComposeJS also uses also provides aspect-oriented decorators to combine functionality of individual methods. This allows for super-call type functionality, but with the much more JavaScript oriented style of aspects, and the ease of using before, after, and around advice. This not only aligns more closely with idiomatic JavaScript, it is about twice as fast as traditional super-call approaches in JavaScript that search through the prototype chain for overridden methods. Aspect oriented decoration is also EcmaScript &#8220;strict mode&#8221; compatible (in fact ComposeJS opts for strict mode when available), whereas class constructors like dojo.declare are currently not compatible with strict mode.</p>
<h3>Functional, Secure</h3>
<p>ComposeJS is designed to follow principles of least authority. Creating new constructors or instances never modifies an existing object or the global scope (unless explicitly applied to an existing object using the &#8220;apply&#8221; API). ComposeJS creates new constructors and instances based on existing objects, preserving fundamental concepts of functional programming and avoiding sloppy side-effect based imperative programming styles. This also makes ComposeJS suitable for use in object capability security systems.</p>
<h3>Installation</h3>
<p>ComposeJS currently is a single file package. You can download the file from the <a href="https://github.com/kriszyp/compose">github repository</a>. ComposeJS is also available in the <a href="http://packages.dojofoundation.org/">Dojo Foundation package repository</a>, so you can also install it with <a href="https://github.com/kriszyp/cpm">CPM</a>:</p>
<pre>
cpm install compose
</pre>
<p>Or you can install it for NodeJS:</p>
<pre>
npm install compose
</pre>
<h3>Dojo and ComposeJS</h3>
<p>ComposeJS is not Dojo specific, but can be used with Dojo classes. Because ComposeJS follows standard JavaScript prototype construction, ComposeJS can extend and mixin classes created with dojo.declare. Remember that <code>dojo.declare</code>&#8216;s inherited() method won&#8217;t work from ComposeJS class methods, ComposeJS&#8217; aspects should be used instead. ComposeJS is also being considered as a replacement for <code>dojo.declare</code> in Dojo 2.0.</p>
<h3>Compose!</h3>
<p>ComposeJS provides clean, simple, compact syntax for creating class-like constructors in JavaScript and is easy to start using. It builds on the best principles of composition and inheritance demonstrated in other languages, while maintaining a completely JavaScript oriented approach. ComposeJS has <a href="https://github.com/kriszyp/compose">solid documentation, look through the explanations and examples and give it try</a>!</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2011/09/28/composejs-robust-lightweight-object-composition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Dojo Foundation Packages]]></series:name>
	</item>
		<item>
		<title>Git-Linked Packages for NPM/Node</title>
		<link>http://www.sitepen.com/blog/2011/07/29/git-linked-packages-for-npmnode/</link>
		<comments>http://www.sitepen.com/blog/2011/07/29/git-linked-packages-for-npmnode/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 21:30:50 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=2907</guid>
		<description><![CDATA[<p>The new Dojo Foundation Package repository is an easy and powerful new way to host Node packages for installation with NPM. This new repository allows you to directly link packages to git repositories and it works with NPM without changes. Developing a Node package couldn&#8217;t be easier. Simply submit your package URL to the repository, [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>The new <a href="http://packages.dojofoundation.org/">Dojo Foundation Package repository</a> is an easy and powerful new way to host Node packages for installation with NPM. This new repository allows you to directly link packages to git repositories and it works with NPM without changes. Developing a Node package couldn&#8217;t be easier. Simply <a href="http://packages.dojofoundation.org/submit.html">submit your package URL to the repository</a>, and instantly it will be available for installation for NPM! Not only that, but you never have to resubmit version updates. Since the package repository is linked to git, any new version tags that you create on your github package repository will automatically be reflected as a new package version available for NPM installation. NPM does not need to be reconfigured at all, just run install like you would with any other package:</p>
<pre>
npm install my-new-package
</pre>
<p>When you have updates for your package that you want to designate as a new version, simply tag it in Git. And that&#8217;s it! Next time your package is installed or upgraded the newest version will be there.</p>
<p><span id="more-2907"></span></p>
<p>As mentioned in the <a href="http://www.sitepen.com/blog/2011/07/25/dojo-foundation-packages/">introductory blog post about the repository</a>, this entire toolset is also open source and interoperable. The package repository is based on the CommonJS package specification and registry specification that was developed and is used by Isaac Schlueter for NPM (Node package manager). NPM and the package registry specification was designed for multiple registries, and the package repository is fits into the distributed package registry model used by NPM. To see NPM in action working with the Dojo Foundation Packages, you can install the compose package, which is housed at the Dojo Foundation:</p>
<pre lang="html4">
npm install compose
</pre>
<p>This repository is designed to for JavaScript packages for any platform. Dojo is using this repository for AMD-based browser packages with a browser-targeted package manager (<a href="https://github.com/kriszyp/cpm">CPM</a>), but the repository can hold packages built for other systems, including Node. This is particularly useful for packages that can be used in the browser and on Node. Give it try with your next Node package for the easiest package maintenance imaginable!</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2011/07/29/git-linked-packages-for-npmnode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Dojo Foundation Packages]]></series:name>
	</item>
		<item>
		<title>Asynchronous Modules Come to Dojo 1.6</title>
		<link>http://www.sitepen.com/blog/2011/02/14/asynchronous-modules-come-to-dojo-1-6/</link>
		<comments>http://www.sitepen.com/blog/2011/02/14/asynchronous-modules-come-to-dojo-1-6/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 07:01:32 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1971</guid>
		<description><![CDATA[<p>Dojo (core) and Dijit 1.6 have been refactored to follow the proposed CommonJS AMD API. Module Compatibility Dojo modules are now completely compatible with: RequireJS Nodules, a Persevere sub-project for Node.js package and module handling Backdraft Framework, the leading candidate for the future Dojo module loader other AMD-compatible module loaders Flexibility, Performance, and Stack Traces [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>Dojo (core) and Dijit 1.6 have been refactored to follow the proposed <a href="http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition">CommonJS AMD API</a>.</p>
<h2>Module Compatibility</h2>
<p>Dojo modules are now completely compatible with:</p>
<ul>
<li><a href="http://requirejs.org/">RequireJS</a></li>
<li><a href="http://www.sitepen.com/blog/2010/09/15/nodules-better-modulepackage-handling-for-node-js/">Nodules</a>, a Persevere sub-project for <a href="http://www.sitepen.com/blog/2010/09/15/nodules-better-modulepackage-handling-for-node-js/">Node.js package and module handling</a></li>
<li><a href="http://bdframework.org/docs/loader/loader.html">Backdraft Framework</a>, the leading candidate for the future Dojo module loader</li>
<li>other AMD-compatible module loaders</li>
</ul>
<h2>Flexibility, Performance, and Stack Traces</h2>
<p>This refactoring gives Dojo excellent flexibility going forward, to support both legacy synchronous loading mechanisms, as well as new asynchronous script-tag based loading that provides significant performance boosts and debugging improvement (including real stack traces!).</p>
<p><span id="more-1971"></span></p>
<h2>Anonymous Modules</h2>
<p>The AMD module format also has a significant advantage in that it allows for anonymous modules, which removes the tight coupling between code and module identity. With anonymous modules, identity is <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>, avoiding the duplication of information in filename and in code. This makes code much more portable, it can be moved to different directories or packages without altering the code itself. AMD also uses slash-delimited module identifiers rather than dot-delimited, which enables more direct mapping to URLs and allows for relative references to other modules, giving us portability of directory structures as well (a directory with a set of modules can be moved or renamed, and the relative ids would still be valid).</p>
<h2>Backwards-Compatible</h2>
<p>First, don&#8217;t worry, your current code based on <code>dojo.provide/dojo.require</code> will still work. By default, Dojo uses a backwards-compatible synchronous loader. You can continue to write:</p>
<pre lang="javascript">
// synchronously load dijit.Tree
dojo.require("dijit.Tree"); 
// dijit.Tree is now available
new dijit.Tree(...);
</pre>
<h2>Using the New Module Format</h2>
<p>While you can continue to use the legacy module format and the legacy module API is the supported format, it is possible to start using the new module format with proper precautions, allowing you to take advantage of asynchronous module loaders including <a href="http://requirejs.org/">RequireJS</a> or <a href="http://bdframework.org/docs/loader/loader.html">Backdraft</a> now for faster and better module loading.</p>
<p>However, first a word of warning. The refactor to AMD is a transitional step for Dojo, and user-authored AMD modules are not supported within Dojo&#8217;s loader/build system yet. The current build system in 1.6 has very limited support for the AMD format. If you write your modules in AMD format and you still use the default synchronous Dojo loader, make sure you follow the same style (including line breaks) as the Dojo modules to ensure proper builds. The build system was minimally altered to build Dojo&#8217;s modules.</p>
<p>There is no official support for building with AMD modules, but of course it works if you follow the module declaration style of Dojo&#8217;s modules. The build systems provided by RequireJS or Backdraft are specifically designed for AMD, and much more robust and capable for the new format.</p>
<p>Again, Dojo 1.6 doesn&#8217;t officially support user-based AMD modules or asynchronous loading, but various users have successfully tested this functionality, and this is the future of Dojo. With Dojo, we do not break forwards-compatibility in dot releases, e.g. 1.5 to 1.6. We&#8217;re currently adding functionality that will be the foundation for Dojo 2.0, without breaking our promise of forwards-compatibility.</p>
<p>Creating a module with this format is simple:</p>
<ul>
<li>define your dependencies in an array as the first argument</li>
<li>and provide a callback function to execute your module once the dependencies are ready</li>
</ul>
<pre lang="javascript">
define(["dijit/Tree"], function(Tree){
  // The dijit Tree is now available, and we can use the local variable Tree
  new Tree(...);
});
</pre>
<p>Now your module is ready for a Dojo async loader or RequireJS. You can still use this module with standard dojo.require() calls as well. </p>
<p>To summarize your options:</p>
<ul>
<li>Use the legacy format (dojo.require()/dojo.provide()) &#8211; These modules will only work with the Dojo sync loader, but they will build properly in the Dojo build system.</li>
<li>Use the AMD format (define()) with Dojo sync loader &#8211; These modules will load properly and can be used with other module loaders. This is not officially supported yet and they will only build properly in the current Dojo 1.6 build system if you carefully follow the Dojo modules code style.</li>
<li>Use the AMD format (define()) with RequireJS or Backdraft &#8211; Modules should load properly and can be used with other module loaders. They will build properly using their provided build system (however Dojo hasn&#8217;t been officially tested on other module loaders)</li>
</ul>
<h2>Referencing Modules</h2>
<p>When using Dojo with the AMD format, there are also a few different ways to reference modules. First, the AMD recommended way of referencing modules is to declare them in the dependency list and then provide matching arguments, like we did above. However, this is not supported by the Dojo 1.6 build system. This module format is only for fully AMD-compliant loaders. An example:</p>
<pre lang="javascript">
define(["dojo/cookie", "dijit/Tree"], function(cookie, Tree){
  var cookieValue = cookie("cookieName"); // get a cookie
  new Tree(...); // create a dijit.Tree
});
</pre>
<p>This provides a big advantage over the nested-object-as-a-namespace approach to reference constructors and functions from modules since we no longer need to type out the namespace on every reference. We only need to give the full path &#8220;dojo/cookie&#8221; in the dependencies, and once it is aliased to an argument, we can directly reference by that argument/variable (<code>cookie</code> in this example). No longer does using Dojo mean typing &#8220;dojo.&#8221; until your ring finger develops carpal tunnel.</p>
<p>However, for those who need to use the Dojo build system, want to maintain current coding style, or want to migrate existing modules to the new format, we can also use &#8220;dojo&#8221; and &#8220;dijit&#8221; as dependencies themselves, for easy nested-object style referencing. Remember though, that we still need to list our dependencies, even if we don&#8217;t use the reference to them. The example above could be written:</p>
<pre lang="javascript">
define(["dojo", "dijit", "dojo/cookie", "dijit/Tree"], function(dojo, dijit){
  var cookieValue = dojo.cookie("cookieName"); // get a cookie
  new dijit.Tree(...); // create a dijit.Tree
});
</pre>
<p>Of course this is more verbose, but it makes migration much easier. </p>
<p>Another thing to be aware of with Dojo&#8217;s move to AMD is that this new format won&#8217;t work with previous versions of Dojo. Consequently, you can&#8217;t just copy Dojo 1.6 modules into an earlier Dojo release and expect it to work. In order to run 1.6 modules in an earlier version you would need to port them to the legacy dojo.require/dojo.provide API (and, of course, deal with any other dependencies from 1.6 that the module had).</p>
<h2>Running Dojo on RequireJS</h2>
<p>Dojo runs with RequireJS, using RequireJS as the module loader. To do this, first load RequireJS, then configure Dojo as a package:</p>
<pre lang="html4">
<script src="require.js"></script>
<script>
require({"packages":[
     {"name":"dojo","location":"packages/dojo","lib":".","main":"./_base/_loader/package-main"}]});
</script>
</pre>
<p>And we can now load our application entry module, which can refer to &#8220;dojo&#8221; as a dependency:</p>
<pre lang="html4">
<script type="text/javascript">
require(["my-module"]);
</script>
</pre>
<p>Where my-module may look like:</p>
<pre lang="javascript">
define(["dojo"], function(dojo){
  dojo.query("some-query")...
  ...
});
</pre>
<p>There are a few things to be aware of when using RequireJS with Dojo:</p>
<ul>
<li>RequireJS is managing module loading, so Dojo cannot automatically determine when to parse your HTML for declarative components/widgets. You must manually call dojo.parser.parse() after all necessary modules are loaded and the page is ready.</li>
<li>RequireJS is handling module loading, so you should also use RequireJS to do your builds.</li>
</ul>
<h2>The Future is Here. The Future is Near.</h2>
<p>We will provide more information in the future on using the Backdraft loader, as we look to integrate it with Dojo.</p>
<p>We are also working on a package installer that will automatically generate your configuration setup.</p>
<p>We are looking forward to great improvements in module loading and interoperability with this new format. This will be a key part of our move towards autonomous packages as well. Look for more updates in the near future, and let us know if you would like to <a href="http://dojofoundation.org/about/">get involved</a>.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2011/02/14/asynchronous-modules-come-to-dojo-1-6/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Many Comet Solutions for your Real-time Apps</title>
		<link>http://www.sitepen.com/blog/2011/01/03/many-comet-solutions-for-your-real-time-apps/</link>
		<comments>http://www.sitepen.com/blog/2011/01/03/many-comet-solutions-for-your-real-time-apps/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 07:01:42 +0000</pubDate>
		<dc:creator>Dylan Schiemann</dc:creator>
				<category><![CDATA[comet]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1687</guid>
		<description><![CDATA[<p>We&#8217;ve been interested in Comet for a long time, dating back to the days of mod_pubsub and early talks on event-driven user interfaces. Now, with the arrival of WebSockets in WebKit-based browsers, and expected in Firefox 4.x and Internet Explorer 9 once the next WebSockets specification draft is completed, and with Comet techniques used by [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>We&#8217;ve been interested in Comet for a long time, dating back to the days of mod_pubsub and early talks on <a href="http://dylanschiemann.com/wp-content/uploads/eventui.pdf">event-driven user interfaces</a>. Now, with the <a href="http://cometdaily.com/2010/11/04/state-of-websocket-support/">arrival of WebSockets</a> in WebKit-based browsers, and expected in Firefox 4.x and Internet Explorer 9 <a href="http://cometdaily.com/2010/12/07/websockets-disconnected-for-firefox-4/">once the next WebSockets specification draft is completed</a>, and with Comet techniques used by many of the world&#8217;s most popular sites, Comet has arrived as a viable necessity for rolling-out real-time capabilities for your web applications.</p>
<p><span id="more-1687"></span></p>
<h2>Comet Server Choices</h2>
<p>At the recent <a href="http://londonajax.com/">London Ajax JSMiniConf</a> I presented a summary of the current state of Comet and Server-Side JavaScript tools.</p>
<p><iframe src="http://player.vimeo.com/video/15216824?color=399CB5" width="400" height="320" frameborder="0"></iframe>
<p><a href="http://vimeo.com/15216824">London Ajax JSMiniConf: &#8220;A quick summary of Comet&#8221; by Dylan Schiemann</a> from <a href="http://vimeo.com/londonajax">London Ajax User Group</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>With so many great options to choose from, the choices can be overwhelming. Our current approach to Comet is as follows.</p>
<ul>
<li><a href="http://cometd.com/">cometD</a> &#8211; We have invested resources in the Bayeux protocol, the DojoX cometD client, and the Python version of the cometD server. cometD is a popular choice because of its solid performance and the widespread support of the Bayeux protocol across many servers including <a href="http://jetty.codehaus.org/jetty/">Jetty</a> (Java), DWR (Java), Erlycomet (Erlang), WebSphere (Java), WebLogic (Java), <a href="http://www.frozenmountain.com/websync/">WebSync</a> (IIS, .Net), and more.</li>
<li><a href="http://directwebremoting.org/">DWR</a> &#8211; We have invested resources in DWR, helping bring Bayeux support to this popular framework for simple remoting between Java and JavaScript applications. DWR helps Java developers ease the transition to using JavaScript, in addition to providing real-time capabilities. In comparison to GWT, because DWR is a Java to JavaScript interface, you can code in JavaScript, the native language of the browser, and communicate with a Java back-end without the magic of translating/rewriting Java into JavaScript.</li>
<li><a href="http://hookbox.org/">Hookbox</a> &#8211; A popular upstart Comet server written in Python that offers an extremely simple approach for getting started, including the Hosted Hookbox service. It includes support for Comet Session Protocol (CSP), and it extends the <a href="http://js.io/">js.io</a> microtoolkit for managing a wide variety of messaging protocols.</li>
<li><a href="http://www.caplin.com/caplin_liberator.php">Liberator</a> and <a href="http://lightstreamer.com/">Lightstreamer</a> &#8211; The two most popular commercial Comet servers on the market, Caplin Liberator and Lightstreamer have both improved dramatically over the past decade due to a healthy level of competition. Companies looking for a tried and true approach to the real-time web typically look at these options as well as Jetty and others. All of these options are great to work with, and each is optimized to handle performance in varying ways. While both of these platforms are proprietary and commercial, both offer limited-use free versions for commercial use as well.  Watch their creators in action at the recent <a href="http://skillsmatter.com/podcast/ajax-ria/comet-panel">Comet panel</a>, where I ask them which Comet servers they would suggest if their own product did not exist!<br />
<iframe src="http://player.vimeo.com/video/13344404?color=399CB5" width="400" height="320" frameborder="0"></iframe>
</li>
<li><a href="http://www.persvr.org/">Persevere</a> and <a href="http://www.sitepen.com/blog/2010/07/19/real-time-comet-applications-on-node-with-tunguska/">Tunguska</a> &#8211; The majority of our non-Dojo research and development efforts have been focused on Persevere, including the new Tunguska sub-project. Tunguska is a set of tools for building real-time applications rather than a closed black box that can’t easily be integrated with, including connection and message queues, long-polling, message routing, and clustering. It runs on top of the popular Node.js or Narwhal+Jack, the two most popular server-side JavaScript engines on the market today. We are very focused on this work today because we&#8217;re strong believers in the value of <a href="http://www.sitepen.com/blog/series/ssjs/">server-side JavaScript</a></li>
<li>XMPP &#8211; We receive a number of inquiries into XMPP server integration for chat applications. We developed the original dojox.xmpp chat client, and have assisted a number of organizations in making this work with their web applications.</li>
<li>Others &#8211; We&#8217;re also fans of Socket.IO, Orbited, Meteor, APE, Google App Engine, Tornado and a number of other projects. We&#8217;re happy to help our clients use these servers should they be appropriate for their web application development needs.</li>
</ul>
<p>With Dojo 1.6, we rely on our new <a href="http://www.sitepen.com/blog/2010/10/31/dojo-websocket/">dojox.socket implementation</a> making it easy to work with a wide variety of Comet servers, including the ones listed above. </p>
<p>Bottom line: there is no shortage of excellent Comet servers, so pick one that meets the needs of your application and your development team.</p>
<h2>SitePen Comet Services</h2>
<p>SitePen offers assistance with your real-time Comet development needs, from consulting on architecture and server choices, to development of real-time applications, to support for your ongoing JavaScript, Ajax, and HTML5 application development needs when working with a Comet server. <a href="http://www.sitepen.com/contact/">Contact SitePen</a> for a free 30-minute consultation to learn more about how we can help your organization create extraordinary real-time web and mobile applications.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2011/01/03/many-comet-solutions-for-your-real-time-apps/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Resource Query Language: A Query Language for the Web, NoSQL</title>
		<link>http://www.sitepen.com/blog/2010/11/02/resource-query-language-a-query-language-for-the-web-nosql/</link>
		<comments>http://www.sitepen.com/blog/2010/11/02/resource-query-language-a-query-language-for-the-web-nosql/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 07:01:58 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1321</guid>
		<description><![CDATA[<p>Data querying is a critical component of most applications. With the advance of rich client-driven Ajax applications and document oriented databases, new querying techniques are needed, and Resource Query Language (RQL) defines a very simple but extensible query language specifically designed to work within URIs and query for collections of resources. The NoSQL movement is [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>Data querying is a critical component of most applications. With the advance of rich client-driven Ajax applications and document oriented databases, new querying techniques are needed, and <a href="http://github.com/kriszyp/rql">Resource Query Language (RQL)</a> defines a very simple but extensible query language specifically designed to work within URIs and query for collections of resources. The NoSQL movement is opening the way for a <a href="http://www.sitepen.com/blog/2010/05/11/nosql-architecture/">more modular approach to databases, and separating out modeling, validation, and querying concerns from storage concerns</a>, but we need new querying approaches to match more modern architectural design.</p>
<p><span id="more-1321"></span></p>
<h2>RQL</h2>
<p>RQL simply consists of named operators that take a set of arguments and syntactically follows the standard parenthesis&#8217;s-based call syntax used by most modern languages (JavaScript, Java, C, PHP, Python, etc) as prefix notation operators. Everything in RQL can be expressed as a set of these operators by nesting and sequencing them. Nothing needs to be altered to add new operators. The only additional syntax in RQL are comparisons that are simply sugar or shorthand for basic RQL operators, and thus do not introduce any parsing or execution complexities. For example, to express a query for all the resources where the rating equals 5 and the price is less than 10:</p>
<pre lang="javascript">
and(eq(rating,5),lt(price,10))
</pre>
<p>RQL is specifically designed for the web, using URI appropriate delimiters, and using URI character encoding for all text/string values. It provides a shorthand or sugar for certain operators that makes RQL a completely compatible superset of the default form encoding, application/x-www-urlencoded, as well as <a href="http://tools.ietf.org/html/draft-nottingham-atompub-fiql-00">Feed Item Query Language</a> (FIQL). The example above could alternately be written in RQL as:</p>
<pre lang="javascript">
rating=5&#038;price=lt=10
</pre>
<p>This syntax makes it easy to use RQL with existing forms and query processors. The documentation in the <a href="http://github.com/kriszyp/rql">RQL git repository</a> contains many more details about the defined operators and syntax. Let&#8217;s look at more of the motivation for RQL.</p>
<h3>What&#8217;s wrong with SQL?</h3>
<p>SQL has long been <strong>the</strong> query language of choice for applications, but a couple factors have motivated alternate query languages. First, the NoSQL movement continues to gain ground, eroding the notion of using SQL as the basis for all database operations. Second, the explosion in Ajax applications has moved significant application logic to the browser, increasing the need for capable queries to be delivered from the browser to the server within URLs. SQL predates URLs by some 20 years, and does not fit well with URLs. SQL is not designed for document, graph, or object-oriented databases and is not only awkward within URLs, it is a terrible security issue to provide any pass-through of query strings directly to SQL (known as SQL injection). SQL is particularly hazardous since retrieval and modifying operations can all be combined in the same query. SQL&#8217;s syntax is also completely unlike that of modern programming languages. Trying to shore up SQL for URIs and modern databases with an SQL-derivative query language is an exercise in futility.</p>
<p>Again, RQL is designed to be URI friendly, leveraging URI encoding and designated delimiters for syntax that works perfectly in web requests. RQL also borrows from the familiar parenthesis-based call syntax of JavaScript, Python, C, C++, Java, PHP, etc. to give a highly composable and extensible query language.</p>
<h3>What&#8217;s wrong with Map (and reduce) functions and key-value stores?</h3>
<p>Using map and reduce functions for generating indexes is awesome! CouchDB’s use of mapping and reducing functions as the basis for highly scalable, incredibly flexible (Turing complete) indexed queries is brilliant, and is one of the main reasons for CouchDB’s enormous popularity. Map/Reduce functions are also an integral part of many other NoSQL databases like Riak, Hadoop, MongoDB, and more. Map/Reduce functions are somewhat low-level, however, and there are a lot of aspects to getting appropriate data out of databases besides just creating flexible indexes. Map/Reduce creates a great foundation to build on, but let&#8217;s look at some of the other useful tools for database querying. For many of these, SQL actually has constructs that we can learn from. It is quite beneficial to utilize a query language layer on top of a Map/Reduce layer.</p>
<p>SQL is a domain specific language (DSL). DSLs are powerful for improving our productivity by providing a syntax that is especially well suited for the task at hand. We have seen the power of DSLs in other areas of programming. For example, CSS selector querying has revolutionized how we retrieve DOM nodes in the browser. This functionality is the core of most modern JavaSscript libraries. Of course it is certainly still possible to retrieve nodes by purely programmatic APIs, but CSS selector querying makes life much easier. A query language plays the same role. While it is possible to query a database through programmatic means, and as I discussed in NoSQL architecture, is very important for maximizing control, modularity, and efficiency, this does not negate the remarkable benefit that can be afforded also being able to utilize a query language on top of a database.</p>
<p>RQL continues the tradition of providing a language specifically designed for the needs of querying data, but doing so with a much simpler, easy to use syntax. RQL is also highly extensible, making it extremely easy to utilize custom map-function-based indexes/views to compose and combine with other querying mechanisms. For example, we could have RQL translate queries to retrieve data from simple single-property indexes and from complex indexes:</p>
<pre lang="javascript">
price<1000&#038;customProductEvaluationFunction(4)
</pre>
<p>SQL does a great job of handling massive permutations of queries and finding the most efficient usage of a fixed set of indexes to find results. With map-reduce alone, you create an index or view for every different type of query. With queries that can take many different forms, it can often be unfeasible to generate a mapping function for every conceivable permutation. This approach simply doesn't scale, developers often can't create large number of views and it wouldn't be efficient to keep a large number of views/indexes up to date. With SQL, the query execution engine can take queries in many forms, including multiple parameters, various constraining columns, and more, and find the most efficient execution path utilizing existing indexes. Query engine implementations have the freedom to make appropriate optimizations because the query language is decoupled from the indexes.</p>
<p>Unfettered query permutations is not without hazards. One of the advantages to key-value stores and mapping functions is the guarantee of O(log n) queries. SQL tends to make it far too easy to generate extremely expensive queries which may not appear to be problematic until a database grows large enough to cause problems. Because of this, RQL is designed in complement with a RQL templating form. This is essentially an application of URI templating with RQL, and allows one to define the set of acceptable RQL queries (without having to write out each individual form). See the RQL templates section below for more on this.</p>
<p>A semantically well-defined query language also serves to make querying more transparent. Interaction transparency is a key concept of REST, and allows intermediaries and components to participate in a meaningful way that can't be achieved with opaque queries. Frameworks can provide client-side querying of cached or replicated data, proxies can understand queries, and queries can be generated by reusable code that can be used across many applications.</p>
<h3>What's wrong with JSONQuery?</h3>
<p>With the increased popularity of JSON-based data representations, we have sought to provide a convenient syntax for querying JSON data by extending and improving JSONPath. This syntax is called JSONQuery. The JSONPath syntax that JSONQuery inherits is still not well-aligned with URL structures and is very difficult to extend due to the fact that each operator is based on a different syntax. Creating new operators thus requires modifications to the parsing engine.</p>
<h2>RQL Templates</h2>
<p>RQL templates provide a means for defining a query or a constrained set of queries and the variables that may be substituted into the query. When queries can be made from the web, we must deal with the challenge of untrusted users, and unmitigating querying capabilities typically makes a server highly vulnerable to overload and resource exhaustion. With RQL templates, we can specify a RQL template (or set) that we know can be efficiently processed by the server (typically O(log n) time). This is also one of the benefits of map-reduce functions, but RQL templates provide more flexibility, still allowing various permutations with adjustable constraint. </p>
<p>One of the key concepts about the Map/Reduce approach is the emphasis on utilizing information about expected query forms up front to generate customized indexes. Then most of the work only needs to be performed once per data change rather than doing large amounts of work for each query (and most applications read and query much more than write/change data). RQL templates not only serve to constrain queries, but RQL in template form can also serve to conveniently inform the creation of views/indexes and their map functions. One can auto-generate map functions and indexes based on RQL templates, fully leveraging the DSL approach. Here is an example of a template, that would indicate that based on the available queries, the price and rating properties should be indexed:</p>
<pre lang="javascript">
{&#038;price,rating}
</pre>
<p>Because RQL is URI-based, templates can naturally be written using the standard URI templating syntax. For example, a simple RQL template might look like:</p>
<pre lang="javascript">
{&#038;price,rating}&#038;limit({count})
</pre>
<p>This indicates acceptable properties to search on (price and rating) and that the query must include a limit on the number of items returned (with the "count" variable).</p>
<p>While standard URI templating provides a good foundation for templating, RQL templates have some additional forms for greater expressibility. We can also use square brackets to indicate a fragment of a query that may or may not exist, or may occur a variable number of times. For example, we could indicate support for sorting, by allowing an optional sort operation:</p>
<pre lang="javascript">
{&#038;price,rating}&#038;limit({count})[&#038;sort([+,-][price,rating])]?
</pre>
<p>With RQL templates, we can give a "menu" of possible queries that the server supports. This follows the fundamental hyperlink principle of REST, providing self-descriptive navigation to the user agent. Clients and users can easily discover what queries can be made against a server and the appropriate format.</p>
<p>RQL templates can also be used like parameterized prepared statements in SQL, where values can be provided outside the query. This can simplify query generation by automating the value encoding process which can be a source of vulnerability if done improperly (hence SQL injection is such a frequent vulnerability of web applications).</p>
<p>Finally, a set of RQL templates can also be used to generate appropriate indexes. Indexed properties can be selected or map (and reduce) functions can be determined from templates.</p>
<p>RQL templates are still a relatively new mechanism with RQL, and we will explore implementation possibilities in a later post.</p>
<h2>Implementation</h2>
<p>A JavaScript implementation of RQL is available on the <a href="http://github.com/kriszyp/rql">RQL github project</a>. This version runs a CommonJS module with async support and runs on NodeJS, Dojo 1.6, RequireJS, Narwhal and any other CommonJS platform (it is pure JavaScript), and is integrated into Persevere 2.0.</p>
<p>The most basic way to use the JavaScript implementation is to query a JavaScript array. To do this we prepare our query, and execute:</p>
<pre lang="javascript">
var query = require("rql/js-array").query;
// some sample data:
var products = [
  {price:14.99, rating: 5},
  {price:5.99, rating: 3}];

var underTen = query("price<10");
var productsUnderTen = underTen(products);
productsUnderTen.length -> 1
</pre>
<h2>Using RQL with Persevere</h2>
<p>RQL is the core query language for <a href="http://persvr.org/">Persevere 2.0</a>. Persevere runs on NodeJS and provides a <a href="http://github.com/kriszyp/pintura">JSON-oriented HTTP/REST interface</a> to various data stores. This interface includes support for RQL. With Persevere, you can create a new data store (using the included data explorer) or by creating one <a href="http://www.sitepen.com/blog/2010/01/25/getting-started-with-pintura/">programmatically</a>. The Persevere <a href="http://github.com/kriszyp/persevere-example-wiki">example wiki</a> includes a <code>Page</code> model as an example store. We can easily query the store with RQL-based URLs. For example, to find the first 10 pages that have a status of "published":</p>
<pre>
GET /Page/?status=published&#038;limit(10)
</pre>
<p>In this example, we sort the pages by the author, and list the author and status, limiting to 10 items, starting at an offset of 10:</p>
<pre>
GET /Page/?sort(createdBy)&#038;select(createdBy,status)&#038;limit(10,10)
</pre>
<p>The composibility of RQL gives Persevere powerful web-based querying to data stores. Persevere uses the RQL parser module to parse the queries and deliver them to the underlying data store (converts to MongoDB queries, SQL queries, etc.).</p>
<p>The RQL implementation in Persevere also lets us generate queries using JavaScript chaining, allowing us to create queries in JavaScript with a similar look to URL-based queries. The previous two examples can be done in JavaScript. First we can filter and limit:</p>
<pre lang="javascript">
var Page = require("model/page").Page;
Page.query().eq("status","published").limit(10).forEach(function(page){
  // this is called for each item returned from the query
});
</pre>
<p>And we can sort, select, and limit:</p>
<pre lang="javascript">
var Page = require("model/page").Page;
Page.query().sort("createdBy").select("createdBy","status").limit(10,10).forEach(function(page){
  // process each item
});
</pre>
<p>The query is sent to the underlying data source (can be in memory, MongoDB, Redis, etc) once an array method is called. Array methods include forEach, map, filter, and other iterative array methods.</p>
<h2>Using RQL with Dojo</h2>
<p>We can also use RQL from Dojo. Download the RQL source files into your JavaScript directory and then you can make queries with RQL. A particularly powerful use case for RQL is as a query engine for Dojo object stores (the new store API introduced in 1.6). Replacing the query engine of an object store is as simple as setting the queryEngine to the RQL query executor:</p>
<pre lang="javascript">
define("my-module", ["dojo/store/Memory", "rql/js-array"],
function(Memory, jsArray){
  var memoryStore = new Memory({data:myData});
  memoryStore.queryEngine = jsArray.query;
  memoryStore.query("price<10&#038;sort(rating)").forEach(function(product){
    // handle each product
  });
});
</pre>
<p>We can also utilize the JavaScript chaining API with the <code>rql/query</code> module in Dojo as well.</p>
<h3>Adding Operators</h3>
<p>RQL is designed to be extensible. The JavaScript makes it easy to add new operators. We can simply add a new operator to the <code>operators</code> object, exported from <code>rql/js-array</code>. Let's imagine we want to add a operator that finds all products that are on sale for at least a given percentage:</p>
<pre lang="javascript">
define("my-module", ["rql/js-array"],
function(jsArray){
  jsArray.operators.saleAt = function(percent){
    var result = [];
    for(var i = 0, length = this.length; i < length; i++){
      var item = this[i];
      if((item.regularPrice - item.salePrice) / item.salePrice * 100 > percent){
        result.push(item);
      } 
    }
    return result;
  };
  var productsOnSaleForMoreThan20PercentOff = jsArray.query("saleAt(20)")(products);
});
</pre>
<p>Future implementation work will include a tool for generating map and reduce functions based on RQL queries, and templating tools.</p>
<h2>RQL: A Modern Query Language</h2>
<p>RQL is designed for modern application development. It is built for the web, ready for NoSQL, and highly extensible with simple syntax. This is a query language for next generation database interaction.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/11/02/resource-query-language-a-query-language-for-the-web-nosql/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Dojo WebSocket</title>
		<link>http://www.sitepen.com/blog/2010/10/31/dojo-websocket/</link>
		<comments>http://www.sitepen.com/blog/2010/10/31/dojo-websocket/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 06:54:44 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[Cometd]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Persevere]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1960</guid>
		<description><![CDATA[<p>NOTE: This post is out of date.Read our updated version of this post for more up to date information! Dojo 1.6 introduces a new API for Comet-style real-time communication based on the WebSocket API. WebSocket provides a bi-directional connection to servers that is ideal for pushing messages from a server to a client in real-time. [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<div style="background:#FFFFCC; padding: 1em; font-weight:bold; border:1px solid #FFCC99; margin-bottom:1em">NOTE: This post is out of date.<br /><a href="http://www.sitepen.com/blog/2012/11/05/dojo-websocket-with-amd/">Read our updated version of this post for more up to date information</a>!</div>
<p>Dojo 1.6 introduces a new API for Comet-style real-time communication based on the WebSocket API. WebSocket provides a bi-directional connection to servers that is ideal for pushing messages from a server to a client in real-time. Dojo&#8217;s new <code>dojox.socket</code> module provides access to this API with automated fallback to HTTP-based long-polling for browsers (or servers) that do not support the new WebSocket API. This allows you start using this API with Dojo now.</p>
<p><span id="more-1960"></span></p>
<p>The <code>dojox.socket</code> module is designed to be simple, lightweight, and protocol agnostic. In the past Dojo has provided protocol specific modules like CometD and RestChannels, but there are numerous other Comet protocols out there, and <code>dojox.socket</code> provides the flexibility to work with virtually any of them, with a simple foundational interface. The <code>dojox.socket</code> module simply passes strings over the HTTP or WebSocket connection, making it compatible with any system.</p>
<p>The simplest way to start a <code>dojox.socket</code> is to simply call it with a URL path:</p>
<pre lang="javascript">
var socket = dojox.socket("/comet");
</pre>
<p>We can now listen for message events from the server:</p>
<pre lang="javascript">
socket.on("message", function(event){
  var data = event.data;
  // do something with the data from the server
});
</pre>
<p>Here we use the <code>socket.on()</code> event registration method (inspired by <a href="http://github.com/LearnBoost/Socket.IO">socket.io</a> and NodeJS&#8217;s registration method) to listen to &#8220;message events&#8221; and retrieve data when they occur. This method is also aliased to the Dojo style <code>socket.connect()</code>.</p>
<p>We can also use <code>send()</code> to send data to the server. If you have just started the connection, you should wait for the <code>open</code> to ensure the connection is ready to send data:</p>
<pre lang="javascript">
socket.on("open", function(event){
  socket.send("hi server");
});
</pre>
<p>Finally, we can listen for the connection being closed by the server or network by listening for the &#8220;close&#8221; event. And we can initiate the close of a connection from the client by calling <code>socket.close()</code>.</p>
<p>The <code>dojox.socket</code> method can also be called with standard Dojo IO arguments to initiate the communication with the server. This makes it easy to provide any necessary headers for the requests. For example:</p>
<pre lang="javascript">
var socket = dojox.socket({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json"
	}});
</pre>
<p>This will automatically translate the relative URL path to a WebSocket URL (using <code>ws://</code> scheme) or an HTTP URL depending on the browser capability.</p>
<p>For some applications, the server may only support HTTP/long-polling (without real WebSocket support). We can also explicitly create a long-poll based connection:</p>
<pre lang="javascript">
var socket = dojox.socket.LongPoll({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json"
	}});
</pre>
<p>We can also provide alternate transports in the socket arguments object. This would allow us to use <code>dojo.io.script.get</code> to connect to a server. However, a more robust solution is to use the <a href="http://www.sitepen.com/blog/2008/07/31/cross-site-xhr-plugin-registry/"><code>dojox.io.xhrPlugins</code></a> for cross-domain long-polling, which will work properly with <code>dojox.socket</code>.</p>
<h2>Auto-Reconnect</h2>
<p>In addition to <code>dojox.socket</code>, we have also added a <code>dojox.socket.Reconnect</code> module. This wraps a socket, adding auto-reconnection support. When a socket is closed by network or server problems, this module will automatically attempt to reconnect to the server on a periodic basis, with a back-off algorithm to minimize resource consumption. We can upgrade a socket to auto-reconnect by this simple code fragment:</p>
<pre lang="javascript">
socket = dojox.socket.Reconnect(socket);
</pre>
<h2>Using Dojo WebSocket with Object Stores</h2>
<p>One of the other big enhancements in Dojo 1.6 is the <a href="http://www.dojotoolkit.org/reference-guide/dojo/store.html">new Dojo object store API</a> (supercedes the Dojo Data API), based on the HTML5 IndexedDB object store API. Dojo 1.6 comes with several store wrappers, and the <code>Observable</code> store provides notification events that work very well with Comet driven updates. <code>Observable</code> is a store wrapper. To use it, we first create a store, and then wrap it with Observable:</p>
<pre lang="javascript">
define("my-module", function(require){
  var JsonRest = require("dojo/store/JsonRest");
  var Observable = require("dojo/store/Observable");
  var store = new JsonRest({data:myData});
  store = Observable(store);
});
</pre>
<p>This store will now provide an <code>observe()</code> method on query results that widgets can use to react to changes in the data. We can notify the store of changes from the server by calling the <code>notify()</code> method on the store:</p>
<pre lang="javascript"> 
socket.on("message", function(event){
  var existingId = event.data.id;
  var object = event.data.object;
  store.notify(object, existingId);
});
</pre>
<p>We can signal a new object by calling <code>store.notify()</code> and omitting the id, and a deleted object by omitting the object (undefined). A changed/updated object should include both.</p>
<h3>Handling Long-Polling from your Server</h3>
<p>Long-polling style connection emulation can require some care on the server-side. For many applications, the server may have sufficient information from request cookies (or other ambient data) to determine what messages to send the browser. However, other applications may vary on what information should be sent to the browser during the life of the application. Different topics may be subscribed to and unsubscribed from. In these situations, the server may need to correlate different HTTP requests with a single connection and its associated state. While there are numerous protocols, one could do this very easily be defining a unique connection and adding that as a header for the socket (the headers are added to each request in the long-poll cycles). For example, we could do:</p>
<pre lang="javascript">
var socket = dojox.socket.LongPoll({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json",
		"Client-Id": Math.random()
	}});
</pre>
<p>In addition, <code>dojox.socket</code> includes a <code>Pragma: long-poll</code> to indicate the first request in a series of long-poll requests to help a server ensure that the connection setup and timeout is properly handled.</p>
<p>We can easily use <code>dojox.socket</code> with other protocols as well:</p>
<h3>CometD</h3>
<p>To initiate a Comet connection with a <a href="http://cometd.org/">CometD</a> server, we can do a CometD handshake, connection, and subscription:</p>
<pre lang="javascript">
var socket = dojox.socket("/cometd");
function send(data){
  return socket.send(dojo.toJson(data));
}
socket.on("connect", function(){
  // send a handshake
  send([
    {
       "channel": "/meta/handshake",
       "version": "1.0",
       "minimumVersion": "1.0beta",
       "supportedConnectionTypes": ["long-polling"] // or ["callback-polling"] for x-domain
     }
  ])
  socket.on("message", function(data){
    // wait for the response so we can connect with the provided client id
    data = dojo.fromJson(data);
    if(data.error){
      throw new Error(error);
    }
    // get the client id for all future messages
    clientId = data.clientId;
    // send a connect message
    send([
      {
         "channel": "/meta/connect",
         "clientId": clientId,
         "connectionType": "long-polling"
       },
       {  // also send a subscription message
         "channel": "/meta/subscribe",
         "clientId": clientId,
         "subscription": "/foo/**"
       }
    ]);
    socket.on("message", function(){
      // handle messages from the server
    });
  });
});
</pre>
<h3>Socket.IO</h3>
<p><a href="http://socket.io/">Socket.IO</a> provides a lower-level interface like dojox.socket, providing simple text-based message passing. Here is an example of how to connect to a Socket.IO server:</p>
<pre lang="javascript">
var args, ws = typeof WebSocket != "undefined";
var socket = dojox.socket(args = {
  url: ws ? "/socket.io/websocket" : "/socket.io/xhr-polling",
  headers:{
    "Content-Type":"application/x-www-urlencoded"
  },
  transport: function(args, message){
    args.content = message; // use URL-encoding to send the message instead of a raw body
    dojo.xhrPost(args);
  };
});
var sessionId;
socket.on("message", function(){
  if (!sessionId){
    sessionId = message;
    args.url += '/' + sessionId;
  }else if(message.substr(0, 3) == '~h~'){
    // a heartbeat
  }
});
</pre>
<h3>Comet Session Protocol</h3>
<p>And here is an example of connecting to a <a href="http://orbited.org/csp/">Comet Session Protocol</a> server (the following example was tested with <a href="http://orbited.org/">Orbited</a>, but could work with <a href="http://hookbox.org/">Hookbox</a>, APE, and others):</p>
<pre lang="javascript">
var args, socket = dojox.socket(args = {
  url: "/csp/handshake"
});
function send(data){
  return socket.send(dojo.toJson(data));
}
var sessionId = Math.random().toString().substring(2);
socket.on("connect", function(){
  send({session:sessionId});
  socket.on("message", function(){
    args.url = "/csp/comet";
    send({session:sessionId});
  });
});
</pre>
<h3>Tunguska</h3>
<p>Tunguska provides a Comet-based interface for subscribing to data changes. This is a very simple protocol which allows us to communicate with a Tunguska server:</p>
<pre lang="javascript">
var socket = dojox.socket({
	url:"/comet",
	headers: {
		"Accept": "application/json",
		"Content-Type": "application/json",
		"Client-Id": Math.random()
	}});  
function send(data){
  return socket.send(dojo.toJson(data));
}
socket.on("connect", function(){
  // now subscribe to all changes for MyTable
  send([{"to":"/MyTable/*", "method":"subscribe"}]);
});
</pre>
<h2>Conclusion</h2>
<p>Dojo&#8217;s socket API is a flexible simple module for connecting to a variety of servers and building powerful, efficient real-time applications without constraints. This adds to the array of awesome new features and improvements in Dojo 1.6.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/10/31/dojo-websocket/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Patr: Promise-based Asynchronous Test Runner</title>
		<link>http://www.sitepen.com/blog/2010/09/21/patr-promise-based-asynchronous-test-runner/</link>
		<comments>http://www.sitepen.com/blog/2010/09/21/patr-promise-based-asynchronous-test-runner/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 07:01:30 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[narwhal]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1719</guid>
		<description><![CDATA[<p>Patr (Promise-based Asynchronous Test Runner) is a simple lightweight cross-platform test runner for promised-based applications. Patr executes tests by simply executing functions of an object and is intended to be used in combination with the &#8220;assert&#8221; module (which is available on NodeJS and Narwhal), so tests can be as simple as: var assert = require("assert"); [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://github.com/kriszyp/patr">Patr</a> (Promise-based Asynchronous Test Runner) is a simple lightweight cross-platform test runner for promised-based applications. Patr executes tests by simply executing functions of an object and is intended to be used in combination with the &#8220;assert&#8221; module (which is available on NodeJS and Narwhal), so tests can be as simple as:</p>
<pre lang="javascript">
var assert = require("assert");
tests = {
  testSomething: function(){
    assert.eq(3, 3);
  }
}
require("patr/runner").run(tests);
</pre>
<p><span id="more-1719"></span></p>
<p>Creating asynchronous tests is easy as well. Inspired by <a href="http://dojocampus.org/content/2009/05/07/a-simple-doh-test-fixture/">Dojo&#8217;s DOH</a> test runner for returning a promise from tests, we don&#8217;t need a new interface to indicate when an asynchronous test is completed. The promise&#8217;s completion or failure indicates the completion or failure of the test. This allows us to elegantly pipe asynchronous operations through to the test runner. Here is an example of an asynchronous test:</p>
<pre lang="javascript">
var fs = require("promised-io/fs");
require("patr/runner").run({
    testFile: function(){
        return fs.readFile("my-file.txt")  // asynchronously read the file
            .then(function(contents){ // next action in promise flow
                // make an assertion
                assert(contents.toString(), "expected contents of the file");
            });
    };
});
</pre>
<p>Now this test will asynchronously read the file, make an assertion, and when completed the returned promise will notify the test runner that the test is complete (or failed) and continue on to the next test.</p>
<p>Tests can be organized into groups by simply creating nested objects: </p>
<pre lang="javascript">
require("patr/runner").run({
    testGroupA: {
      testFile: function(){
        ..
      },
      ..
    },
    testGroupB: {
      testAnother: function(){
      ....
    }
});
</pre>
<p>(Also, remember all your test properties must start with &#8220;test&#8221;.)</p>
<p>The recommended approach for creating test modules is to use the exports object to hold tests. This makes it very easy to run a test module directly or as part of a group. For example:</p>
<pre lang="javascript">
//something.js:
var assert = require("assert");
exports.testSomething: function(){
    assert.eq(3, 3);
};

if (require.main === module)
    require("patr/runner").run(exports);
</pre>
<p>Now we can directly run this module to run these tests. We can also include this module in another set of tests:</p>
<pre lang="javascript">
//all-tests.js:
exports.testSomething = require("./something");
exports.testAnotherGroupOf = require("./more-tests");
if(require.main === module){
    require("patr/runner").run(exports);
}
</pre>
<p>We now have each of these tests within a sub group for easy execution of groups of tests at any level in the hierarchy (all the tests or individual groups).</p>
<h3>Performance Testing</h3>
<p>Patr is also great for performance testing. You can set an iterations flag with in a test group or from the command line to run through the test a specified number of iterations and give performance statistics.</p>
<h2>Summary</h2>
<p>Patr is a lightweight test runner that makes it extremely simple to create test suites and hierarchies with idiomatic JavaScript and asynchronous tests with promises.  Patr is built on <a href="http://www.sitepen.com/blog/2010/09/20/promised-io/">Promised-IO</a>, so it is ready for cross-platform execution, including Node, Narwhal, and within the web browser.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/09/21/patr-promise-based-asynchronous-test-runner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Server-Side JavaScript, Pintura, and Persevere 2.0]]></series:name>
	</item>
		<item>
		<title>Promised-IO</title>
		<link>http://www.sitepen.com/blog/2010/09/20/promised-io/</link>
		<comments>http://www.sitepen.com/blog/2010/09/20/promised-io/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 07:24:22 +0000</pubDate>
		<dc:creator>Kris Zyp</dc:creator>
				<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[Persevere]]></category>
		<category><![CDATA[and Persevere 2.0]]></category>
		<category><![CDATA[narwhal]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[pintura]]></category>
		<category><![CDATA[Server-Side JavaScript]]></category>

		<guid isPermaLink="false">http://www.sitepen.com/blog/?p=1596</guid>
		<description><![CDATA[<p>Promises are a well-established mechanism for modeling future or asynchronous actions. Promises allow asynchronicity while maintaining the core programming principles of composability and encapsulation. Writing asynchronous code in JavaScript can often be a confusing exercise due to the extensive need for callbacks, but promises help to define composable units of asynchronicity to encapsulate actions and [...]</p><p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></description>
				<content:encoded><![CDATA[<p>Promises are a <a href="http://en.wikipedia.org/wiki/Futures_and_promises">well-established mechanism</a> for modeling future or asynchronous actions. Promises allow asynchronicity while maintaining the core programming principles of composability and encapsulation. Writing asynchronous code in JavaScript can often be a confusing exercise due to the extensive need for callbacks, but promises help to define composable units of asynchronicity to encapsulate actions and reliably separate caller and callee&#8217;s concerns.</p>
<h2>Promised-IO</h2>
<p><a href="http://github.com/kriszyp/promised-io">Promised-IO</a> utilizes promises as an abstraction for I/O operations on top of <a href="http://nodejs.org/">Node</a>, <a href="http://narwhaljs.org/">Narwhal</a>/Rhino, and the browser (where possible). This serves two purposes. First, this package provides the benefits of promise usage: clean separation of concerns and proper encapsulation of eventual values. Second, Promised-IO provides a consistent normalized interface for I/O that will work on multiple platforms without sacrificing any of the advantages of asynchronous I/O, making it easy to build modules that can be used by developers on many platforms.</p>
<p><span id="more-1596"></span></p>
<h2>Usage Principles</h2>
<p>One of the characteristics of a good developer is understanding when to apply different abstractions (rather than trying to force a single approach on every situation). Asynchronous code is often more complicated than synchronous code, but can provide big benefits to performance and scalability. In the application of asynchronous I/O there are several different approaches, and each has an appropriate usage.</p>
<h3>Synchronous (don&#8217;t use asynchronous)</h3>
<p>Of course synchronous calls are usually simpler (no addition of callbacks), and for parts of applications that aren&#8217;t &#8220;hot&#8221; parts of concurrent code paths, and aren&#8217;t performance sensitive, this can be the smart choice. For the startup process for servers, long period scheduled tasks, and infrequently executed code, the simplicity and code manageability of using synchronous code can outweigh any performance benefit of asynchronous code.</p>
<p>(There are actually times when synchronous performs better than asynchronous. Asynchronous operations have the additional overhead of handling the low-level callback, then adding the JavaScript callback to the event queue, and finally pulling events of the event loop. Synchronous operations do not have this overhead. For example, for file operations that hit the OS cache, the synchronous operation can often be about twice as fast (half as much CPU time) as the asynchronous counterpart. Of course this is only advantageous for cached data.</p>
<p>Synchronous operations can have a much larger performance advantage when there isn&#8217;t to-the-core support for asynchronicity. Fortunately, most of the I/O operations in NodeJS leverage libeio and low level async Posix operations for good performance, but for actions that are natively synchronous, some libraries use libeio&#8217;s <code>eio_custom</code> function to make it asynchronous. This function can be very expensive and should only be used for operations that will take a significant amount of time (more than about 0.1ms). I have observed synchronous operations executing hundreds of times faster than the asynchronous counterparts when <code>eio_custom</code> is used.</p>
<p>All this being said, most of the time asynchronous is a good choice for code that needs to scale, but it is important to know when and when not to use it.)</p>
<h3>Event Registration</h3>
<p>This is the typical way that one is notified of events in the browser and in NodeJS. You register for an event and are notified of the event anytime after the registration (no notification of past events). Event registration is the most generic and low-level asynchronous API. NodeJS uses this model exclusively because it is a low-level platform for I/O. For some situations (see below) it is appropriate to build abstractions on top of this API. However, event registration is the right choice if multiple events may take place and you only want future events.</p>
<h3>Promises</h3>
<p>An asynchronous abstraction designed specifically to model an action that has a single eventual completion (successful or failing). Promises act much like a value (returned from the result of a function call or computation), except that the result is asynchronous, and may or may not be immediately available. Promises are more specific than event registration, providing encapsulation (that can easily be passed around) of an eventual value and decoupling the receiver from the timing of the underlying events that fulfill the action (whether they be past or future). Promises are the right choice for asynchronous actions that have a single point of eventual completion.</p>
<h3>Streams</h3>
<p>Streams are an asynchronous abstraction for the progressive flow of sequential data. Streams are important for allowing a sender to feed data to a receiver without requiring large scale buffering at either end. Promised-IO implements streams with &#8220;lazy arrays&#8221;, an array-like interface that follows the standard API of JavaScript Arrays. This greatly improves the modularity of streams since they can be used with code that expects standard arrays. Lazy arrays are indeed lazy, following the functional programming principle of lazy evaluation as well. Iterative methods including map, some, filter, and every are purely functional and only iterate through the stream as needed, avoiding any unnecessary buffering or computations. Promised-IO&#8217;s streams also utilize promises to signal the eventual completion of a stream (for the end of a file or end of a HTTP response, for example), for consistency. For situations where you want to model sequential data, streams/lazy arrays are appropriate.</p>
<h2>Promised-IO Modules</h2>
<p>There are several key modules available in Promised-IO for access to promise-based I/O.</p>
<h3>fs</h3>
<p>The &#8220;fs&#8221; module implements key parts of the <a href="http://wiki.commonjs.org/wiki/Filesystem/A">CommonJS file system API</a> (the entire API will eventually be supported). It also provides Node&#8217;s file system API where each asynchronous function returns a promise rather than taking callback parameters (fortunately these two APIs can easily coexist). This gives you access to a number of the convenience functions from CommonJS&#8217;s API (like <code>makeTree()</code>) and gives normalizable access to asynchronous functions. This module is obviously not available for browser usage. Here is an example of using <code>readFile()</code> (an asynchronous function from Node&#8217;s API):</p>
<pre lang="javascript">
var fs = require("promised-io/fs");
return fs.readFile("my-file.txt").then(function(contents){
   // once it is loaded, we can do something with the contents
});
</pre>
<p>One of the most important functions in the &#8220;fs&#8221; module is the <code>open()</code> function which can be used in several powerful ways. The open function returns a file descriptor for use with Node&#8217;s functions that take a descriptor (the read, write, and close functions). While the open function opens the file asynchronously, the returned object can be directly passed to read, write, and close functions (they will execute once the file is open):</p>
<pre lang="javascript">
var fs = require("promised-io/fs");
var myFile = fs.open("my-file.txt", "r");
fs.read(myFile, buffer, offset, length, position).then(function(){
  // buffer should now be filled
});
</pre>
<p>The returned object is also a promise for the completion of the open operation:</p>
<pre lang="javascript">
var when = require("promised-io/promise").when;
var myFile = when(fs.open("my-file.txt", "r"), onSuccess, onFailure);
</pre>
<p>The object returned from open also has methods (based on asynchronous versions of the CommonJS filesystem API) for writing and closing:</p>
<pre lang="javascript">
var myFile = fs.open("my-file.txt", "a");
myFile.write("some data").then(myFile.close);
</pre>
<p>And finally, the returned file object can be treated like a JavaScript array for streamed reading (lazy arrays):</p>
<pre lang="javascript">
var myFile = fs.open("my-file.txt", "r");
myFile.forEach(function(block){
    // called for each block of data
}).then(myFile.close); // The forEach returns promise for the completion of the reading
</pre>
<p>The <code>some()</code> JavaScript array method can be particularly useful if you might not need to traverse the whole file:</p>
<pre lang="javascript">
var myFile = fs.open("my-file.txt", "r");
myFile.some(function(block){
    // if we are searching for something, we can exit be returning true at any time
});
</pre>
<p>One other useful aspect of using these lazy arrays is that we can return the file object directly as the body of JSGI responses and it will automatically be piped to the client.</p>
<pre lang="javascript">
function SomeJSGIApp(request){
  return {
    status: 200,
    headers: {"content-type":"text/plain"},
    body: fs.open("my-file.txt", "r");
  };
}
</pre>
<h3>http-client</h3>
<p>An HTTP client that follows the JSGI API. Originally developed for handling incoming HTTP requests (for web servers) and returning responses, http-client effectively uses the JSGI API for the reverse situation, allowing for the construction of JSGI requests and receiving JSGI responses. This module allows for some extra convenience properties and defaults to make it easy to create a request. You can simply provide a &#8220;url&#8221; property to indicate the target, and then all other properties are optional. For example:</p>
<pre lang="javascript">
var request = require("promised-io/http-client").request;
request({
  url:"http://somesite.com/data",
  method: "GET", // optional
  headers: {} // also optional
}).then(function(response){
  response.status -> http status code
  response.headers -> http response headers
  response.body -> A lazy array stream representing the body of the response
});
</pre>
<p>This module can be used on the server and browser.</p>
<h3>delay</h3>
<p>This module provides promise-based scheduling and timing similar to that of <code>setTimeout</code> and <code>setInterval</code>, but utilizing promises. The two exported functions are <code>delay(ms)</code> and <code>schedule(ms)</code>. The <code>delay()</code> function returns a promise that is fulfilled after the specified amount of time. The <code>schedule()</code> function returns a lazy array that iterates periodically by the given amount of time. For example:</p>
<pre lang="javascript">
var delay = require("promised-io/delay").delay;
delay(2000).then(function(){
  // and two seconds later this executes
});
</pre>
<p>This module can also can be used on the server and browser.</p>
<h3>process</h3>
<p>This module implements the CommonJS &#8220;system&#8221; module API and provides access to a <code>print()</code> function and process arguments.</p>
<h2>Summary</h2>
<p>Promised-IO provides a robust, cross-platform I/O system based on the proven design principles of promises for efficient, portable, asynchronous JavaScript.</p>
<p>SitePen offers beginner, intermediate, and advanced Dojo Toolkit workshops to make your development team as skilled and efficient as possible when creating dynamic, responsive web applications.  <a href="http://sitepen.com/services/workshops/index.php">Sign up today!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.sitepen.com/blog/2010/09/20/promised-io/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<series:name><![CDATA[Server-Side JavaScript, Pintura, and Persevere 2.0]]></series:name>
	</item>
	</channel>
</rss>
