Using RemoveAllRows function with DIV based pattern

For quite long time we were not able to use DIV based HTML in our DWR based applications.

DWR supports only tables to remove and add rows for any data. Now if your HTML contains DIV based pattern you can not clear and re-populate rows, you need to convert the design to TABLE.

I have modified removeAllRows function frovided by util.js and achieved same for DIV
 
dwr.util.removeAllRowsDiv = function(ele,options) {
return;
ele = document.getElementById(ele);
if (!options) options = {};
if (!options.filter) options.filter = function() { return true; };
if (ele == null) return;

var child = ele.firstChild;
var next;
while (child != null) {
next = child.nextSibling;
if (options.filter(child)) {
ele.removeChild(child);
}
child = next;
}
};


So Now by providing your parrent DIV id you can and pattern div Id you can acheive the same result.

Secrets of implementing a Gtalk Bot

I have read an article on implementing a Gtalk bot. I found it intresting so, I thought to write the concepts behind Google Talk and implementing your own Google Talk Bot.

Google Talk (GTalk) is a free Windows and web-based instant messaging application offered by Google Inc .

Instant messaging between the Google Talk servers and its clients uses an open protocol, XMPP, Protocol.

XMPP (Extensible Messaging and Presence Protocol) is an open technology for instant messaging. The core technology behind XMPP was invented by Jeremie Miller in 1998, refined in the Jabber open-source community in 1999 and 2000, and formalized by the IETF in 2002 and 2003, resulting in publication of the XMPP RFCs in 200.

Google talk uses XMPP for authentication, presence and messaging so any client that supports XMPP can connect to the Google Talk service . Google Talk seervice is hosted at talk.google.com on 5222 port.

To write a Google Talk bot you need to implement XMPP client API of your choice in your choice of platform. For example SMACK is java XMPP client API.

Steps required to write a Gtalk Bot:

1. Download SMACK client API from
2. Extract smack.jar, smackx.jar, smackx-debug.jar
3. Write Gtalk Service Code.

Sending and Receiving Message using SMACK:
  

public class MyGtalkClient implements MessageListener {

public void processMessage(Chat chat,Message message) {
/*Callback method from MessageListener interface .
It is called when a message is received */

System.out.println("Received message: " + message.getBody());
}

public static void main(String [] args) throws XMPPException,IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

/*Login to GTalk service*/
ConnectionConfiguration config = new ConnectionConfiguration (
"talk.google.com",
5222,
"gmail.com");
XMPPConnection connection = new XMPPConnection(config);

connection.connect(); /* Connect to the XMPP server */

connection.login("gtalkid","gtalk pass");
/*Enter your username & password to login to the gtalk service */
Chat chat = connection.getChatManager().createChat(
your_friend_id",new MyGtalkClient());

System.out.println(" ****Welcome to MyGtalkClient**** ");
System.out.println("****Enter your message, one per line ."+
"To stop chat enter stop****");

while( !(msg=br.readLine()).equals("stop")) {
chat.sendMessage(msg); //Send the message
}

connection.disconnect() ; //Disconnect
}
}


This is a very basic example here your buddy is hard coded and you are initiating the chat.
I will write implementing a calculator bot in next part which will provide concepts of listening for a packet, process that packet and send result back to that user.

Reverse Ajax with Spring and DWR


Ajax is used heavily for web 2.0 applications to provide enhance user experience, which allows a web page to be updated without the need of full refresh. Normally this updated information is pulled from client side.
Javascript call is used to make an Ajax call to the server and received information is updated.
But there are scenarios when server decides to update information on client side e.g. Chat applications, server side processing status, user presence etc.


In these situations server side "PUSH" is required instead of Client side "PULL". There are three techniques used for this purpose:
  • Polling (the browser sends requests at regular interval)
  • Comet (long lived HTTP connection connection is kept open and server keeps replying)
  • Piggyback (server waits for client to make request and sends update with that request data)
Configuration:
1. Enable Reverse Ajax in DWR configuration
There are two ways of going this:

You can enable in DWR.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>

or if you are using DWR name spaces then you can provide entry in [Servlet Name]-servelt.xml
<dwr:controller id="dwrController" debug="true">
<dwr:config-param name="activeReverseAjaxEnabled" value="true" />
</dwr:controller>

2. Enable Reverse Ajax in JSP
dwr.engine.setActiveReverseAjax(true);

3. Now we need to get Browser sessions from server side and push content there:

WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();

Get current page from webContext

Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Get all other clients session

After getting these sessions you can push data from here using proxy interface
Util utilAll = new Util(sessions);
utilAll.addOptions("chatlog", messages, "text");

Above lines will add messages collection to all the browsers under "chatlog" element

Similarly you can execute scripts on browsers from server side.

ScriptBuffer script = new ScriptBuffer();
script.appendScript("receiveMessages(")
.appendData(messages)
.appendScript(");");

Above snippet will execute receiveMessages() javascript function on client side.

This way you can have full control on client side from server side. Chat application, Stock Ticker control applications, mail client can use this feature effectively.