Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Start ports of the tutorial programs to the callback API

  • Loading branch information...
commit 3996a8b19fbb70ad85caeab44755e9af61a267db 1 parent 42d5013
@squaremo authored
View
3  examples/tutorials/README.md
@@ -1,7 +1,8 @@
# RabbitMQ tutorials
This directory contains the [RabbitMQ tutorials][rabbitmq-tutes],
-ported to amqplib.
+ported to amqplib. The sub-directory `callback_api` has translations
+of the tutorial programs to the callback-oriented API.
## Preparation
View
26 examples/tutorials/callback_api/receive.js
@@ -0,0 +1,26 @@
+#!/usr/bin/env node
+
+var amqp = require('amqplib/callback_api');
+
+function on_connect(err, conn) {
+ var q = 'hello';
+
+ if (err !== null) return console.error(err);
+
+ process.once('SIGINT', function() { conn.close(); });
+
+ function on_channel_open(err, ch) {
+ ch.assertQueue(q, {durable: false}, function(err, ok) {
+ if (err !== null) return console.error(err);
+ ch.consume(q, function(msg) { // message callback
+ console.log(" [x] Received '%s'", msg.content.toString());
+ }, {noAck: true}, function(_consumeOk) { // consume callback
+ console.log(' [*] Waiting for messages. To exit press CTRL+C');
+ });
+ });
+ }
+
+ conn.createChannel(on_channel_open);
+}
+
+amqp.connect(on_connect);
View
24 examples/tutorials/callback_api/send.js
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var amqp = require('amqplib/callback_api');
+
+function on_connect(err, conn) {
+ if (err !== null) return console.error(err);
+
+ var q = 'hello';
+ var msg = 'Hello World!';
+
+ function on_channel_open(err, ch) {
+ if (err !== null) return console.error(err);
+ ch.assertQueue(q, {durable: false}, function(e, ok) {
+ if (e !== null) return console.error(err);
+ ch.sendToQueue(q, new Buffer(msg));
+ console.log(" [x] Sent '%s'", msg);
+ ch.close(function() { conn.close(); });
+ });
+ }
+
+ conn.createChannel(on_channel_open);
+}
+
+amqp.connect(on_connect);
Please sign in to comment.
Something went wrong with that request. Please try again.