• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Building an AJAX chat with Parancoe and DWR ...

The AJAX integration through DWR 3 is not the main feature in Parancoe, but it is easy and funny. Let is see how simple is building a Web chat.

Starting from a project generated with Maven using the Parancoe Web Archetype, you need to add the Parancoe DWR Plugin to the dependencies in your pom.xml:


<dependency>
   <groupId>org.parancoe</groupId>
   <artifactId>parancoe-plugin-dwr</artifactId>
   <version>2.0.2</version>
</dependency>

Then add the /dwr/* pattern to the mapping of the parancoe servlet in your web.xml.

Now DWR is ready to be used in your application. You check it building and deploying your application, and pointing your browser to <your_base_address>/dwr/index.html. You should see the DWR Test Page.

Then add a class with methods (a single method in this example) that will be invoked through AJAX:


package com.mycompany.testapp.ajax;

import org.directwebremoting.Browser;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.ui.dwr.Util;
import org.springframework.stereotype.Component;

@Component
@RemoteProxy(name = "chat")
public class Chat {

   @RemoteMethod
   public void send(final String nickname, final String message) {
       Browser.withCurrentPage(new Runnable() {
           public void run() {
               Util.addRows("chatLog", new String[][]{{nickname, message}});
           }
       });
   }
}

 

Simple, isn it? ...but powerful. When invoked that method will add a row to the table with id chatLog, in all browsers that are visualizing that page.

Let is see the page chat.jsp in which that method is invoked:


<%@ include file="WEB-INF/jsp/common.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
   <head>
       <%@ include file="WEB-INF/jsp/head.jspf" %>
       <script src="${cp}/dwr/interface/chat.js" type="text/javascript"/>
       <script src="${cp}/dwr/engine.js" type="text/javascript" />
       <script src="${cp}/dwr/util.js" type="text/javascript" />
       <title>Parancoe DWR chat</title>
   </head>
   <body>
       <h1>DWR Chat</h1>
     
       <table id="chatLog">
           <thead>
               <tr><th>Nickname</th><th>Message</th></tr>
           </thead>
           <tbody> </tbody>
       </table>
     
       <form id="chatForm" method="post" action="/sendMessage">
           Nickname: <input id="nickname" type="text" /><br/>
           Say: <input id="say" type="text" /><br/>
           <input type="submit">
       </form>
             
       <script type="text/javascript">
           function bindUI() {
               $(chatForm).observe( isubmit, chatSubmit);
           }
 
           function chatSubmit(event) {
               event.stop();
               chat.send($F( ickname), $F( isay));
           }
 
           dwr.engine.setActiveReverseAjax(true);
           document.observe( wouldom:loaded, bindUI);
       </script>
   </body>
</html>

In your parancoe-servlet.xml file add the following configuration for enabling the active reverse AJAX in DWR:  


Build and deploy your application. Open multiple windows of your browser(s) to <your_base_address>/chat.jsp, and start chatting:

 Original Source:
http://benfante.blogspot.com

AddThis Social Bookmark Button

Posted at 11:35:21 am | Permalink | Posted in Chat  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange