Skip to content

Instantly share code, notes, and snippets.

@yunghoy
Created June 6, 2016 15:08
Show Gist options
  • Save yunghoy/b6dc2fcedba4ce896de8f4f27206a095 to your computer and use it in GitHub Desktop.
Save yunghoy/b6dc2fcedba4ce896de8f4f27206a095 to your computer and use it in GitHub Desktop.
routing key baed
// emit_log_direct.js
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var ex = 'direct_logs';
var args = process.argv.slice(2);
var msg = args.slice(1).join(' ') || 'Hello World!';
var severity = (args.length > 0) ? args[0] : 'info';
ch.assertExchange(ex, 'direct', {durable: false});
ch.publish(ex, severity, new Buffer(msg));
console.log(" [x] Sent %s: '%s'", severity, msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
// receive_logs_direct.js
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
var args = process.argv.slice(2);
if (args.length == 0) {
console.log("Usage: receive_logs_direct.js [info] [warning] [error]");
process.exit(1);
}
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var ex = 'direct_logs';
ch.assertExchange(ex, 'direct', {durable: false});
ch.assertQueue('', {exclusive: true}, function(err, q) {
console.log(' [*] Waiting for logs. To exit press CTRL+C');
args.forEach(function(severity) {
ch.bindQueue(q.queue, ex, severity);
});
ch.consume(q.queue, function(msg) {
console.log(" [x] %s: '%s'", msg.fields.routingKey, msg.content.toString());
}, {noAck: true});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment