Permalink
Browse files

Tutorial five

  • Loading branch information...
1 parent c599752 commit eae6850aa77ae170da39e522e9c6be2699d1ff7e @squaremo committed Jun 21, 2013
Showing with 73 additions and 1 deletion.
  1. +10 −1 examples/tutorials/README.md
  2. +19 −0 examples/tutorials/emit_log_topic.js
  3. +44 −0 examples/tutorials/receive_logs_topic.js
@@ -52,16 +52,25 @@ log messages.
## [Tutorial four: Routing][tute-four]
-Uses RabbitMQ as a routing ('somecast') mechanism. `emit_log_direct`
+Using RabbitMQ as a routing ('somecast') mechanism. `emit_log_direct`
sends a log message with a severity, and all `receive_logs_direct`
processes receive log messages for the severities on which they are
listening.
* [emit_log_direct.js](emit_log.js)
* [receive_logs_direct.js](receive_logs_direct.js)
+## [Tutorial five: Topics][tute-five]
+
+Extends the previous tutorial to routing with wildcarded patterns.
+
+ * [emit_log_topic.js](emit_log_topic.js)
+ * [receive_logs_topic.js](receive_logs_topic.js)
+
+
[rabbitmq-tutes]: http://github.com/rabbitmq/rabbitmq-tutorials
[tute-one]: http://www.rabbitmq.com/tutorials/tutorial-one-python.html
[tute-two]: http://www.rabbitmq.com/tutorials/tutorial-two-python.html
[tute-three]: http://www.rabbitmq.com/tutorials/tutorial-three-python.html
[tute-four]: http://www.rabbitmq.com/tutorials/tutorial-four-python.html
+[tute-five]: http://www.rabbitmq.com/tutorials/tutorial-five-python.html
@@ -0,0 +1,19 @@
+#!/usr/bin/env node
+
+var amqp = require('amqplib');
+var when = require('when');
+
+var args = process.argv.slice(2);
+var key = (args.length > 0) ? args[0] : 'info';
+var message = args.slice(1).join(' ') || 'Hello World!';
+
+amqp.connect('amqp://localhost').then(function(conn) {
+ return when(conn.createChannel().then(function(ch) {
+ var ex = 'topic_logs';
+ var ok = ch.assertExchange(ex, 'topic', {durable: false});
+ return ok.then(function() {
+ ch.publish(ex, key, new Buffer(message));
+ console.log(" [x] Sent %s:'%s'", key, message);
+ });
+ })).ensure(function() { conn.close(); })
+}).then(null, console.log);
@@ -0,0 +1,44 @@
+#!/usr/bin/env node
+
+var amqp = require('amqplib');
+var basename = require('path').basename;
+var all = require('when').all;
+
+var keys = process.argv.slice(2);
+if (keys.length < 1) {
+ console.log('Usage: %s pattern [pattern...]',
+ basename(process.argv[1]));
+ process.exit(1);
+}
+
+amqp.connect('amqp://localhost').then(function(conn) {
+ process.once('SIGINT', function() { conn.close(); });
+ return conn.createChannel().then(function(ch) {
+ var ex = 'topic_logs';
+ var ok = ch.assertExchange(ex, 'topic', {durable: false});
+
+ ok = ok.then(function() {
+ return ch.assertQueue('', {exclusive: true});
+ });
+
+ ok = ok.then(function(qok) {
+ var queue = qok.queue;
+ return all(keys.map(function(rk) {
+ ch.bindQueue(queue, ex, rk);
+ })).then(function() { return queue; });
+ });
+
+ ok = ok.then(function(queue) {
+ return ch.consume(queue, logMessage, {noAck: true});
+ });
+ return ok.then(function() {
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
+ });
+
+ function logMessage(msg) {
+ console.log(" [x] %s:'%s'",
+ msg.fields.routingKey,
+ msg.content.toString());
+ }
+ });
+}).then(null, console.warn);

0 comments on commit eae6850

Please sign in to comment.