Re: HOWTO: Send Instant Messages from Rails

I think maybe active_messaging might do what you want.

It uses apaches activeMQ.

here is a tutorial

http://www.infoq.com/articles/intro-act … ging-rails

it has links to their wiki, and stuff.

I don't know much at all about it, but it might suit your needs.

Re: HOWTO: Send Instant Messages from Rails

I'm getting error while start my webrick server at almost the end of the tutorial:

#./script/server
=> Booting WEBrick...
(drbunix:///tmp/backgroundrbunix_localhost_2000) /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:278:in `load_missing_constant': uninitialized constant AlertWorker (NameError)
    from (drbunix:///tmp/backgroundrbunix_localhost_2000) /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:467:in `const_missing'
    from (drbunix:///tmp/backgroundrbunix_localhost_2000) /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:479:in `const_missing'
    from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:453:in `const_get'
    from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:453:in `worker_klass_constant'
    from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:178:in `new_worker'
    from /home/hliu/Sandbox/dailynews/app/controllers/application.rb:17
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_without_new_constant_marking'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_file'
     ... 31 levels...
    from /usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
    from ./script/server:3


I'm using rails 2.1.0 and ruby 1.8.6

Anyone has any idea about this error? Thank you.

Re: HOWTO: Send Instant Messages from Rails

loop do
  @client.process
  sleep(1)
end

what are these 4 lines supposed to mean? Thank you.

Re: HOWTO: Send Instant Messages from Rails

leomayleomay wrote:

loop do
  @client.process
  sleep(1)
end

what are these 4 lines supposed to mean? Thank you.

will continually loop, calling the method process on @client.

Re: HOWTO: Send Instant Messages from Rails

In my experience, the current XMPP4R has removed the @client.process method and instead expects you to keep a variable with a reference to Thread.current. This keeps the thread alive so XMPP4R can execute the callbacks when information is received.

Most of the XMPP4R examples are still incorrect, but this one shows the idea. You can call thread.wakeup to trigger the end of the app.

http://github.com/ln/xmpp4r/tree/master … erprint.rb

This works for a daemon, but wouldn't work inside a Rails application.

Re: HOWTO: Send Instant Messages from Rails

hey bp, great tutorial..Just what I was looking for. Can we implement the same to mimic gmail chat ??

I mean two users logged into the app and using a web interface to send-receive messages with real-time updation of chat popups at both ends.

Re: HOWTO: Send Instant Messages from Rails

What an excellent tutorial!

I have implemented per the instructions as follows:

#application_controller.rb
  unless MiddleMan[:alerter]
    MiddleMan.new_worker(:class => :alert_worker,:job_key => :alerter)
  end

# alert_worker.rb

require 'XMPP4R'
class AlertWorker < BackgrounDRb::Worker::RailsBase
  set_worker_name :alert_worker
  
  def do_work(args)
    jid = Jabber::JID.new("username@gmail.com")
#    @client = Jabber::Client.new(jid, false)
   @client = Jabber::Client.new(jid)
    @client.connect
    @client.auth("password")
    @client.send(Jabber::Presence.new.set_show(:chat).set_status('BackgrounDRb!'))
    loop do
      @client.process
      sleep(1)
    end
  end

  def send_alert(title)
    to_jid = Jabber::JID.new('username@gmail.com')
    message = Jabber::Message::new(to_jid, title).set_type(:normal).set_id('1')
    @client.send(message)
  end

end
AlertWorker.register

# my controller

  after_create :send_message

  def send_message
    MiddleMan.worker(:alerter).send_alert("hello")
  end

However when it runs I get the following error:

NoMethodError in AssetsController#index

undefined method `[]' for #<BackgrounDRb::ClusterConnection:0x24a0a04>



Would you be so kind as to point me in the right direction?

28

Re: HOWTO: Send Instant Messages from Rails

I'm glad that people still find this interesting.

@venkat1246 In a browser you would have to poll the server for updates.

@ryanshackintosh Sorry, I'm afraid I haven't looked at these libraries in a long time. Good luck!

Re: HOWTO: Send Instant Messages from Rails

Hi BP, I have found some referenced to having to include the line:

include DRbUndumped

But I have no idea where or what it means...

30

Re: HOWTO: Send Instant Messages from Rails

Hi BP,

Thanks for the great tutorial. Like the others, I also found it very useful.
I am a ruby on rails newbie, and I have two follow-up questions that I wonder if you have any thoughts on:

1. Without using backgroundRb, is there anyway to keep the client variable around? Is it possible to make it an instance variable or global variable to keep it alive?
2. You have shown how to send a message via XMPP, but what if you also want to have a ruby on rails app that listens and update itself when it receives a new message?

- Gel

31

Re: HOWTO: Send Instant Messages from Rails

To answer own question #2 above, would I be correct in saying that I should be using a XMPP javascript library to do that instead?

Re: HOWTO: Send Instant Messages from Rails

I just wanted to paste my working code with the latest versions for those of you who may stumble across this article that was written when backgroundRB was of an earlier version:

require 'xmpp4r'


class MessengerWorker < BackgrounDRb::MetaWorker
  
  set_worker_name :messenger_worker

  def create(args = nil)
    # this method is called, when worker is loaded for the first time    
    
    jid = Jabber::JID.new('username@domain.com')
    @client = Jabber::Client.new(jid)
    @client.connect
    @client.auth('password')
    @client.send(Jabber::Presence.new.set_show(:chat).set_status('my status'))
    
    # loop do
    # @client.process
    # sleep(1)
    # end
  end
  
  def do_work
    
  end
  
  def add_buddy(nickname)
    # funtion to add a buddy to your list so you can IM them
    pres = Jabber::Presence.new.set_type(:subscribe).set_to(nickname)
    @client.send(pres)
  end
  
  
  def send_a_message(title)
    # function to send a message
    to_jid = Jabber::JID.new('friend@domain2.com')
    message = Jabber::Message::new(to_jid, title).set_type(:normal).set_id('1')
    @client.send(message)
  end
    
end

Then to add a buddy:

 MiddleMan.worker(:messenger_worker).async_add_buddy(:args => 'buddy@domain2.com')

Then to send an IM

 MiddleMan.worker(:messenger_worker).async_send_a_message(:args => 'hello how are you')

Re: HOWTO: Send Instant Messages from Rails

Gel - why don;t you have a look at http://www.rubyfleebie.com/im_integration_with_zmpp4r ... there is some good information there in the 3 part series too that talk about listening for messages and responding to them.

Re: HOWTO: Send Instant Messages from Rails

I heard about a plugin called SMS_Fu Plugin just google it.

Re: HOWTO: Send Instant Messages from Rails

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 backgrounddrb.
I have the problem in returning the object from the worker to my rails application.

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.......'
  end

  def connect_chat(args)
    logger.info "Conencting chat.......#{args[:username]} --"
    jid = Jabber::JID.new(args[:username])
    @client = Jabber::Client.new(jid)
    @client.connect
    @client.auth(args[:password])
    @client.send(Jabber::Presence.new.set_show(nil).set_status('From Rails!'))
    logger.info "Chat Connection status - #{@client.is_connected?}"
    return @client.is_connected?
  end

  def connected?
    return @client.is_connected?
  end

  def get_buddies
    if self.connected?
      @buddy_list = []
      roster = Jabber::Roster::Helper.new(@client)
      mainthread = Thread.current
      roster.add_query_callback { |iq|
        mainthread.wakeup
      }
      Thread.stop
      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

    end
   
  end


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.
MiddleMan.worker(:chat_worker).get_buddies

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.

Regards
Murugaraj B