Last active
May 17, 2016 02:07
-
-
Save pegasuskim/feafaef9fb547c8f6c87 to your computer and use it in GitHub Desktop.
RabbitMq Consumer
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
var amqp = require('amqplib/callback_api'); | |
var qName = qname; | |
amqp.connect(host, function(err, conn) { | |
if (err != null) errorHandler(err); | |
on_consumer(conn); | |
}); | |
// Consumer | |
function on_consumer(conn) { | |
var ok = conn.createChannel(on_channel_open); | |
function on_channel_open(err, channel) { | |
if (err != null) errorHandler(err); | |
channel.assertQueue(qName); | |
channel.consume(qName, function(msg) { | |
if (msg !== null) { | |
receive(msg); | |
channel.ack(msg); | |
} | |
}); | |
} | |
} | |
function errorHandler(err, conn) { | |
console.log('RabbitMq error: ', err); | |
if (conn) conn.close(function() { process.exit(1); }); | |
}; | |
function receive(msg) { | |
var recv_msg = JSON.parse(msg.content.toString()); | |
console.log('RabbitMq msg: ', recv_msg); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment