Google Android chat using Jetty Cometd java client
I have ported the cometd java client from Jetty to run on the google android phone. Cometd implements the bayeux protocol for push messaging over HTTP and is normally used by Ajax clients running in the browser. By porting the java client to android, this allows native applications on the phone to have bidirectional messaging to a server over HTTP.
To demonstrate this, I've implemented a simple chat room that interacts with the Ajax Comet chat room demos. This would be a great basis for other android applications that need to update in realtime.
The following core dependencies were used:
- jetty-client-6.1.12.rc2 (asynchronous http client)
- bayeux-client-6.1.12.rc2 (bayeux protocol via http)
- android-1.0_r1
I had been wrestling with android for a couple of days trying to get familiar with their apis. The more you dive into it, you would notice a pattern that you might be used to, which is developing webapps using the MVC pattern .
View = xml resources (like html .. static rendering)
Activity = Controller (logic)
To get started, you can use the ADT (Android Developer Tools) eclipse plugin. Although it could speed up your productivity, it would can up your cpu and memory as well. It takes a few seconds to recompile(annoying) even with just the slightest change in code.
The import part of the plugin is the auto-update of the R.class of your application (after adding/editing/removing your views and other xml resources).
Implementing the chat client using cometd-jetty client was a breeze because all the work (bayex protocol) was already done by org.mortbay.cometd.client.BayeuxClient.
The one problem I've encountered was updating android's UI from another thread (e.g message received event). If I had read all the android FAQs, I wouldn't have been stuck on it. Oh well ...so the solution was simply creating an android.os.Handler inside your Activity to be able to receive asynchronous events (dispatched from another thread) and to be able to render/update your UI.
public class MyActivity extends Activity
{
public static final int SOME_EVENT = 10;
Handler _handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch(msg.what)
{
case SOME_EVENT:
// update your ui here
break;
}
}
}
Runnable _someEventHandler = new Runnable()
{
public void run()
{
_handler.sendEmptyMessage(SOME_EVENT);
}
}
.....
}
The source and apk is available here.
To chat live (after installation), change your connection settings:
host: cometdchat.morphexchange.com
port: 80
Enjoy!
Posted at 08:27PM Oct 15, 2008 by dyu in General | Comments[20]
Posted by Meirbek (Mike) on November 01, 2008 at 07:11 AM PHT #
1. I tried running the MVN build, but got and error "Patch command failed (exit value != 0) for cookie.patch" (full stacktrace is here: http://pastebin.com/m5868c281 )
2. I instead tried setting the project up in Eclipse (using the Android plugin) and it compiles/runs, but get an error about a missing org/mortbay/jetty/mime resource bundle (full stacktrace here: http://pastebin.com/m1568ed4 )
Help? I'd love to see this in action.
Posted by Ben Krembs on November 12, 2008 at 12:09 PM PHT #
I ran the MVN exactly as is and got the following exception when I clicked "Join Chat Room"
W/dalvikvm( 237): Link of class 'Lorg/mortbay/cometd/client/BayeuxClient;' failed I/dalvikvm( 237): Failed resolving Lorg/mortbay/cometd/client/BayeuxClient; interface 300 'Lorg/cometd/Client;' W/dalvikvm( 237): Link of class 'Lorg/mortbay/cometd/client/BayeuxClient;' failed W/dalvikvm( 237): VFY: unable to resolve virtual method 1717: Lorg/mortbay/cometd/client/BayeuxClient;.publish (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V W/dalvikvm( 237): VFY: rejecting opcode 0x6e at 0x0025 W/dalvikvm( 237): VFY: rejected Lorg/mortbay/demos/android/cometdchat/util/ChatRoomClient;.chat (Ljava/lang/String;)Z W/dalvikvm( 237): Verifier rejected class Lorg/mortbay/demos/android/cometdchat/util/ChatRoomClient;
Your provided .apk worked, however.
Posted by Laura on November 17, 2008 at 01:52 AM PHT #
Posted by dyu on December 03, 2008 at 02:36 PM PHT #
Posted by dyu on December 03, 2008 at 02:44 PM PHT #
Posted by chipsyKing on January 08, 2009 at 11:34 PM PHT #
Posted by Jerry on April 26, 2009 at 07:21 PM PHT #
Posted by dyu on April 27, 2009 at 11:16 AM PHT #
Posted by Kevin on April 30, 2009 at 02:27 PM PHT #
HTTP ERROR 404
\nProblem accessing /cometd/handshake. Reason:\n
Powered by Jetty://
\n
\n
Posted by Jerry on April 30, 2009 at 02:42 PM PHT #
Posted by fatemeh on May 28, 2009 at 12:33 PM PHT #
I used the same jetty cometd demo you used. No custom code on the server side.
@Jerry
You need to fire up the jetty cometd demo
Basically grab a jetty distro in http://dist.codehaus.org/jetty/
After you extract it the bundle, start jetty with $ java -jar start.jar
@fatemeh
You mean CometdChatApplication.java?
Posted by dyu on May 28, 2009 at 03:29 PM PHT #
Posted by fatemeh on May 28, 2009 at 04:41 PM PHT #
You need to import this project to eclipse using the android eclipse plugin and run it using the same plugin. E.g right click the AndroidManifest and click 'run'
Posted by dyu on May 28, 2009 at 05:16 PM PHT #
Posted by fatemeh on May 29, 2009 at 05:19 PM PHT #
Posted by fatemeh on June 03, 2009 at 07:33 PM PHT #
I don't see anywhere in the code where httpClient.getAttribute is used.
Posted by dyu on June 04, 2009 at 10:13 AM PHT #
Posted by fatemeh on June 07, 2009 at 07:06 PM PHT #
Posted by fatemeh on June 08, 2009 at 05:15 PM PHT #
Posted by fatemeh on June 08, 2009 at 05:47 PM PHT #