8000 Publish/Subscribe tutorial emit_log.js · syk-coder/rabbitmq-tutorials@6d6a421 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d6a421

Browse files
committed
Publish/Subscribe tutorial emit_log.js
1 parent 8961f92 commit 6d6a421

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

javascript-nodejs/src/emit_log.js

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

0 commit comments

Comments
 (0)
0