Skip to content

Instantly share code, notes, and snippets.

@oisin
Created September 10, 2013 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisin/6506907 to your computer and use it in GitHub Desktop.
Save oisin/6506907 to your computer and use it in GitHub Desktop.
Simple AMQP clients using Bunny and CloudAMQP
# Consumer - subscribes to the queue and gets a callback for each message
require 'bunny'
AMQP_URL = "your amqp broker URL"
conn = Bunny.new(AMQP_URL)
conn.start
ch = conn.create_channel
q = ch.queue("converser.triggers", durable: true)
q.subscribe do |delivery_info, metadata, payload|
puts "Message Payload: #{payload}"
end
conn.close
# Producer - publishes 10 testing messages, each 2 seconds apart
#
require 'bunny'
require 'json'
AMQP_URL = "your amqp broker URL"
conn = Bunny.new(AMQP_URL)
conn.start
ch = conn.create_channel
q = ch.queue("converser.triggers", durable: true)
10.times {
ch.default_exchange.publish({text: "Testing, testing"}.to_json, persistent: true, routing_key: q.name)
sleep 2
}
conn.close
source 'https://rubygems.org'
gem 'bunny'
gem 'json'
$ bundle install
Fetching gem metadata from https://rubygems.org/...
Resolving dependencies...
Using amq-protocol (1.7.0)
Using bunny (0.10.6)
Using json (1.8.0)
Using bundler (1.3.5)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ bundle exec ruby bunnypub.rb
# Wait for 20 seconds or so!
$ bundle exec ruby bunnycon.rb
Message Payload: {"text":"Testing, testing"}
Message Payload: {"text":"Testing, testing"}
Message Payload: {"text":"Testing, testing"}
Message Payload: {"text":"Testing, testing"}
# etc etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment