Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I usually do this via a separate messaging daemon which loads the rails environment.</p> <p>So a very simplistic example would look like this in rails_root/script/myapp_daemon.rb :</p> <pre><code> #!/usr/bin/env ruby require 'rubygems' require 'amqp' require 'daemons' ENV["RAILS_ENV"] ||= "development" require File.dirname(__FILE__) + "/../config/environment" options = { :backtrace => true, :dir => '.', :log_output => true} Daemons.run_proc('myapp_daemon', options) do EventMachine.run do connection = AMQP.connect(:host => "127.0.0.1") channel = AMQP::Channel.new(connection) queue = channel.queue("/myapp_daemon", :durable => true) exchange = channel.direct("") queue.subscribe do |payload| obj = JSON.parse(payload) #... handle messages here, utilize your rails models Foo.create(...) end end end </code></pre> <p>You would also need the right gem requires in your Gemfile: amqp, daemons, eventmachine</p> <p>Then either run it manually alongside your app:</p> <pre><code>RAILS_ENV=development script/myapp_daemon.rb run </code></pre> <p>Or start it from one of your app initializers:</p> <pre><code>system('script/myapp_daemon.rb start') </code></pre> <p>To dig into amqp check out the following, this will give a nice high level overview: <a href="http://www.rubyinside.com/why-rubyists-should-care-about-messaging-a-high-level-intro-5017.html">http://www.rubyinside.com/why-rubyists-should-care-about-messaging-a-high-level-intro-5017.html</a></p> <p>This gives a very detailed explanation with working examples: <a href="http://rubydoc.info/github/ruby-amqp/amqp/master/file/docs/Exchanges.textile#Publishing_messages_as_immediate_">http://rubydoc.info/github/ruby-amqp/amqp/master/file/docs/Exchanges.textile#Publishing_messages_as_immediate_</a></p> <p>Finally see if Bunny accomplishes everything you need for the client, it is simpler: <a href="https://github.com/celldee/bunny/wiki/Using-Bunny">https://github.com/celldee/bunny/wiki/Using-Bunny</a></p> <p>Hope that helps</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload