8000 node/amqplib "Hello World!" tutorial · syk-coder/rabbitmq-tutorials@7f8f3d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f8f3d3

Browse files
committed
node/amqplib "Hello World!" tutorial
1 parent eb1b500 commit 7f8f3d3

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

javascript-nodejs/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "rabbitmq-node-tutorial",
3+
"version": "1.0.0",
4+
"description": "RabbitMQ amqp.node tutorial ",
5+
"dependencies": {
6+
"amqplib": "^0.3.2",
7+
"when": "^3.6.4"
8+
}
9+
}

javascript-nodejs/src/receive.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
3+
var amqp = require('amqplib');
4+
5+
amqp.connect('amqp://localhost').then(function(conn) {
6+
process.once('SIGINT', function() { conn.close(); });
7+
return conn.createChannel().then(function(ch) {
8+
9+
var ok = ch.assertQueue('hello', {durable: false});
10+
11+
ok = ok.then(function(_qok) {
12+
return ch.consume('hello', function(msg) {
13+
console.log(" [x] Received '%s'", msg.content.toString());
14+
}, {noAck: true});
15+
});
16+
17+
return ok.then(function(_consumeOk) {
18+
console.log(' [*] Waiting for messages. To exit press CTRL+C');
19+
});
20+
});
21+
}).then(null, console.warn);

javascript-nodejs/src/send.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
var amqp = require('amqplib');
4+
var when = require('when');
5+
6+
amqp.connect('amqp://localhost').then(function(conn) {
7+
return when(conn.createChannel().then(function(ch) {
8+
9+
var q = 'hello';
10+
var ok = ch.assertQueue(q, {durable: false});
11+
12+
return ok.then(function(_qok) {
13+
var msg = 'Hello World!';
14+
ch.sendToQueue(q, new Buffer(msg));
15+
console.log(" [x] Sent '%s'", msg);
16+
return ch.close();
17+
});
18+
})).ensure(function() { conn.close(); });
19+
}).then(null, console.warn);

0 commit comments

Comments
 (0)
0