<?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; ajax</title> <atom:link href="http://martin.ankerl.com/tag/ajax/feed/" rel="self" type="application/rss+xml" /><link>http://martin.ankerl.com</link> <description>Chunky bacon!!</description> <lastBuildDate>Sat, 04 Feb 2012 09:34:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <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>martinus</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 &#8230; <a href="http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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?9d7bd4" 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 class="brush: xml; title: ; notranslate">&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 class="brush: jscript; title: ; notranslate">&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;
      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);
      }
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;input type=&quot;button&quot;
       onclick=&quot;&lt;b&gt;cometd.publish('/hello/world', { test: 'hello world' } )&lt;/b&gt;&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 class="brush: xml; title: ; notranslate">&lt;script type=&quot;text/javascript&quot; src=&quot;../dojo.js&quot;&gt;&lt;/script&gt;</pre><p> you integrate the dojo toolkit into the project.</p><li>To activate the cometd class of dojo:<pre class="brush: jscript; title: ; notranslate">dojo.require(&quot;dojo.io.cometd&quot;);</pre><li>Connect the server with the client:<pre class="brush: jscript; title: ; notranslate">cometd.init({}, &quot;cometd&quot;);</pre><li>Here we say what to do when there is a subscribe event:<pre class="brush: jscript; title: ; notranslate">cometd.subscribe(&quot;/hello/world&quot;, false, &quot;publishHandler&quot;);</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 class="brush: jscript; title: ; notranslate">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?9d7bd4" width="324" height="124" /></center></p><p> The reason for this is that when you click the code<pre class="brush: jscript; title: ; notranslate">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 class="brush: xml; title: ; notranslate">&lt;%@page import=&quot;java.util.*&quot;%&gt;
&lt;%@page import=&quot;dojox.cometd.*&quot; %&gt;
&lt;%
Bayeux b = (Bayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
Channel c = b.getChannel(&quot;/hello/world&quot;,false);

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

c.publish(b.newClient(&quot;server_user&quot;,null),message, &quot;new server message&quot;);
%&gt;</pre><p>When this page is loaded, an alert popup appears at the page saying <i>jsp: hello world</i>.</p><p>Remember pushing too much data from server to client can slow the performance of your site specially is you have a huge amount of traffic. Getting one of the <a href="http://www.webhostingsearch.com/wordpress-hosting.php" title="best wordpress hosting">best wordpress hosting</a> will solve this kind of problem.</p><p>That&#8217;s it. Happy hacking!</p><div style='clear:both'></div>]]></content:encoded> <wfw:commentRss>http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/feed/</wfw:commentRss> <slash:comments>30</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/5 queries in 0.003 seconds using disk: basic
Object Caching 421/421 objects using disk: basic

Served from: martin.ankerl.com @ 2012-02-04 10:36:28 -->
