<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>ThingsILearned &#187; Orbited</title>
	<atom:link href="http://thingsilearned.com/category/orbited/feed/" rel="self" type="application/rss+xml" />
	<link>http://thingsilearned.com</link>
	<description>something new every day ... or so  By Dave Fowler</description>
	<lastBuildDate>Sun, 05 Feb 2012 18:47:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='thingsilearned.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>ThingsILearned &#187; Orbited</title>
		<link>http://thingsilearned.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://thingsilearned.com/osd.xml" title="ThingsILearned" />
	<atom:link rel='hub' href='http://thingsilearned.com/?pushpress=hub'/>
		<item>
		<title>Starting Out With Comet (Orbited) Part 3 – The Client</title>
		<link>http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%e2%80%93-the-client/</link>
		<comments>http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%e2%80%93-the-client/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 20:58:46 +0000</pubDate>
		<dc:creator>godavemon</dc:creator>
				<category><![CDATA[comet]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Orbited]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://thingsilearned.com/?p=567</guid>
		<description><![CDATA[Comet implementations require both server and client side components.  In Part 2 of this series we installed and configured the server side component and then used the example STOMP Test client to test it out and get a feel for what was going on. In this part of the series we&#8217;ll be covering the client [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=567&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Comet implementations require both server and client side components.  In <a href="http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/">Part 2</a> of this series we installed and configured the server side component and then used the example STOMP Test client to test it out and get a feel for what was going on.</p>
<p>In this part of the series we&#8217;ll be covering the client side to make our custom interface to the comet server.  To demonstrate we&#8217;ll go through the steps in creating an app I call EZChat which is basically a bare bones comet chat client.  The interface will allow you to choose a name and submit messages that will be broadcast to everyone viewing the page in real time.</p>
<p>For it to work you will have to have the configuration of <a href="http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/">Part 2</a> setup and your Orbited server running.  For reference I learned most of the client side code in this example by playing with the source of the example <a href="http://localhost:9000/static/demos/stomp/">STOMP Test client</a>. I recommend taking a look at that source if you need more advanced options or another example.</p>
<h2>Includes</h2>
<p>At the top of the &lt;head&gt; you first need to include the libraries for the Orbited (/static/Orbited.js) and STOMP (/static/protocols/stomp/stomp.js) client-side implementations.</p>
<p><pre class="brush: xml;">

&lt;script&gt;document.domain=document.domain&lt;/script&gt;
 &lt;script src=&quot;http://localhost:9000/static/Orbited.js&quot;&gt;&lt;/script&gt;
 &lt;script&gt;
 Orbited.settings.port = 9000;
 TCPSocket = Orbited.TCPSocket;
 &lt;/script&gt;
 &lt;script src=&quot;http://localhost:9000/static/protocols/stomp/stomp.js&quot;&gt;&lt;/script&gt;

 &lt;script src=&quot;http://www.json.org/json2.js&quot;&gt;&lt;/script&gt;

</pre></p>
<p>In between the scripts we&#8217;ve set up a TCPScocket.  It must be don between the two script includes because the STOMP library needs the socket setup for its execution.  We&#8217;ve also specified the Orbited port which is necessary especially if you change the port on which the orbited and stomp javascript files are hosted on (they can both be hosted on port 80 along with your other scripts).</p>
<p>Lastly, we&#8217;ve also included a popular <a href="http://www.json.org/js.html">JSON library</a>.  Typically I use jquery-json but I&#8217;ve kept this tutorial free of javascript frameworks so as not to add unneeded complexity.  If you&#8217;d like you can of course switch to whatever JSON library you&#8217;re used to; just replace the JSON.stringify and JSON.parse functions with your equivalents.</p>
<h2>STOMP Setup</h2>
<p>Because we&#8217;re dealing with the setup we&#8217;ll skip to the bottom of the page and add the following script just before the end tag of the body (&lt;/body&gt;).</p>
<p><pre class="brush: jscript;">

&lt;script type=&quot;text/javascript&quot;&gt;
(function() { // set up stomp client.

stomp = new STOMPClient();

 stomp.onconnectedframe = function() {  // Run on initial connection to STOMP (comet) server
 stomp.ready = true;
 // subscribe to channel CHANNEL = &quot;/ezchat/&quot;
 var CHANNEL = '/ezchat/'
 stomp.subscribe(CHANNEL);
 };

 stomp.onmessageframe = function(frame) {  // Executed when a messge is received
 my_receive( JSON.parse(frame.body) );
 };

 // Everything is setup. Start the connection!
stomp.connect(document.domain, 61613); //, 'guest', 'guest');
})();
&lt;/script&gt;

</pre></p>
<p>My apologies as always for the crappiness of the wordpress syntax parser.   Lets walk through what&#8217;s happening.</p>
<p>At the top, we initialize a new STOMPClient object.  The StompClient has the following hooks you can override to trigger your own events.</p>
<p style="padding-left:30px;"><strong>onopen</strong> &#8211; Called when the Transport is opened<br />
<strong>onclose</strong> &#8211; Called when the Transport has closed<br />
<strong>onerror</strong> &#8211; Called when the Stomp Client has errored<br />
<strong>onerrorframe</strong> &#8211; Called when there is an error in the message received<br />
<strong>onconnectedframe</strong> &#8211; Called when a the client is fully set up for sending/receiving<br />
<strong>onmessageframe</strong> &#8211; Called when a message is received</p>
<p>The STOMP object also has these functions for connecting and resetting the connections.</p>
<p style="padding-left:30px;"><strong>reset</strong> &#8211; Resets the STOMP connection<br />
<strong>connect</strong> &#8211; Connects to the STOMP server<br />
<strong>send</strong> &#8211; Sends the object in the first argument to the channel specified by the second argument</p>
<p>In our simple example only the onconnectedframe and onmessage frames need to be overwritten.</p>
<p>The onconnectedframe function is called when the STOMP server has been connected to and everything is setup for sending and receiving messages.  Inside this function we simply need to subscribe/listen to the CHANNEL we&#8217;ve setup for our chat.  For the example I&#8217;ve chosen the channel &#8216;/ezchat/&#8217;.  Once subscribed our STOMP client will receive any messages sent to that channel in real time.  You can subscribe to multiple channels if you&#8217;d like, and you can make clients with different channels if you&#8217;d like to have different chat rooms.  But for this example we&#8217;ll just stick with the hard coded &#8216;/ezchat/&#8217; channel.</p>
<p>The onmesageframe function is called when a message has been received.  It is passed a frame object with the following structure</p>
<p style="padding-left:30px;"><strong>frame</strong></p>
<ul style="padding-left:30px;">
<li>body: &#8220;{&#8220;name&#8221;:&#8221;Dave&#8221;,&#8221;message&#8221;:&#8221;awesome this is working&#8221;}&#8221;</li>
<li>headers: Object
<ul>
<li>content-encoding: &#8220;utf-8&#8243;</li>
<li>content-length: &#8220;51&#8243;</li>
<li>content-type: &#8220;text/plain&#8221;</li>
<li>destination: &#8220;/ezchat/&#8221;</li>
<li>message-id: &#8220;/ezchat/_3&#8243;</li>
</ul>
</li>
</ul>
<ul style="padding-left:30px;">
<li>type: &#8220;MESSAGE&#8221;</li>
</ul>
<p>where the body holds the information that has been sent.  The STOMP server and client add extra &#8220;type&#8221; and &#8220;headers&#8221; objects to communicate between each other.  The extra information can be very useful for more complicated applications but for our simple example we&#8217;re only interested in the frame &#8220;body&#8221;.</p>
<p>So you see that the onmessageframe is simply parsing the json object in frame.body of every received message and passing it to my_receive, a function we will soon create.</p>
<h2>The Content</h2>
<p>For EZChat we need a form where users can specify a name and type messages to send.  We also need an area to put the messages.  Plop this HTML in at the top of the &lt;body&gt; to handle all of that.</p>
<p><pre class="brush: xml;">
&lt;h2&gt;EZChat - Example Comet Client!&lt;/h2&gt;
Everyone viewing this page will see the messsages you submit instantly.
&lt;form id=&quot;message_form&quot; action=&quot;#&quot;&gt;
 Name:
 &lt;input type=&quot;text&quot; name=&quot;chat_name&quot; id=&quot;chat_name&quot;&gt;&lt;/input&gt;
 Message:
 &lt;textarea name=&quot;message&quot; id=&quot;message&quot; rows=&quot;4&quot; cols=&quot;40&quot;&gt;&lt;/textarea&gt;
 &lt;input type=&quot;submit&quot; name=&quot;Send&quot; onclick=&quot;return my_send(); return false&quot;&gt;&lt;/input&gt;
&lt;/form&gt;
&lt;div id=&quot;messages&quot;&gt;&lt;!--- All received messages will get placed here ---&gt;&lt;/div&gt;
</pre></p>
<p>There are a few things to notice here.  First, the important elements in the form have ids &#8216;chat_name&#8217; and &#8216;message&#8217; and the area that will be containing all the received messages is called &#8216;messages&#8217;.  The names don&#8217;t matter except that we&#8217;ll use them in the functions we create later.</p>
<p>Second, the onclick event of the submit button is overridden with instead calling the my_send function.  We&#8217;ll make this function in the next step.</p>
<h2>The Functions</h2>
<p>Lastly we need to make the custom my_send and my_receive functions that get called to send and receive messages.  Insert these functions into the head after the includes.</p>
<h3>my_send</h3>
<p>The my_send function will get the values from the &#8216;chat_name&#8217; and &#8216;message&#8217; form elements, combine them in an object, convert the object to json, and then sends it to the &#8216;/ezchat/&#8217; channel.  The sending is handled using the stomp.send command which takes as input the object to send and second, the channel to send it to.</p>
<p><pre class="brush: jscript;">
var CHANNEL = '/ezchat/';
function my_send() { 

// Get the values to send from the form
 var name = document.getElementById('chat_name').value;
 var message = document.getElementById('message').value;

 var msg = {'name': name, 'message': message};

 var json_msg = JSON.stringify(msg);
 stomp.send(json_msg, CHANNEL)
 return false;
 }

</pre></p>
<h3>my_receive</h3>
<p>As discussed earlier the my_receive function gets the JSON parsed version of whatever was sent in &#8216;frame.body&#8217;.  In the case of our app its always an object of the format</p>
<p style="padding-left:30px;">msg = {&#8216;name&#8217;: &lt;some name&gt;, &#8216;message&#8217;: &lt;some message&gt;}</p>
<p>The my_receive function simply takes this object and converts it into a prettier HTML format and appends it to the top of the message list we created in the HTML.</p>
<p><pre class="brush: jscript;">
function my_receive( msg ) {
 console.log('received message', msg);
 // append the &lt;msg&gt; to the top of the list of messages.
 var messages_el = document.getElementById('messages');
 var new_message = &quot;
&lt;div&gt;&lt;strong&gt;&quot; + msg['name'] + &quot;:&lt;/strong&gt; &quot; + msg['message'] + &quot;&lt;/div&gt;
&quot;;
 messages_el.innerHTML = new_message + messages_el.innerHTML;
 }
</pre></p>
<p>That&#8217;s it for the code.  Scroll to the bottom of the page for the full version of the source.</p>
<h2>Results</h2>
<p>Ensure that your Orbited server is running as described in Part 2 of the series.  Then load up the page we&#8217;ve made in two or more separate windows.  Choose a different name for each window and start sending each other messages.  You&#8217;ll notice that both windows will receive the submitted messages almost instantaneously!  The beauty of Comet!</p>
<p><img class="aligncenter size-full wp-image-573" title="ezchat" src="http://thingsilearned.files.wordpress.com/2009/08/ezchat1.png?w=570" alt="ezchat"   />If you open your <a href="http://localhost:9000/static/demos/stomp/">STOMP Test Client</a> and subscribe to the &#8216;/ezchat/&#8217; channel you&#8217;ll see a more raw input on what&#8217;s actually being received by the STOMP clients as you chat.</p>
<p><img class="aligncenter size-full wp-image-574" title="ezchatstomp" src="http://thingsilearned.files.wordpress.com/2009/08/ezchatstomp.png?w=570" alt="ezchatstomp"   />You can see how the STOMP Test client is incredibly handy for debugging.</p>
<p>That&#8217;s it for this part of the Tutorial!  There are still parts to come including writing a data handler on the server side and hopefully a much requested post on Django integration.</p>
<h2>Full Source</h2>
<p>For your convenience here&#8217;s the full index.html file for this example.</p>
<p><pre class="brush: xml;">

 &lt;html&gt;

&lt;head&gt;
 &lt;script&gt;document.domain=document.domain&lt;/script&gt;
 &lt;script src=&quot;http://localhost:9000/static/Orbited.js&quot;&gt;&lt;/script&gt;
 &lt;script&gt;
 Orbited.settings.port = 9000;
 TCPSocket = Orbited.TCPSocket;
 &lt;/script&gt;
 &lt;script src=&quot;http://localhost:9000/static/protocols/stomp/stomp.js&quot;&gt;&lt;/script&gt;

 &lt;script src=&quot;http://www.json.org/json2.js&quot;&gt;&lt;/script&gt;

 &lt;script type=&quot;text/javascript&quot;&gt;
 // These are our custom functions for sending and receiving STOMP messages.
 // They will be sent in the format msg = {'name': somename, 'message': somemessage}

 var CHANNEL = '/ezchat/';

 function my_receive( msg ) {
 console.log('received message', msg);
 // append the &lt;msg&gt; to the top of the list of messages.
 var messages_el = document.getElementById('messages');
 var new_message = &quot;
&lt;div&gt;&lt;strong&gt;&quot; + msg['name'] + &quot;:&lt;/strong&gt; &quot; + msg['message'] + &quot;&lt;/div&gt;
&quot;;
 messages_el.innerHTML = new_message + messages_el.innerHTML;
 }
 function my_send() {
 // Get the values to send from the form      

 var name = document.getElementById('chat_name').value;
 var message = document.getElementById('message').value;

 var msg = {'name': name, 'message': message};
 console.log(msg);

 var json_msg = JSON.stringify(msg);
 console.log(json_msg);
 stomp.send(json_msg, CHANNEL)
 return false;
 }       
 &lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;h2&gt;EZChat - Example Comet Client!&lt;/h2&gt;
&lt;div&gt;Everyone viewing this page will see the messsages you submit instantly.&lt;/div&gt;
&lt;form id=&quot;message_form&quot; action=&quot;#&quot;&gt;
 Name:
 &lt;input type=&quot;text&quot; name=&quot;chat_name&quot; id=&quot;chat_name&quot;&gt;&lt;/input&gt;
 Message:
 &lt;textarea name=&quot;message&quot; id=&quot;message&quot; rows=&quot;4&quot; cols=&quot;40&quot;&gt;&lt;/textarea&gt;
 &lt;input type=&quot;submit&quot; name=&quot;Send&quot; onclick=&quot;return my_send(); return false&quot;&gt;&lt;/input&gt;
&lt;/form&gt;
&lt;div id=&quot;messages&quot;&gt;&lt;!--- All received messages will get placed here ---&gt;&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
(function() { // set up stomp client.
 stomp = new STOMPClient();
 stomp.onconnectedframe = function() {  // Run on initial connection to STOMP (comet) server
 stomp.ready = true;
 // subscribe to channel CHANNEL = &quot;/ezchat/&quot;
 stomp.subscribe(CHANNEL);          
 };

 stomp.onmessageframe = function(frame) {  // Executed when a messge is received
 console.log('frame is', frame);
 my_receive( JSON.parse(frame.body) );
 };

 // Everything is setup. Start the connection!
 stomp.connect(document.domain, 61613); //, 'guest', 'guest');
})();
&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

</pre><br />
As usual, if there are corrections or questions please be sure to leave them in the comments.  And remember to <a href="http://thingsilearned.com/feed/">subscribe</a> to catch the rest of the series.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thingsilearned.wordpress.com/567/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thingsilearned.wordpress.com/567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thingsilearned.wordpress.com/567/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=567&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%e2%80%93-the-client/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4b0e75fea564a9693a4592692e6baeb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">godavemon</media:title>
		</media:content>

		<media:content url="http://thingsilearned.files.wordpress.com/2009/08/ezchat1.png" medium="image">
			<media:title type="html">ezchat</media:title>
		</media:content>

		<media:content url="http://thingsilearned.files.wordpress.com/2009/08/ezchatstomp.png" medium="image">
			<media:title type="html">ezchatstomp</media:title>
		</media:content>
	</item>
		<item>
		<title>Starting Out With Comet (Orbited) Part 2 &#8211; Installation and STOMP</title>
		<link>http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/</link>
		<comments>http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 19:16:20 +0000</pubDate>
		<dc:creator>godavemon</dc:creator>
				<category><![CDATA[comet]]></category>
		<category><![CDATA[Orbited]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://thingsilearned.com/?p=509</guid>
		<description><![CDATA[In this part of the tutorial we will install and setup the server side of a Comet installation using the Orbited implementation.  We&#8217;ll also be using the MorbitQ STOMP server to handle message passing and we&#8217;ll play around with STOMP/Comet setup using Orbited&#8217;s STOMP Test demo.  If you have not read the first blog post [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=509&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this part of the tutorial we will install and setup the server side of a Comet installation using the Orbited implementation.  We&#8217;ll also be using the MorbitQ STOMP server to handle message passing and we&#8217;ll play around with STOMP/Comet setup using Orbited&#8217;s STOMP Test demo.  If you have not read the <a href="http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/">first blog post</a> in this series I advise you do so.  I will be assuming you have an understanding of those terms and concepts of the previous post for this tutorial.</p>
<p>Before we start I&#8217;d like to quote my source.  I&#8217;ve learned most of what I&#8217;m sharing from <a href="http://cometdaily.com/2008/10/10/scalable-real-time-web-architecture-part-2-a-live-graph-with-orbited-morbidq-and-jsio/">Michael Carter&#8217;s Tutorial</a> and many hours of playing around.</p>
<h2>Installation</h2>
<p>We will need to install the latest version of Orbited and also some Stomp tools.  The stomp tools aren&#8217;t required for this step but will be for anything else you want to do with comet so I&#8217;ve included them in the installation steps here as well.  Conveniently Orbited is setup in the Cheeseshop.  You need <a href="http://www.python.org/download/">python2.5+</a> and if you have not installed the <a href="http://peak.telecommunity.com/DevCenter/EasyInstall">python setup-tools</a> do so now.</p>
<p>Installation simply consists of the following commands.</p>
<pre>easy_install twisted
easy_install orbited
easy_install stompservice
easy_install simplejson</pre>
<p>To test if it works enter your python shell and test importing the libraries.  The following should load without any errors</p>
<p>$ python<br />
&gt;&gt;&gt; import twisted<br />
&gt;&gt;&gt; import orbited<br />
&gt;&gt;&gt; import stompservice<br />
&gt;&gt;&gt; import simplejson</p>
<p>If you have any troubles there is more info on the <a href="http://www.orbited.org/wiki/Installation">Orbited Installation Guide</a>.</p>
<h2>Configuration</h2>
<p>Orbited is configured with a &#8216;.cfg&#8217; file. Lets call ours example.cfg.  Make a directory anywhere for your project and paste the following into example.cfg</p>
<pre>[global]
session.ping_interval = 300

[listen]

http://:9000

stomp://:61613

[access]
* -&gt; localhost:61613</pre>
<p>Lets go through the different parts.</p>
<p>The ping interval is a number of seconds for the backend to wait before it pings the client.  We&#8217;ve got it set up for 5 minutes.  Good comet implementations have some sort of pinging system.  This is a necessary step as due to current HTTP protocols the client cannot tell if something has gone wrong on the server end.  It simply waits happily all day for some sort of response for the server.  But with a pinging system setup we can tell the client to refresh its connection if it hasn&#8217;t heard from the server in the last 300 seconds, and the server will make sure to ping the client at least every 300 seconds, letting it know that the connection is still alive.</p>
<p>The listen parameters tell the orbited server which ports to listen to and who to proxy requests to.  In our configuration port 9000 will be serving static html files, and port 61613 will be a proxy for our STOMP server.</p>
<p>And finally, the access parameter gives permission to proxy to the stomp server.</p>
<h2>Lets Run It</h2>
<p>To run enter your project directory and type</p>
<p>orbited &#8211; -config example.cfg</p>
<p>It should look something like this:</p>
<p>06/24/09 21:05:24:651 INFO   orbited.start    proxy protocol active<br />
06/24/09 21:05:24:511 INFO   orbited.start    Listening http@9000<br />
06/24/09 21:05:24:525 INFO   orbited.start    Listening stomp@61613</p>
<h2>STOMP Test</h2>
<p>Orbited comes with a really nice STOMP demo that also serves as a nice tool for debugging your setup later.  We&#8217;ll use it to play around with Comet and understand the concepts behind STOMP.</p>
<p>While your orbited server is running visit the following URL.</p>
<p>http://localhost:9000/static/demos/stomp/</p>
<p><img class="aligncenter size-full wp-image-517" title="cometwindow" src="http://thingsilearned.files.wordpress.com/2009/06/cometwindow1.png?w=570" alt="cometwindow"   />There are 3 important tools/rows we&#8217;ll be using here: Connect, Subscribe and Send.</p>
<p>First click on &#8220;Connect&#8221; to connect to the orbited and stomp servers.  We&#8217;re using MorbitQ has the stomp client, which doesn&#8217;t deal with authentication, so any name and password will work.  Notice that the STOMP test shell will now say.</p>
<p>→ Transport openned<br />
→ Connected as user guest</p>
<p>Second change the &#8220;destination&#8221; in the &#8220;Subscribe&#8221; row to be &#8220;/channel/1/&#8221; and click Subscribe.  You have now created and subscribed to a channel called &#8220;/channel/1/&#8221;.</p>
<p>Next we&#8217;ll send something to that channel using the Send tool.  Again change the destination to &#8220;/channel/1/&#8221; and type something into the message box replacing &#8220;hello&#8221;.  In the image above I&#8217;ve chosen &#8220;comet is working!&#8221;.  Now hit Send and notice that your message shows up in your STOMP shell!</p>
<p>Try sending to other destinations and notice that only messages sent to &#8220;/channel/1/&#8221; will show up in the stomp shell.  We can change that however by subscribing to additional channels.  Try subscribing to &#8220;/anotherchannel/&#8221; and then send it a message.  Notice that this setup can handle being subscribed to many different channels at once.</p>
<h2>The Real Power</h2>
<p>All of the things we&#8217;ve tried so far could have been fairly easily implemented with simple AJAX.  The real power of comet is that it can push information to the client without having to submit a request.  Also, the real power of STOMP is that it smoothly handles message passing between clients.  Lets demonstrate both of these now by opening up multiple browser windows all pointing to our STOMP Test.</p>
<p><img class="aligncenter size-full wp-image-518" title="stompmultiple" src="http://thingsilearned.files.wordpress.com/2009/06/stompmultiple.png?w=570" alt="stompmultiple"   />Open up 3 windows and &#8220;Connect&#8221; them each to the server.  Now subscribe the second window to &#8220;/channel/1/&#8221;, the third window to &#8220;/channel/2/&#8221; and the first window to both.</p>
<p>Once setup, using the first window send a message &#8220;Message to Channel 1&#8243; to &#8220;/channel/1/&#8221;.  You&#8217;ll notice that it not only showed up instantly in window 1 (where you submitted), but also in window 2 (where you did nothing)!  The STOMP server has passed the message all clients listening to &#8220;/channel/1/&#8221; and the Comet server has pushed it to the client without it having to poll for updates!</p>
<p>Now send a message to &#8220;/channel/2/&#8221; and notice that it shows up in windows 1 and 3, but not 2.</p>
<p>Play around with this setup more to become familiar.  Each window can subscribe to any number of channels, and each can send messages to any channel, whether it is subscribed to it or not.</p>
<h2>Whats Next</h2>
<p>We&#8217;ve now setup and tested a Comet implementation.  You can see its benefits and understand how it works with STOMP.  In the next example we&#8217;ll work on the client side of the Comet implementation and write a python STOMP client to handle processing and sending the data on a comet server.</p>
<p>In the mean time you may want to look at the <a href="http://localhost:9000/static/demos/">other demo&#8217;s</a> that came with Orbited or <a href="http://cometdaily.com/2008/10/10/scalable-real-time-web-architecture-part-2-a-live-graph-with-orbited-morbidq-and-jsio/">Michael&#8217;s Demo</a>.</p>
<p><strong>Update:</strong> <a href="http://thingsilearned.com/2009/08/03/starting-out-with-comet-orbited-part-3-%e2%80%93-the-client/">Part 3 &#8211; The Client</a> is now available.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thingsilearned.wordpress.com/509/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thingsilearned.wordpress.com/509/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thingsilearned.wordpress.com/509/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=509&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4b0e75fea564a9693a4592692e6baeb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">godavemon</media:title>
		</media:content>

		<media:content url="http://thingsilearned.files.wordpress.com/2009/06/cometwindow1.png" medium="image">
			<media:title type="html">cometwindow</media:title>
		</media:content>

		<media:content url="http://thingsilearned.files.wordpress.com/2009/06/stompmultiple.png" medium="image">
			<media:title type="html">stompmultiple</media:title>
		</media:content>
	</item>
		<item>
		<title>Starting Out With Comet (Orbited) Part 1</title>
		<link>http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/</link>
		<comments>http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 21:12:07 +0000</pubDate>
		<dc:creator>godavemon</dc:creator>
				<category><![CDATA[comet]]></category>
		<category><![CDATA[Orbited]]></category>

		<guid isPermaLink="false">http://thingsilearned.com/?p=492</guid>
		<description><![CDATA[This is the first article in a series I&#8217;m creating to ease developers into using Comet.  The documentation is severely lacking on every comet implementation I&#8217;ve come across which I think is Comet&#8217;s biggest limitation at the moment. Hopefully this will help those interested in Comet to struggle less with their implementations than I did/do. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=492&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the first article in a series I&#8217;m creating to ease developers into using Comet.  The documentation is severely lacking on every comet implementation I&#8217;ve come across which I think is Comet&#8217;s biggest limitation at the moment. Hopefully this will help those interested in Comet to struggle less with their implementations than I did/do.</p>
<p>The series will specifically focus on using the <a href="http://orbited.org/">Orbited</a> implementation of Comet, but many of the concepts will apply to other Comet implementations as well.</p>
<p>This first post will explain some terms and concepts you need to become familiar with.</p>
<h2>Comet</h2>
<p>Comet is a term referring to a set of techniques (hacks) that enable the server to push data to the client whenever it wants.  Traditionally the client has to initiate all requests.  If the server wants to send something to the client it has to wait until the client chooses to connect again.</p>
<p>Comet is a set of methods for the server to ping the client.</p>
<p>As a definition, Comet is as arbitrary as the word AJAX.  Try not to get hung up on the word any more than you&#8217;d get hung up on defining what AJAX is.</p>
<h2>What and Why Orbited</h2>
<p>Orbited is a Python based implementation of Comet using the Twisted framework.  There&#8217;s not a lot out there discussing the pro&#8217;s and cons of the different comet services so its hard to tell which implementation is better than another.  There is clearly not yet a dominant implementation.</p>
<p>I basically chose Orbited because it was the first one I could get up and running thanks to <a href="http://cometdaily.com/2008/10/10/scalable-real-time-web-architecture-part-2-a-live-graph-with-orbited-morbidq-and-jsio/">this tutorial</a>, because I love python, and because my implementation will be working with Django.</p>
<p>Orbited is built on the Twisted framework and is event based, so in theory it should scale just fine.</p>
<p>Its important to note that Orbited has both a server side component and a JavaScript library for dealing with the client side of Comet connections as well.</p>
<h2>Long-Polling</h2>
<p>Long-polling is the technique Orbited uses to maintain a consistent real-time connection between the server and the client, enabling the server to push data to the client whenever it wants. Put simply the client makes a request that is kept open (no immediate response is returned).  The server can continue to send data to the client through this open connection until it is terminated by the client.</p>
<h2>Streaming</h2>
<p>Streaming is another Comet technique.  Michael Carter, one of Orbited&#8217;s main contributors has a <a href="http://cometdaily.com/2007/11/16/more-on-long-polling/">nice article</a> on the advantages of the two techniques.</p>
<h2>STOMP</h2>
<p>Short for <em>Streaming Text Orientated Messaging Protocol</em>, STOMP is is a message passing protocol commonly used in Comet implementations.  It enables a publish/subscribe (pub/sub) model that comes in handy in many real-time applications.</p>
<p>STOMP isn&#8217;t a required part of a Comet implementation, but we&#8217;re going to be using it in this series.</p>
<h2>MorbidQ</h2>
<p><a href="http://www.morbidq.com/">MorbidQ</a> is a STOMP server that comes with Orbited.  Think of it as Apache, but for STOMP instead of web apps.  Its great in that its easy to set up and work with, but its not robust enough for a significant load.  When deploying you should instead use <a href="http://www.rabbitmq.com/">RabbitMQ</a>.</p>
<h2>Pub/Sub</h2>
<p>In the following posts we will be using a publish/subscribe model thanks to the help of STOMP.  With this model you can create channels and both publish and subscribe to them.  This model works great when multiple clients are listening to the same thing, such as a group chat or a game.</p>
<p>I like to think of it as a telegraphy system.  A whole bunch of people can hook up to the wire, and if they do they hear everything everyone else is broadcasting, and can broadcast their own messages as well.</p>
<p>So subscribing is like hooking up your headphones to the transmission lines and publishing is like sending out a signal with your <a href="http://en.wikipedia.org/wiki/File:L-Telegraph1.png">morse key</a>.  A different channel would correspond to a different wire.</p>
<h2>Orbited Server</h2>
<p>Comet requires an event based server to deal with the long requests.  Typical servers like Apache for instance were designed to handle a request as quickly as possible.  They allocate memory to returning a response as soon as a request is received, and maintain that memory until a response is returned.</p>
<p>An event-based server such as Orbited can chop up the different components of the request/response process.  It can accept a request and then forget about it while allocating its memory elsewhere until at some later point something needs to be done (a response returned or data transmitted).  For this reason it can handle dramatically more open connections than Apache.</p>
<p>Lets use an example.  Pretend we have a typical Apache hosted site that attracts 10,000 people every hour.  Lets say with that amount of traffic we get on average 50 new page requests every second and it takes about a second to return each page.  That means Apache has at any given time about 50 open connections.  That&#8217;s what it is designed to do.</p>
<p>But if we now put Comet on that site, all 10,000 of those users need to have a long connection open with our server.  Similar to the first estimate lets say only 50 of them are really receiving any data at any point.  Our Comet server will only assign memory to the tasks of those 50 connections, where Apache would attempt to assign an equal amount of memory to all 10,000 open connections.</p>
<p>That&#8217;s why its important in Comet to separate connections from events, or why we use Orbited (or equivalent comet server) instead of Apache.</p>
<h2>Continuing&#8230;</h2>
<p>That&#8217;s it for my explanations/intro post.  I&#8217;m fairly new to the Comet scene myself and am very busy with my startup.  I&#8217;m sure there are a lot of additions or corrections, please leave them in the comments and I will try to keep up with corrections..</p>
<p>I&#8217;ll put more time in this depending on how much interest there is in the comments and <a href="http://thingsilearned.com/feed/">subscribriptions</a>.  Make sure to check the <a href="http://thingsilearned.com/category/comet/">Comet Category</a> listings for the other posts.</p>
<p>The next post will be an example implementation.</p>
<p>Update:  Continue to the next post in the series: <a href="http://thingsilearned.com/2009/06/25/starting-out-with-comet-orbited-part-2-installation-and-stomp/">Starting Out With Comet (Orbited) Part 2 – Installation and STOMP</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thingsilearned.wordpress.com/492/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thingsilearned.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thingsilearned.wordpress.com/492/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thingsilearned.com&amp;blog=1027874&amp;post=492&amp;subd=thingsilearned&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4b0e75fea564a9693a4592692e6baeb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">godavemon</media:title>
		</media:content>
	</item>
	</channel>
</rss>
