<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - HOWTO: Send Instant Messages from Rails]]></title>
		<link>http://railsforum.com/viewtopic.php?id=1033</link>
		<description><![CDATA[The most recent posts in HOWTO: Send Instant Messages from Rails.]]></description>
		<lastBuildDate>Wed, 23 Dec 2009 04:55:21 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=115253#p115253</link>
			<description><![CDATA[<p>Hello all,</p><p>I am trying to use xmpp4r and backgroundrb for my chat application.<br />I am able to create a connectnion with XMPP4r through a worker in backgrounddrb.<br />I have the problem in returning the object from the worker to my rails application.</p><p>My code is as follows:</p><p>class ChatWorker &lt; BackgrounDRb::MetaWorker<br />&nbsp; require &#039;xmpp4r&#039;<br />&nbsp; require &#039;xmpp4r/bytestreams&#039;<br />&nbsp; require &#039;xmpp4r/roster/helper/roster&#039;<br />&nbsp; require &#039;xmpp4r/roster/iq/roster&#039;<br />&nbsp; include Jabber<br />&nbsp; Jabber::debug = true<br />&nbsp; <br />&nbsp; set_worker_name :chat_worker</p><p>&nbsp; def create(args = nil)<br />&nbsp; &nbsp; # this method is called, when worker is loaded for the first time<br />&nbsp; &nbsp; logger.info &#039;Initializing Chat Worker.......&#039;<br />&nbsp; end</p><p>&nbsp; def connect_chat(args)<br />&nbsp; &nbsp; logger.info &quot;Conencting chat.......#{args[:username]} --&quot;<br />&nbsp; &nbsp; jid = Jabber::JID.new(args[:username])<br />&nbsp; &nbsp; @client = Jabber::Client.new(jid)<br />&nbsp; &nbsp; @client.connect<br />&nbsp; &nbsp; @client.auth(args[:password])<br />&nbsp; &nbsp; @client.send(Jabber::Presence.new.set_show(nil).set_status(&#039;From Rails!&#039;))<br />&nbsp; &nbsp; logger.info &quot;Chat Connection status - #{@client.is_connected?}&quot;<br />&nbsp; &nbsp; return @client.is_connected?<br />&nbsp; end</p><p>&nbsp; def connected?<br />&nbsp; &nbsp; return @client.is_connected?<br />&nbsp; end</p><p>&nbsp; def get_buddies<br />&nbsp; &nbsp; if self.connected?<br />&nbsp; &nbsp; &nbsp; @buddy_list = []<br />&nbsp; &nbsp; &nbsp; roster = Jabber::Roster::Helper.new(@client)<br />&nbsp; &nbsp; &nbsp; mainthread = Thread.current<br />&nbsp; &nbsp; &nbsp; roster.add_query_callback { |iq|<br />&nbsp; &nbsp; &nbsp; &nbsp; mainthread.wakeup<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; Thread.stop<br />&nbsp; &nbsp; &nbsp; roster.groups.each { |group|<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;roster.find_by_group(group).each { |item|<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @buddy_list &lt;&lt; item#[{:name =&gt; item.iname, :jid =&gt; item.jid, :subscription =&gt; item.subscription}]<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; logger.info &quot;Buddies\n#{@buddy_list.inspect}&quot;<br />&nbsp; &nbsp; &nbsp; return @buddy_list</p><p>&nbsp; &nbsp; end<br />&nbsp; &nbsp; <br />&nbsp; end</p><br /><p>When i call the connect_chat worker method from console i am able to connect to the server as below.<br />MiddleMan.worker(:chat_worker).connect_chat(:arg =&gt; {:username =&gt; &#039;xxx@gmail.com&#039;,:password =&gt; &#039;xxxx&#039;})</p><p>But when i try to call the get_buddies worker method, i am not able to get the buddy list.<br />MiddleMan.worker(:chat_worker).get_buddies</p><p>Even in the log file, i printed the @buddy_list object and its showing the correct result. But it is not returning the result.</p><p>The problem is with result returning.</p><p>Could you please help me in solving the issues. I am struggling with this for the whole day.</p><p>Thank you for your any kind of suggestions.</p><p>Regards<br />Murugaraj B</p>]]></description>
			<author><![CDATA[dummy@example.com (murraj_dckap)]]></author>
			<pubDate>Wed, 23 Dec 2009 04:55:21 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=115253#p115253</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=114284#p114284</link>
			<description><![CDATA[<p>I heard about a plugin called SMS_Fu Plugin just google it.</p>]]></description>
			<author><![CDATA[dummy@example.com (jomillergo)]]></author>
			<pubDate>Wed, 09 Dec 2009 05:07:16 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=114284#p114284</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113678#p113678</link>
			<description><![CDATA[<p>Gel - why don;t you have a look at <a href="http://www.rubyfleebie.com/im_integration_with_zmpp4r">http://www.rubyfleebie.com/im_integration_with_zmpp4r</a> ... there is some good information there in the 3 part series too that talk about listening for messages and responding to them.</p>]]></description>
			<author><![CDATA[dummy@example.com (ryanshackintosh)]]></author>
			<pubDate>Tue, 01 Dec 2009 07:44:26 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113678#p113678</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113677#p113677</link>
			<description><![CDATA[<p>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:</p><div class="codebox"><pre><code>require &#039;xmpp4r&#039;


class MessengerWorker &lt; 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(&#039;username@domain.com&#039;)
    @client = Jabber::Client.new(jid)
    @client.connect
    @client.auth(&#039;password&#039;)
    @client.send(Jabber::Presence.new.set_show(:chat).set_status(&#039;my status&#039;))
    
    # 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(&#039;friend@domain2.com&#039;)
    message = Jabber::Message::new(to_jid, title).set_type(:normal).set_id(&#039;1&#039;)
    @client.send(message)
  end
    
end</code></pre></div><p>Then to add a buddy:</p><div class="codebox"><pre><code> MiddleMan.worker(:messenger_worker).async_add_buddy(:args =&gt; &#039;buddy@domain2.com&#039;)</code></pre></div><p>Then to send an IM<br /></p><div class="codebox"><pre><code> MiddleMan.worker(:messenger_worker).async_send_a_message(:args =&gt; &#039;hello how are you&#039;)</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (ryanshackintosh)]]></author>
			<pubDate>Tue, 01 Dec 2009 07:42:49 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113677#p113677</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113549#p113549</link>
			<description><![CDATA[<p>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?</p>]]></description>
			<author><![CDATA[dummy@example.com (Gel)]]></author>
			<pubDate>Sat, 28 Nov 2009 18:54:52 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113549#p113549</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113548#p113548</link>
			<description><![CDATA[<p>Hi BP,</p><p>Thanks for the great tutorial. Like the others, I also found it very useful.<br />I am a ruby on rails newbie, and I have two follow-up questions that I wonder if you have any thoughts on:</p><p>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?<br />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?</p><p>- Gel</p>]]></description>
			<author><![CDATA[dummy@example.com (Gel)]]></author>
			<pubDate>Sat, 28 Nov 2009 18:17:55 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113548#p113548</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113262#p113262</link>
			<description><![CDATA[<p>Hi BP, I have found some referenced to having to include the line:</p><p>include DRbUndumped</p><p>But I have no idea where or what it means...</p>]]></description>
			<author><![CDATA[dummy@example.com (ryanshackintosh)]]></author>
			<pubDate>Wed, 25 Nov 2009 10:56:56 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113262#p113262</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113248#p113248</link>
			<description><![CDATA[<p>I&#039;m glad that people still find this interesting.</p><p>@venkat1246 In a browser you would have to poll the server for updates.</p><p>@ryanshackintosh Sorry, I&#039;m afraid I haven&#039;t looked at these libraries in a long time. Good luck!</p>]]></description>
			<author><![CDATA[dummy@example.com (bp)]]></author>
			<pubDate>Wed, 25 Nov 2009 05:42:36 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113248#p113248</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=113241#p113241</link>
			<description><![CDATA[<p>What an excellent tutorial!</p><p>I have implemented per the instructions as follows:</p><div class="codebox"><pre><code>#application_controller.rb
  unless MiddleMan[:alerter]
    MiddleMan.new_worker(:class =&gt; :alert_worker,:job_key =&gt; :alerter)
  end

# alert_worker.rb

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

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

end
AlertWorker.register

# my controller

  after_create :send_message

  def send_message
    MiddleMan.worker(:alerter).send_alert(&quot;hello&quot;)
  end</code></pre></div><p>However when it runs I get the following error:</p><p> NoMethodError in AssetsController#index</p><p>undefined method `[]&#039; for #&lt;BackgrounDRb::ClusterConnection:0x24a0a04&gt;</p><br /><br /><p>Would you be so kind as to point me in the right direction?</p>]]></description>
			<author><![CDATA[dummy@example.com (ryanshackintosh)]]></author>
			<pubDate>Wed, 25 Nov 2009 02:58:52 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=113241#p113241</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=84547#p84547</link>
			<description><![CDATA[<p>hey bp, great tutorial..Just what I was looking for. Can we implement the same to mimic gmail chat ?? </p><p>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.</p>]]></description>
			<author><![CDATA[dummy@example.com (venkat1246)]]></author>
			<pubDate>Thu, 08 Jan 2009 11:23:24 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=84547#p84547</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=77280#p77280</link>
			<description><![CDATA[<p>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.</p><p>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.</p><p><a href="http://github.com/ln/xmpp4r/tree/master/data/doc/xmpp4r/examples/basic/rosterprint.rb">http://github.com/ln/xmpp4r/tree/master &#133; erprint.rb</a></p><p>This works for a daemon, but wouldn&#039;t work inside a Rails application.</p>]]></description>
			<author><![CDATA[dummy@example.com (topfunky)]]></author>
			<pubDate>Thu, 09 Oct 2008 18:56:58 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=77280#p77280</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=71297#p71297</link>
			<description><![CDATA[<div class="quotebox"><cite>leomayleomay wrote:</cite><blockquote><p>loop do<br />&nbsp; @client.process<br />&nbsp; sleep(1)<br />end</p><p>what are these 4 lines supposed to mean? Thank you.</p></blockquote></div><p>will continually loop, calling the method process on @client.</p>]]></description>
			<author><![CDATA[dummy@example.com (Radar)]]></author>
			<pubDate>Sun, 10 Aug 2008 11:29:44 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=71297#p71297</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=71296#p71296</link>
			<description><![CDATA[<p>loop do<br />&nbsp; @client.process<br />&nbsp; sleep(1)<br />end</p><p>what are these 4 lines supposed to mean? Thank you.</p>]]></description>
			<author><![CDATA[dummy@example.com (leomayleomay)]]></author>
			<pubDate>Sun, 10 Aug 2008 10:05:23 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=71296#p71296</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=70173#p70173</link>
			<description><![CDATA[<p>I&#039;m getting error while start my webrick server at almost the end of the tutorial:</p><p>#./script/server<br />=&gt; Booting WEBrick...<br />(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&#039;: uninitialized constant AlertWorker (NameError)<br />&nbsp; &nbsp; 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&#039;<br />&nbsp; &nbsp; 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&#039;<br />&nbsp; &nbsp; from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:453:in `const_get&#039;<br />&nbsp; &nbsp; from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:453:in `worker_klass_constant&#039;<br />&nbsp; &nbsp; from (drbunix:///tmp/backgroundrbunix_localhost_2000) /home/hliu/Sandbox/dailynews/vendor/plugins/backgroundrb/server/lib/backgroundrb/middleman.rb:178:in `new_worker&#039;<br />&nbsp; &nbsp; from /home/hliu/Sandbox/dailynews/app/controllers/application.rb:17<br />&nbsp; &nbsp; from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_without_new_constant_marking&#039;<br />&nbsp; &nbsp; from /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_file&#039;<br />&nbsp; &nbsp;&nbsp; ... 31 levels...<br />&nbsp; &nbsp; from /usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39<br />&nbsp; &nbsp; from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require&#039;<br />&nbsp; &nbsp; from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require&#039;<br />&nbsp; &nbsp; from ./script/server:3</p><br /><p>I&#039;m using rails 2.1.0 and ruby 1.8.6</p><p>Anyone has any idea about this error? Thank you.</p>]]></description>
			<author><![CDATA[dummy@example.com (leomayleomay)]]></author>
			<pubDate>Thu, 31 Jul 2008 05:44:46 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=70173#p70173</guid>
		</item>
		<item>
			<title><![CDATA[Re: HOWTO: Send Instant Messages from Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=41409#p41409</link>
			<description><![CDATA[<p>I think maybe active_messaging might do what you want.</p><p>It uses apaches activeMQ.</p><p>here is a tutorial</p><p><a href="http://www.infoq.com/articles/intro-active-messaging-rails">http://www.infoq.com/articles/intro-act &#133; ging-rails</a></p><p>it has links to their wiki, and stuff.</p><p>I don&#039;t know much at all about it, but it might suit your needs.</p>]]></description>
			<author><![CDATA[dummy@example.com (levicole)]]></author>
			<pubDate>Sat, 06 Oct 2007 16:05:00 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=41409#p41409</guid>
		</item>
	</channel>
</rss>
