Created
August 10, 2011 04:57
-
-
Save arobson/1136159 to your computer and use it in GitHub Desktop.
coffeescript pub/sub demo for rabbitmq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sys = require "sys" | |
rabbit = require "amqp" | |
console.log "Connecting to rabbit..." | |
connection = rabbit.createConnection { host: "localhost", port: 5672 } | |
connection.on "ready", () -> | |
exchangeOptions = { type: "fanout", autoDelete: true } | |
connection.exchange( | |
"pubbery", | |
exchangeOptions, | |
(x) -> whenExchangeReady x | |
) | |
whenExchangeReady = (pub) -> | |
queueOptions = { autoDelete: true } | |
sub1 = createSubscription connection, "sub2" | |
sub2 = createSubscription connection, "sub1" | |
whenQueueIsReady sub2, () -> | |
console.log("HAY! Let's send like, 5 messages!") | |
message = { text: "HAY" } | |
for x in [5..1] | |
do () -> | |
pub.publish "nada", message, {} | |
whenQueueIsReady = ( queue, callback ) -> | |
queue.on "queueBindOk", () -> | |
queue.on "basicConsumeOk", () -> | |
callback() | |
createSubscription = (connection, queueName) -> | |
connection.queue( | |
queueName, | |
queueOptions, | |
(q) -> | |
q.bind("pubbery", "*") | |
q.subscribe( getHandler(queueName) ) | |
getHandler = (x) -> | |
(message, headers, deliveryInfo) -> | |
console.log(x + " received: " + message.text ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment