For instance, I have this consumer script:
$ cat consume.rb
require "bunny"
conn = Bunny.new
conn.start
ch = conn.create_channel
exchange = ch.fanout("fanout")
queue = ch.queue("").bind(exchange)
queue.subscribe(:block => true) do |delivery_info, metadata, payload|
puts "Received #{payload}"
end
And this producer script:
$ cat publish.rb
require "bunny"
conn = Bunny.new
conn.start
ch = conn.create_channel
exchange = ch.fanout("fanout")
10.times do
data = rand.to_s
exchange.publish(data)
end
I open two terminals, run each consumer.rb in each terminal separately.
Then open the third terminal, run publish.rb.
And we will see two consumers get and print the same message items.
Consumer1 output:
$ ruby consume.rb
Received 0.8439900790883322
Received 0.7934571880375225
Received 0.7594074915448258
Received 0.6846564266710206
Received 0.04857228429430649
Received 0.5275599997437828
Received 0.9342227328889011
Received 0.3994398464282922
Received 0.2484340114543926
Received 0.6501834053032844
Consumer2 output:
$ ruby consume.rb
Received 0.8439900790883322
Received 0.7934571880375225
Received 0.7594074915448258
Received 0.6846564266710206
Received 0.04857228429430649
Received 0.5275599997437828
Received 0.9342227328889011
Received 0.3994398464282922
Received 0.2484340114543926
Received 0.6501834053032844
This is what the fanout exchange works like.
Thanks.