Commit c9892aa

mo khan <mo@mokhan.ca>
2014-10-10 17:03:33
add example using a fanout exchange.
1 parent 3ced6cb
Changed files (2)
lib/3-pubsub/publish.rb
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+## encoding: utf-8
+
+require "bunny"
+
+connection = Bunny.new
+connection.start
+
+channel = connection.create_channel
+exchange = channel.fanout("logs")
+message = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
+exchange.publish(message)
+puts " [x] Sent #{message}"
+
+connection.close
lib/3-pubsub/subscriber.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+## encoding: utf-8
+
+require "bunny"
+
+connection = Bunny.new
+connection.start
+
+channel = connection.create_channel
+exchange = channel.fanout("logs")
+queue = channel.queue("", exclusive: true)
+
+queue.bind(exchange)
+
+puts " [*] Waiting for logs. To exit press CTRL+C"
+
+begin
+  queue.subscribe(block: true) do |delivery_info, properties, body|
+    puts " [x] #{body}"
+  end
+rescue Interrupt => _
+  channel.close
+  connection.close
+end