xmmp4r and backgroundrb

Hello all,

I am trying to use xmpp4r and backgroundrb for my chat application.
I am able to create a connectnion with XMPP4r through a worker in
I have the problem in returning the object from the worker to my rails

My code is as follows:

class ChatWorker < BackgrounDRb::MetaWorker
  require 'xmpp4r'
  require 'xmpp4r/bytestreams'
  require 'xmpp4r/roster/helper/roster'
  require 'xmpp4r/roster/iq/roster'
  include Jabber
  Jabber::debug = true

  set_worker_name :chat_worker
  def create(args = nil)
    # this method is called, when worker is loaded for the first time
    logger.info 'Initializing Chat Worker.......'
  def connect_chat(args)
    logger.info "Conencting chat.......#{args[:username]} --"
    jid = Jabber::JID.new(args[:username])
    @client = Jabber::Client.new(jid)
    logger.info "Chat Connection status - #{@client.is_connected?}"
    return @client.is_connected?
  def connected?
    return @client.is_connected?
  def get_buddies
    if self.connected?
      @buddy_list = []
      roster = Jabber::Roster::Helper.new(@client)
      mainthread = Thread.current
      roster.add_query_callback { |iq|
      roster.groups.each { |group|
         roster.find_by_group(group).each { |item|
          @buddy_list << item#[{:name => item.iname, :jid => item.jid,
:subscription => item.subscription}]
      logger.info "Buddies\n#{@buddy_list.inspect}"
      return @buddy_list


When i call the connect_chat worker method from console i am able to
connect to the server as below.
MiddleMan.worker(:chat_worker).connect_chat(:arg => {:username =>
'xxx@gmail.com',:password => 'xxxx'})

But when i try to call the get_buddies worker method, i am not able to
get the buddy list as below.
Even in the log file, i printed the @buddy_list object and its showing
the correct result. But it is not returning the result.

The problem is with result returning.

Could you please help me in solving the issues. I am struggling with
this for the whole day.

Thank you for your any kind of suggestions.

Murugaraj B

What is the call returning? Is it nil or an empty array? Not sure if
its causing the issue, but you shouldn't be using instance variables
(@buddy_list vs. buddy_list) as that will propagate from one function
call to the next since I believe that the ChatWorker is only
instantiated once when backgroundrb starts up. Depending on how
you're calling the functions perhaps one following behind is setting
the instance variable back to [] before you read it?

Side note, you're making a socket call to send this chunk of work to
backgroundrb, but you're doing so synchronously; in that case you
would probably just want to do the work in the rails request. I think
perhaps what you mean to do is async_get_buddies, which would offload
the request to backgroundrb and return immediately; then you could
use .ask_result later to get it (more info here: http://backgroundrb.rubyforge.org/rails/).
Depends on your requirements naturally, but thought the link might


Anybody please help me !