Skip to content

Commit 4fbf330

Browse files
committed
first node.js tutorial
1 parent 39ef465 commit 4fbf330

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
javascript-nodejs/node_modules

javascript-nodejs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Node.js code for RabbitMQ tutorials
2+
3+
Here you can find Node.js code examples from [RabbitMQ
4+
tutorials](http://www.rabbitmq.com/getstarted.html).
5+
6+
To successfully use the examples you will need a running RabbitMQ server.
7+
8+
## Requirements
9+
10+
Apart from `npm` and `node`, to run this code you need
11+
[`node-amqp`](https://github.com/postwait/node-amqp) version 0.1.X. To
12+
pull the dependency from `npm` run:
13+
14+
npm install
15+
16+
## Code
17+
18+
[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
19+
20+
node send.js
21+
node receive.js
22+

javascript-nodejs/amqp-hacks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
exports.safeEndConnection = function(connection) {
3+
4+
// `connection.end` doesn't flush outgoing buffers, run a
5+
// synchronous command to comprehend
6+
7+
connection.queue('tmp-' + Math.random, {exclusive: true}, function(){
8+
connection.end();
9+
10+
// `connection.end` in 0.1.3 raises a ECONNRESET error, silence it:
11+
connection.once('error', function(e){
12+
if (e.code !== 'ECONNRESET' || e.syscall !== 'write')
13+
throw e;
14+
});
15+
});
16+
17+
};
18+

javascript-nodejs/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "rabbitmq-tutorials",
3+
"version": "0.0.0-unreleasable",
4+
"private": true,
5+
"main": "none",
6+
"description": "Node.JS implementation of RabbitMQ tutorials",
7+
"dependencies": {
8+
"amqp": "0.1.3"
9+
}
10+
}

javascript-nodejs/receive.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var amqp = require('amqp');
2+
var amqp_hacks = require('./amqp-hacks');
3+
4+
var connection = amqp.createConnection({ host: 'localhost' });
5+
6+
connection.on('ready', function(){
7+
connection.queue('hello', {autoDelete: false}, function(queue){
8+
queue.subscribe(function(msg){
9+
console.log(" [x] Received %s", msg.data.toString('utf-8'));
10+
});
11+
});
12+
console.log(' [*] Waiting for messages. To exit press CTRL+C')
13+
});

javascript-nodejs/send.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var amqp = require('amqp');
2+
var amqp_hacks = require('./amqp-hacks');
3+
4+
var connection = amqp.createConnection({ host: 'localhost' });
5+
6+
connection.on('ready', function(){
7+
connection.publish('hello', 'Hello World!');
8+
console.log(" [x] Sent 'Hello World!'");
9+
10+
amqp_hacks.safeEndConnection(connection);
11+
});

0 commit comments

Comments
 (0)