Sending and receiving IM messages via controller in Rails
- by Grnbeagle
Hi,
I need a way to handle XMPP communication in my Rails app.
My requirements are:
Keep an instance of XMPP client
running and logged in as one specific
user (my bot user)
Trigger an event from a controller to
send a message and wait for a
reply. The message is sent to another
machine equipped with a bot so that
the reply is supposed to be returned
quickly.
I installed xmpp4r and backgrounDrb similar to what's described here, but backgrounDrb seems to have evolved and I couldn't get it to wait for a reply. If it has to happen asynchronously, I am willing to use a server-push technology to notify the browser when the reply arrives.
To give you a better idea, here are snippets of my code:
(In controller)
class ServicesController < ApplicationController
layout 'simple'
def index
render :text => "index"
end
def show
@my_service = Service.find(params[:id])
worker = MiddleMan.worker(:jabber_agent_worker)
worker.send_request(:arg => {:jid => "someuser@someserver", :cmd => "help"})
render :text => "testing"
end
end
(In worker script)
require 'xmpp4r'
require 'logger'
class JabberAgentWorker < BackgrounDRb::MetaWorker
set_worker_name :jabber_agent_worker
def create(args = nil)
jid = Jabber::JID.new('myagent@myserver')
@client = Jabber::Client.new(jid)
@client.connect
@client.auth('pass')
@client.send(Jabber::Presence.new.set_show(:chat).set_status('BackgrounDRb'))
@client.add_message_callback do |message|
logger.info("**** messaged received: #{message}") # never reaches here
end
end
def send_request(args = nil)
to_jid = Jabber::JID.new(args[:jid])
message = Jabber::Message::new(to_jid, args[:cmd]).set_type(:normal).set_id('1')
@client.send(message)
end
end
If anyone can tell me any of the following, I'd much appreciate it:
issue with my backgrounDrb usage
other background process alternatives appropriate for XMPP interactions
other ways of achieving this
Thanks in advance.