For the Messaging chapter in San Gria, I needed a good example for our Twitter clone, hubbub. The one I ended up with was a simple Jabber gateway.

You can send messages from your jabber client directly to your hubbub account, and they’ll become part of your Post history. Here’s the beast in action. Sent from a Spark jabber client in the foreground to the hubbub webapp running in the backend:

Jabbering into Hubbub

The Jabber gateway listens on the hubbub user’s jabber account (based on some good work on the feedlr blog), slurps off any incoming jabbers, and places them on a JMS queue. In due course, hubbub takes those incoming queue messages and puts them on the Post history of the user with that jabber id in their profile. Eventually hubbub will let you go both ways - jabbering you your friend’s posts as well.

But for all that to work, you need a Grails app to act as the Jabber gateway. Now with a Jabber plugin, that all becomes pretty straightforward. Here’s a 20 line Jabber gateway for your next social networking app which shuffles things between a JMS queue and a Jabber account:

class GatewayService {

    static expose = ['jabber', 'jms']

    static destination = "jabberOutQ"

    def onMessage = { msg ->

        log.info "JMS from: ${msg.userId} to: ${msg.to} content: ${msg.content}"

        def addrs = msg.content.split(",")
        addrs.each { addr ->
            log.debug "Sending to: ${addr}"
            sendJabberMessage(addr, msg.content)
        }

    }

    def onJabberMessage = { jabber ->

        log.info "Jabber from: ${jabber.from} with body ${jabber.body}"
    def jabberClientsDeets = jabber.from.split("/")
        def msg = [ jabberId: jabberClientsDeets[0],
                      content: jabber.body,
              jabberClient: jabberClientsDeets[1] ]
    log.debug "About to send: ${msg.dump()}"
        sendQueueJMSMessage("jabberInQ", msg)

    }

}

I know just about 0 about plugins. I’m going on what I’ve learned from Peter’s chapter on Plugin Development in Grails in Action (which is just a super chapter for you hardcore dudes, btw) - along with the sample code lifted from the uber-cool JMS plugin. The reason it’s not on grails.org yet is that I’m not up to the bit on how to publish plugins yet!

I’m in the process of getting the plugin published on grails.org, but the source is available now on github (along with a Wiki doc describing its use). So feel free to download and do a “grails package-plugin” to get the zip you need. Or just hang out until I learn how to do a publish. ;-)

Happy Jabbering!