In RabbitMQ, messages are published to exchanges and based on the type of the exchange, messages will be routed to specific queues, which is where the messages are consumed.
There are three types of exchanges you can publish to: Direct, Fanout, and Topic. In a Direct exchange (which is the default), a queue subscribes to the exchange with a routing key and any messages delivered to the exchange with that routing key is delivered to that queue to be consumed. So in a sense it's 1-1: exchange -> key -> queue.
However, in a fanout exchange, every queue subscribed to an exchange gets a copy of the message. This means that when you deliver a message to a fanout exchange, that exchange will give a copy of that message to every queue that is listening.
So you can send the "New Item" message to the "notify" fanout exchange, and every queue bound to that exchange will get the "New Item" message; i.e. the phone notification service, the web client notification service, etc, will all get the "New Item" message and can react accordingly. What makes this even better, is that if you add a new service (e.g. a new iPhone app), all you have to do to tap into your notification system to receive new item alerts is to tell a queue to listen to the notify exchange! You hear me right: you don't have to change any existing code.
Now, Andrei Marcu's aamnotifs is written in Python and he uses pika as his Rabbitmq module. Pika is a little more relaxed about the exchange types: any queues subscribed to an exchange with the same routing key will be treated as if they are attached to a fanout exchange, and will all receive copies of each message sent to the exchange with that routing key.
I say all of this just to point out the important point that Andrei left out: to add a second, third, or nth listener to your notification queue which receives a copy of the updates, you just have to add another
n.receive("routing_name", print_notification)
line and change print_notification to whatever method you want the message to go to. For example: import notifs
def print_notification(title, message):
print "Notification received: {0}: {1}".format(title, message)
def web_app_notify(title, message):
print "Webapp notification received: {0}: {1}".format(title, message)
def iphone_app_notify(title, message):
print "iPhone App notification received: {0}: {1}".format(title, message)
def android_app_notify(title, message):
print "Android App notification received: {0}: {1}".format(title, message)
try:
n = notifs.Notifs("amqps://user:password@domain.tld:5673/%2F")
n.receive("routing_name", print_notification)
n.receive("routing_name", web_app_notify)
n.receive("routing_name", iphone_app_notify)
n.receive("routing_name", android_app_notifify)
except KeyboardInterrupt:
break
gist here: https://gist.github.com/rickhanlonii/6091542