New Cometd client and server demo

On and off for the last couple weeks I have really been pushing myself to learn Java better. I constantly find myself unable to do things I want to with the languages I usually use (PHP specifically). As I am getting better I am coming to like it a lot more. As any reader of my blog (do I actually have ‘readers’?) know, I am really into Cometd, which the most major implementation of is written in Java. So in order to utilize Cometd Java and push my jQuery plugin further along, I figured it was high time I learn Java. I am really glad I am too. Just in time for my new Android phone too :)

CometdMap
Almost every demo of Cometd out there is a chat application, next up is stock tickers. There are one or two more I can think of off hand, none of which could be made into something more useful. I have had an application idea in the back of my head for quick some time. I really like doing work with maps, have been doing professional Google Maps work for a couple years, had to get the (second) dorkiest bike computer with GPS to map my rides. I have wanted for some time to do a real-time map. CometdMap is a proof of concept of that. It has no actually data input at the moment, infact, it is really basic right now. What it really does is show how simply you can build a Cometd Java app. I was really surprised at how easily I built this with my limited knowledge of Java.

What does it do
Right now, if you build and run the application you will get a simple Google Map. Clicking on the map will place a marker. Wowee! Pretty basic yeah, but what is going on here. Clicking the map publishes the point to the server, it is not until the server returns the data to the subscription callback that it is added. That is about all for the client side. On the server side two things are going on. First, when a new client joins they are added to a list, very similar to the way the Cometd Chat demo handles clients for private messages. Second, when points are published they are tracked on another list. So what can we do with these lists? Add a couple points and reload your browser. Since you are now a new client and there are points on the list, the old points are sent back to you. This will work for any new clients joining also. With multiple clients connected markers are updated to each in real-time.

Download & Build
Download
As I have mentioned before, the Java build environment and I do not exactly get along. In the tarball there is a script call build (partially ripped from the Harmony build script). If you have built the Cometd demo from Jetty with Maven this script should work just fine. Otherwise change the paths in the build (and server) scripts to point to the directories where your Jetty and Comet components are.

Enjoy, hopefully everyone is able to build with few problems. Check back in a couple weeks to see what I have in store next. Cheers, Morgan.

7 Responses to “New Cometd client and server demo”

  1. Kevin Peno says:

    I read! :P

    Cool example. I’m working on a very easy online game using a comet server for responses and php for a simple client to client proxy. Once that’s up there’ll be at least 2 apps that aren’t chat apps :P (maybe I’ll through shat in just for fun, lol)

  2. Thanks Kevin, my lone reader.
    Games! I think this is one area where Comet will really start taking over. live.chess.com uses Jetty/Cometd for live moves. I have read about several versions of Poker written. There’s a tic-tac-toe demo out there somewhere, there’s also the Fridge Magnets demo, This is definitely one area where Comet is a must have. With the progress made in Cometd Jetty in recent months it is easier then ever to write back end logic that truly makes Comet useful.
    Make sure you come back and put up a link when you have something to show off.

  3. Kevin Peno says:

    Check it out morgan!

    http://www.metalaxe.com/games/battleship/battlefield.php

    Please note I really through this together and there is no data sanitization. Use with trusted friends. I plan on cleaning it up one day.

    Comet server is Meteor, php is used to simply relay messages to the comet channel. Using json, each player processes the incoming data based on whether it is them or the “enemy”.

  4. Super cool Kevin. Nice example and one of my favorite games.

    Now the challenge is on! Who can take me in battleship? Bring it suckas. hehe. How about Risk next?

  5. Kevin Peno says:

    Risk would be a good adventure in more than 2 player. I think applying the same sort of broadcast principles I applied to this game would make it work much more efficiently.

    One thing I’d like to do first (taking the same approach) is expand this to allow observers and group chat (not to mention allowing direct rematches and stuff). IF that can work well, risk should be cake :P

  6. Matt says:

    Hey Morgan,

    I’m having some trouble modifying the build script for your example. I guess my installation of Jetty is not the same as the default plus I’m using 6.1 instead of 7.0. Can you provide any more insight into what each line is supposed to be doing? Or can you point me to a useful resource for constructing my own build script?

    Thanks!
    Matt

  7. David says:

    Now you have two readers! I’ve been looking over your jquery cometd sample, I d/l jetty and mvn, but for the life of me I cannot get my arms around how I am going to set this up to run as a service. or even how to copy the samples somewhere else to start coding! What I am really trying to do is not so much two way chat but I want to send messages to the clients via json based on mysql database change using php. All the db changes are done in php and javascript so that end is done, I just need to extend your chat.js and figure out the rest!