Fanning out messages requires an exchange of fanout type. In the example below, we have greetings exchange of type fanout bound to greetings1 and greetings2 queues.
First, declare the queues, exchange, and bindings. Note we explicitly put an empty routing key because it’s required, although it won’t be used anyway because the exchange type is fanout, meaning the messages are published to multiple queues.
➜ ~ docker exec -it rabbitmq rabbitmqadmin declare queue --name=greetings1
➜ ~ docker exec -it rabbitmq rabbitmqadmin declare queue --name=greetings2
➜ ~ docker exec -it rabbitmq rabbitmqadmin declare exchange --name greetings --type=fanout --durable=true
➜ ~ docker exec -it rabbitmq rabbitmqadmin declare binding --source=greetings --destination=greetings1 --destination-type=queue --routing-key=""
➜ ~ docker exec -it rabbitmq rabbitmqadmin declare binding --source=greetings --destination=greetings2 --destination-type=queue --routing-key=""
Then publish the messsage. Notice that the two queues received the same message
➜ ~ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=greetings --payload=hello
Message published and routed successfully
➜ ~ docker exec -it rabbitmq rabbitmqadmin get messages --queue=greetings1
┌───────────────┬─────────────┬───────────┬─────────────┬───────────────┬────────────┬─────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼───────────┼─────────────┼───────────────┼────────────┼─────────┼──────────────────┤
│ 5 │ false │ greetings │ │ 0 │ │ hello │ string │
└───────────────┴─────────────┴───────────┴─────────────┴───────────────┴────────────┴─────────┴──────────────────┘
➜ ~ docker exec -it rabbitmq rabbitmqadmin get messages --queue=greetings2
┌───────────────┬─────────────┬───────────┬─────────────┬───────────────┬────────────┬─────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼───────────┼─────────────┼───────────────┼────────────┼─────────┼──────────────────┤
│ 5 │ false │ greetings │ │ 0 │ │ hello │ string │
└───────────────┴─────────────┴───────────┴─────────────┴───────────────┴────────────┴─────────┴──────────────────┘