Jabber::Simple [
src,
doc] is a simple (duh!) Ruby library that aims to make the implementation of basic Jabber functionality trivial. It is an extraction of the Jabber support that was added to
Twitter, and is released under the GPL by
Obvious. A line of code is worth a thousand words, so here is the complete code for sending a simple message to a Jabber user:
jabber = Jabber::Simple.new('rex@friendosaurus.com', 'password')
jabber.deliver("bront@friendosaurus.com", "Hey! I'm thinking of going Vegetarian - Any suggestions?")
Getting incoming messages is just as easy:
jabber.received_messages do |msg|
puts "#{msg.body}" if msg.type == :chat
end
You can also set your status, and get information about your friends' statuses:
jabber.status(:away, "Eating at the Tree Cafe. I need a ladder.")
jabber.presence_updates do |update|
friend = update[0]
presence = update[2]
puts "#{friend.jid} is #{presence.status}"
end
Installation
sudo gem install xmpp4r-simple
or download the package from
RubyForge. Source code is also
available, licensed under the GPLv2.
Yet Another Jabber Library?
There are a number of existing Jabber libraries for Ruby (
jabber4r,
xmpp4r, and
Net::XMPP), so why Jabber::Simple?
First off, Jabber::Simple does not aim to replicate any core XMPP protocol functionality present in these libraries — in fact, Jabber::Simple depends on
xmpp4r and the
Jabber::Simple#client and
Jabber::Simple#roster methods expose all of xmpp4r's awesome functionality.
When I started building in Jabber support for
Twitter I'd used
various Jabber clients, and even set up a simple Jabber server. Writing my own client, however, was a bit more complex. It turns out that the seamless experience of "adding a friend" and chatting with them is (unsurprisingly) comprised of a series of disjoint steps, and fraught with the peril of threads, XML streams, and arcane magic. The available libraries handle these tasks and many more admirably, but lack in elegance. My hope is that Jabber::Simple provides a sufficiently
obvious interface with which to develop tools that use the Jabber protocol.
But Wait! There's More!
Now, you might shy away from writing that really cool chat-bot you've been meaning to write, saying "Wow, this is great, but setting up a Jabber server is a pain." --- but fear not! Go over to
Google Talk and sign up for an account. Once you're done, use your Google Talk username and password, and start Jabbering. No really, it's that simple.
jabber = Jabber::Simple.new("you@gmail.com", "password")
Labels: code, jabber, ruby, xmpp