<?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/"
	>

<channel>
	<title>Martin Ankerl &#187; dojo</title>
	<atom:link href="http://martin.ankerl.com/tag/dojo/feed/" rel="self" type="application/rss+xml" />
	<link>http://martin.ankerl.com</link>
	<description>No movement is faster than no movement</description>
	<lastBuildDate>Tue, 13 Jul 2010 05:31:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Ajax Dojo Comet Tutorial</title>
		<link>http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/</link>
		<comments>http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 12:41:22 +0000</pubDate>
		<dc:creator>Martin Ankerl</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[videos]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://martin.ankerl.com/?p=92</guid>
		<description><![CDATA[EDIT: This tutorial is for an old version of dojo / comet, and it will not work in a recent version! Markus Holzmann, an intern at Profactor of my fellow colleague Philipp Hartl, had the opportunity to experiment with Ajax during his job. He wrote a tutorial about how to push events from the server [...]]]></description>
			<content:encoded><![CDATA[<p><strong>EDIT</strong>: This tutorial is for an old version of dojo / comet, and it will not work in a recent version!</p>
<p>Markus Holzmann, an intern at <a href="http://www.profactor.at/">Profactor</a> of my fellow colleague <a href="http://leanaustria.net/">Philipp Hartl</a>, had the opportunity to experiment with <a href="http://en.wikipedia.org/wiki/Ajax_(programming)">Ajax</a> during his job. He wrote a tutorial about how to push events from the server to the client. For example, display popup messages on all browsers at the same time (see screencast in <a href="/files/hello_comet.html" target="_blank">full resolution here</a>):<br />
<center><br />
<a href="/files/hello_comet.html" target="_blank">  <object width="400" height="317"><param name="wmode" value="transparent"></param>
    <embed src="/files/Hello_Comet.swf" wmode="transparent" width="401" height="317" type="application/x-shockwave-flash"></embed></object><br />
</a><br />
</center><br />
Read on how Markus did this:</p>
<p><span id="more-92"></span></p>
<h1>Cometd Hello World</h1>
<p>
I&#8217;ve read Chris Bucchere&#8217;s <a href="http://thebdgway.blogspot.com/2006/11/say-hello-world-to-comet.html">Say Hello World to Comet</a> and built an application based on this using a more current version of <a href="http://www.mortbay.org/">Jetty</a> (version <a href="http://dist.codehaus.org/jetty/jetty-6.1.5/">6.1.5</a>) which I embedded into a <a href="http://tomcat.apache.org/">Tomcat</a> v5.5 Server. For the developing I used <a href="http://www.eclipse.org/">Eclipse</a> 3.2.</p>
<p><h1>Start your Engines</h1>
<p>At first you have to get the server running. As I mentioned I embedded Jetty into a Tomcat server. Therefore you have configure the libraries:</p>
<ol>
<li>Add the packages <tt>org.mortbay.cometd</tt> and <tt>dojox.cometd</tt> to your source folder and delete the <tt>client</tt> package in the <tt>org.mortbay.cometd</tt> package.</li>
<li>Add <tt>jetty-util-6.1.5.jar</tt>, <tt>jetty-6.1.5.jar</tt> and <tt>servlet-api-2.5-6.1.5.jar</tt> to your build path.</li>
<li>Copy the <tt>jetty-util-6.1.5.jar</tt> file into the <tt>/lib</tt> folder in the <tt>WEB-INF</tt> directory.</li>
</ol>
<p>Replace the existing servlets in your web.xml &#8211; file in the WEB-INF &#8211; folder with the following servlets:</p>
<pre>&lt;servlet&gt;
  &lt;servlet-name&gt;cometd&lt;/servlet-name&gt;
  &lt;servlet-class&gt;org.mortbay.cometd.continuation.ContinuationCometdServlet&lt;/servlet-class&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;cometd&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/cometd/*&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;</pre>
<p>For the project I used the <a href="http://dojotoolkit.org/">dojo toolkit</a> (version 0.4.3) which has an integrated <a href="http://www.cometd.com/">COMETd</a> class that makes it easy to build comet projects. <a href="http://download.dojotoolkit.org/release-0.4.3/dojo-0.4.3-ajax.tar.gz">Download it</a> and add it to your <tt>WebContent</tt> folder.</p>
<p>
When you&#8217;ve done all this, the hardest piece of work for this program is already done.</p>
<h1>Hack the Code</h1>
<p>Now you can implement the code for the client side: You need a HTML file with a button on it. The code for this looks like this (<a href="/files/hello_comet_test.html">download</a>):</p>
<pre>&lt;html&gt;
  &lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;../dojo.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;<b>
      dojo.require(&quot;dojo.io.cometd&quot;);

      cometd.init({}, &quot;cometd&quot;);

      cometd.subscribe(&quot;/hello/world&quot;, false, &quot;publishHandler&quot;);

      publishHandler = function(msg) {
        alert(msg.data.test);
      }</b>
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;input type=&quot;button&quot;
       onclick=&quot;<b>cometd.publish('/hello/world', { test: 'hello world' } )</b>&quot;
       value=&quot;Click Me!&quot;&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Line by line, the above bold code works like this:</p>
<ol>
<li>In the line
<pre>&lt;script type="text/javascript" src="../dojo.js"&gt;&lt;/script&gt;</pre>
<p> you integrate the dojo toolkit into the project.</p>
<li>To activate the cometd class of dojo:
<pre>dojo.require("dojo.io.cometd");</pre>
<li>Connect the server with the client:
<pre>cometd.init({}, "cometd");</pre>
<li>Here we say what to do when there is a subscribe event:
<pre>cometd.subscribe("/hello/world", false, "publishHandler");</pre>
<li>Last but not least, the <tt>publishHandler</tt> function serves as the callback function, which uses <tt>alert</tt> to show a simple message box:
<pre>publishHandler = function(msg) {
  alert(msg.data.test);
}</pre>
</ol>
<h1>Give it a Try</h1>
<p>When you load the HTML file now, you can click on the button and an alert box saying <i>hello world</i> will appear:</p>
<p>
<center><img src="/files/helloworld.png" width="324" height="124" /></center></p>
<p>
The reason for this is that when you click the code
<pre>cometd.publish('/hello/world', { test: 'hello world' } )</pre>
<p> is executed which publishes a text on the channel with the id <tt>/hello/world</tt>.</p>
<p>
The funny thing is that this is able to run on any number of browsers. Everytime when a client clicks the button, on <i>all</i> browsers that view this page the alert box is shown. (See screencast above).</p>
<h1>Pushing Data from Server to Client</h1>
<p>You can also add serverside code to trigger an event. I wrote a JSP file with the following code:</p>
<pre>&lt;%@page import="java.util.*"%&gt;
&lt;%@page import="dojox.cometd.*" %&gt;
&lt;%
Bayeux b = (Bayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
Channel c = b.getChannel("/hello/world",false);

Map&lt;String,Object&gt; message = new HashMap&lt;String,Object&gt;();
message.put("test", "jsp: hello world");

c.publish(b.newClient("server_user",null),message, "new server message");
%&gt;</pre>
<p>When this page is loaded, an alert popup appears at the page saying <i>jsp: hello world</i>.</p>
<p>That&#8217;s it. Happy hacking!</p>
<div style='clear:both'></div><img src="http://martin.ankerl.com/?ak_action=api_record_view&id=92&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
