Skip to content
Browse files

first node.js tutorial

  • Loading branch information...
1 parent 39ef465 commit 4fbf3306cc63a289ba88d8f9f7e4315ec1755d33 @majek majek committed Apr 17, 2012
View
1 .gitignore
@@ -0,0 +1 @@
+javascript-nodejs/node_modules
View
22 javascript-nodejs/README.md
@@ -0,0 +1,22 @@
+# Node.js code for RabbitMQ tutorials
+
+Here you can find Node.js code examples from [RabbitMQ
+tutorials](http://www.rabbitmq.com/getstarted.html).
+
+To successfully use the examples you will need a running RabbitMQ server.
+
+## Requirements
+
+Apart from `npm` and `node`, to run this code you need
+[`node-amqp`](https://github.com/postwait/node-amqp) version 0.1.X. To
+pull the dependency from `npm` run:
+
+ npm install
+
+## Code
+
+[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
+
+ node send.js
+ node receive.js
+
View
18 javascript-nodejs/amqp-hacks.js
@@ -0,0 +1,18 @@
+
+exports.safeEndConnection = function(connection) {
+
+ // `connection.end` doesn't flush outgoing buffers, run a
+ // synchronous command to comprehend
+
+ connection.queue('tmp-' + Math.random, {exclusive: true}, function(){
+ connection.end();
+
+ // `connection.end` in 0.1.3 raises a ECONNRESET error, silence it:
+ connection.once('error', function(e){
+ if (e.code !== 'ECONNRESET' || e.syscall !== 'write')
+ throw e;
+ });
+ });
+
+};
+
View
10 javascript-nodejs/package.json
@@ -0,0 +1,10 @@
+{
+ "name": "rabbitmq-tutorials",
+ "version": "0.0.0-unreleasable",
+ "private": true,
+ "main": "none",
+ "description": "Node.JS implementation of RabbitMQ tutorials",
+ "dependencies": {
+ "amqp": "0.1.3"
+ }
+}
View
13 javascript-nodejs/receive.js
@@ -0,0 +1,13 @@
+var amqp = require('amqp');
+var amqp_hacks = require('./amqp-hacks');
+
+var connection = amqp.createConnection({ host: 'localhost' });
+
+connection.on('ready', function(){
+ connection.queue('hello', {autoDelete: false}, function(queue){
+ queue.subscribe(function(msg){
+ console.log(" [x] Received %s", msg.data.toString('utf-8'));
+ });
+ });
+ console.log(' [*] Waiting for messages. To exit press CTRL+C')
+});
View
11 javascript-nodejs/send.js
@@ -0,0 +1,11 @@
+var amqp = require('amqp');
+var amqp_hacks = require('./amqp-hacks');
+
+var connection = amqp.createConnection({ host: 'localhost' });
+
+connection.on('ready', function(){
+ connection.publish('hello', 'Hello World!');
+ console.log(" [x] Sent 'Hello World!'");
+
+ amqp_hacks.safeEndConnection(connection);
+});

0 comments on commit 4fbf330

Please sign in to comment.
Something went wrong with that request. Please try again.