1

I have two rails applications, App1 and App2(added cloudAMQP gem) in heroku, App1 is producing some message when click on a button

App1

class Publisher
   def publish
    # Start a communication session with RabbitMQ
    connection = Bunny.new(:host => "chimpanzee.rmq.cloudamqp.com", :vhost => "test", :user => "test", :password => "password")
    connection.start

    # open a channel
    channel = connection.create_channel

    # declare a queue
    queue  = channel.queue("test1")

    # publish a message to the default exchange which then gets routed to this queue
    queue.publish("Hello, everybody!")

   end
end

so in the App2 i have to consume all the messages without any button click and put that in sidekiq to process the data, but i am stuck on how can i automatically read from that queue, i know the code how to read values from queue, people are saying sneakers gem, but i am bit confused with sidekiq and sneakers, any idea of how can we do it in heroku?

Developer
  • 561
  • 7
  • 29
  • thanks @Sergio Tulentsev, i have a question can i use sidekiq instead of sneakers if this is using for background jobs? i don't want to use more gem, maximum 2 – Developer Sep 08 '18 at 01:20

1 Answers1

1

To read the messages you publish from App1 to App2, in App2 you gonna need sneakers (https://github.com/jondot/sneakers)

your reader would do something like:

class Reader
  include Sneakers::Worker
  from_queue 'test1'

  def work(message)
    # your code
    ack!
  end
end

and you need to configure your environment, you can take a look at https://github.com/jondot/sneakers/wiki/Configuration

sad parrot
  • 71
  • 1
  • 3
  • thanks @sad parrot, will this automatically listen from publisher? i have a question can i use sidekiq instead of sneakers if this is using for background jobs? i don't want to use more gem, maximum 2 – Developer Sep 08 '18 at 01:20
  • yes, it will automatically listen from publisher and you can run it in background ([with rails](https://github.com/jondot/sneakers/wiki/How-To:-Rails-Background-Jobs) and [with stand alone worker](https://github.com/jondot/sneakers/wiki/How-to:-running-a-stand-alone-worker)). If you don't want to use sneakers you can write your own consumer with `queue.subscribe` (see de docs [here](https://www.rabbitmq.com/tutorials/tutorial-one-ruby.html)). – sad parrot Sep 08 '18 at 14:30
  • I wouldn't recommend sidekiq for this cause it was not designed for it (but it's possible, take a look in this [thread](https://github.com/mperham/sidekiq/issues/1961)). If your concern is not use more gems I would follow the approach of writing your own consumer with `queue.subscribe`. – sad parrot Sep 08 '18 at 14:32
  • if i want to use queue.subscribe, how can i write to automatically listen the queue with bunny, or should i use some cron job to check evey 5 seconds whether that queue has any message or not ? anyone has any idea about that, i am stuck on this. – Developer Sep 11 '18 at 05:40
  • 1
    @django Follow this rabbitmq tutorial here [part1](https://www.rabbitmq.com/tutorials/tutorial-one-ruby.html) and [part2](https://www.rabbitmq.com/tutorials/tutorial-two-ruby.html). You are going to write just the receiver part of the tutorial with Bunny as it is described in it. After that, you will leave your receiver running, it has to stay "up" to keep receiving the messages your App1 is sending (for ex: if you are using heroku, leave it running in a dyno). – sad parrot Sep 12 '18 at 00:40