Introducing the em-xmpp gem

I found this gem on rubygems.org, it uses EventMachine and it works with Ruby 2.0.

In the example below the XMMP client sends a message to an online user. It then listens for the response from the user as well as any changes to presence updates.

require 'em-xmpp'

jid = 'jeeves_mobile@jamesrobertson.eu'
pass = 'secretpassword'
Thread.new do
  EM.run do
    conn = EM::Xmpp::Connection.start(jid, pass)
  end
end


data = conn.message_stanza('to' => 'james@jamesrobertson.eu') do |xml| 
         xml.body('hello world')
       end
conn.send_stanza data

Looking for r

eceived messages and presence updates.

conn.on_message do |ctx|
  ctx.with(:message) # extends with EM::Xmpp::Context::Contexts::Message
  ctx.with(:mood)    #                                           Mood
  #then lets you write:
  puts ctx.from
  puts ctx.body
  puts ctx.mood
  ctx 
end

observe

d:

james@jamesrobertson.eu/niko
hello
conn.on_presence do |ctx|
   presence = ctx.bit(:presence)
  puts presence.from
  puts 'status : ' + presence.status
  ctx #unmodified context
end

observed:

james@jamesrobertson.eu/niko
status : I'm not here right now.
Tags:
Source:
2113hrs.txt
Published:
01-03-2013 21:13