Skip to content

Commit fc6bce3

Browse files
committed
Modernize JavaScript tutorials: async/await and Direct Reply-to
Companion change for rabbitmq/rabbitmq-website#2477.
1 parent cd6409a commit fc6bce3

12 files changed

Lines changed: 264 additions & 310 deletions

javascript-nodejs/src/emit_log.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
13-
var exchange = 'logs';
14-
var msg = process.argv.slice(2).join(' ') || 'Hello World!';
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
158

16-
channel.assertExchange(exchange, 'fanout', {
17-
durable: false
18-
});
19-
channel.publish(exchange, '', Buffer.from(msg));
20-
console.log(" [x] Sent %s", msg);
9+
const exchange = 'logs';
10+
const msg = process.argv.slice(2).join(' ') || 'Hello World!';
11+
12+
await channel.assertExchange(exchange, 'fanout', {
13+
durable: false
2114
});
15+
channel.publish(exchange, '', Buffer.from(msg));
16+
console.log(" [x] Sent %s", msg);
2217

2318
setTimeout(function() {
2419
connection.close();
2520
process.exit(0);
2621
}, 500);
27-
});
22+
}
23+
24+
main();
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
13-
var exchange = 'direct_logs';
14-
var args = process.argv.slice(2);
15-
var msg = args.slice(1).join(' ') || 'Hello World!';
16-
var severity = (args.length > 0) ? args[0] : 'info';
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
178

18-
channel.assertExchange(exchange, 'direct', {
19-
durable: false
20-
});
21-
channel.publish(exchange, severity, Buffer.from(msg));
22-
console.log(" [x] Sent %s: '%s'", severity, msg);
9+
const exchange = 'direct_logs';
10+
const args = process.argv.slice(2);
11+
const msg = args.slice(1).join(' ') || 'Hello World!';
12+
const severity = (args.length > 0) ? args[0] : 'info';
13+
14+
await channel.assertExchange(exchange, 'direct', {
15+
durable: false
2316
});
17+
channel.publish(exchange, severity, Buffer.from(msg));
18+
console.log(" [x] Sent %s: '%s'", severity, msg);
2419

2520
setTimeout(function() {
2621
connection.close();
2722
process.exit(0);
2823
}, 500);
29-
});
24+
}
25+
26+
main();
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
13-
var exchange = 'topic_logs';
14-
var args = process.argv.slice(2);
15-
var key = (args.length > 0) ? args[0] : 'anonymous.info';
16-
var msg = args.slice(1).join(' ') || 'Hello World!';
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
178

18-
channel.assertExchange(exchange, 'topic', {
19-
durable: false
20-
});
21-
channel.publish(exchange, key, Buffer.from(msg));
22-
console.log(" [x] Sent %s: '%s'", key, msg);
9+
const exchange = 'topic_logs';
10+
const args = process.argv.slice(2);
11+
const key = (args.length > 0) ? args[0] : 'anonymous.info';
12+
const msg = args.slice(1).join(' ') || 'Hello World!';
13+
14+
await channel.assertExchange(exchange, 'topic', {
15+
durable: false
2316
});
17+
channel.publish(exchange, key, Buffer.from(msg));
18+
console.log(" [x] Sent %s:'%s'", key, msg);
2419

2520
setTimeout(function() {
2621
connection.close();
2722
process.exit(0);
2823
}, 500);
29-
});
24+
}
25+
26+
main();

javascript-nodejs/src/new_task.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
13-
var queue = 'task_queue';
14-
var msg = process.argv.slice(2).join(' ') || "Hello World!";
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
158

16-
channel.assertQueue(queue, {
17-
durable: true,
18-
arguments: { 'x-queue-type': 'quorum' }
19-
});
20-
channel.sendToQueue(queue, Buffer.from(msg), {
21-
persistent: true
22-
});
23-
console.log(" [x] Sent '%s'", msg);
9+
const queue = 'task_queue';
10+
const msg = process.argv.slice(2).join(' ') || "Hello World!";
11+
12+
await channel.assertQueue(queue, {
13+
durable: true,
14+
arguments: { 'x-queue-type': 'quorum' }
15+
});
16+
channel.sendToQueue(queue, Buffer.from(msg), {
17+
persistent: true
2418
});
19+
console.log(" [x] Sent '%s'", msg);
20+
2521
setTimeout(function() {
2622
connection.close();
2723
process.exit(0);
2824
}, 500);
29-
});
25+
}
26+
27+
main();

javascript-nodejs/src/receive.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
138

14-
var queue = 'hello';
9+
const queue = 'hello';
1510

16-
channel.assertQueue(queue, {
17-
durable: true,
18-
arguments: { 'x-queue-type': 'quorum' }
19-
});
11+
await channel.assertQueue(queue, {
12+
durable: true,
13+
arguments: { 'x-queue-type': 'quorum' }
14+
});
2015

21-
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
16+
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
2217

23-
channel.consume(queue, function(msg) {
24-
console.log(" [x] Received %s", msg.content.toString());
25-
}, {
26-
noAck: true
27-
});
18+
channel.consume(queue, function(msg) {
19+
console.log(" [x] Received %s", msg.content.toString());
20+
}, {
21+
noAck: true
2822
});
29-
});
23+
}
24+
25+
main();
Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
amqp.connect('amqp://localhost', function(error0, connection) {
6-
if (error0) {
7-
throw error0;
8-
}
9-
connection.createChannel(function(error1, channel) {
10-
if (error1) {
11-
throw error1;
12-
}
13-
var exchange = 'logs';
5+
async function main() {
6+
const connection = await amqp.connect('amqp://localhost');
7+
const channel = await connection.createChannel();
8+
9+
const exchange = 'logs';
1410

15-
channel.assertExchange(exchange, 'fanout', {
16-
durable: false
17-
});
11+
await channel.assertExchange(exchange, 'fanout', {
12+
durable: false
13+
});
1814

19-
channel.assertQueue('', {
20-
exclusive: true
21-
}, function(error2, q) {
22-
if (error2) {
23-
throw error2;
24-
}
25-
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
26-
channel.bindQueue(q.queue, exchange, '');
15+
const q = await channel.assertQueue('', {
16+
exclusive: true
17+
});
18+
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
19+
channel.bindQueue(q.queue, exchange, '');
2720

28-
channel.consume(q.queue, function(msg) {
29-
if (msg.content) {
30-
console.log(" [x] %s", msg.content.toString());
31-
}
32-
}, {
33-
noAck: true
34-
});
35-
});
21+
channel.consume(q.queue, function(msg) {
22+
if (msg.content) {
23+
console.log(" [x] %s", msg.content.toString());
24+
}
25+
}, {
26+
noAck: true
3627
});
37-
});
28+
}
29+
30+
main();
Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib/callback_api');
3+
const amqp = require('amqplib');
44

5-
var args = process.argv.slice(2);
5+
const args = process.argv.slice(2);
66

77
if (args.length == 0) {
88
console.log("Usage: receive_logs_direct.js [info] [warning] [error]");
99
process.exit(1);
1010
}
1111

12-
amqp.connect('amqp://localhost', function(error0, connection) {
13-
if (error0) {
14-
throw error0;
15-
}
16-
connection.createChannel(function(error1, channel) {
17-
if (error1) {
18-
throw error1;
19-
}
20-
var exchange = 'direct_logs';
21-
22-
channel.assertExchange(exchange, 'direct', {
23-
durable: false
24-
});
25-
26-
channel.assertQueue('', {
27-
exclusive: true
28-
}, function(error2, q) {
29-
if (error2) {
30-
throw error2;
31-
}
32-
console.log(' [*] Waiting for logs. To exit press CTRL+C');
33-
34-
args.forEach(function(severity) {
35-
channel.bindQueue(q.queue, exchange, severity);
36-
});
37-
38-
channel.consume(q.queue, function(msg) {
39-
console.log(" [x] %s: '%s'", msg.fields.routingKey, msg.content.toString());
40-
}, {
41-
noAck: true
42-
});
43-
});
12+
async function main() {
13+
const connection = await amqp.connect('amqp://localhost');
14+
const channel = await connection.createChannel();
15+
16+
const exchange = 'direct_logs';
17+
18+
await channel.assertExchange(exchange, 'direct', {
19+
durable: false
20+
});
21+
22+
const q = await channel.assertQueue('', {
23+
exclusive: true
24+
});
25+
console.log(' [*] Waiting for logs. To exit press CTRL+C');
26+
27+
args.forEach(function(severity) {
28+
channel.bindQueue(q.queue, exchange, severity);
29+
});
30+
31+
channel.consume(q.queue, function(msg) {
32+
console.log(" [x] %s: '%s'", msg.fields.routingKey, msg.content.toString());
33+
}, {
34+
noAck: true
4435
});
45-
});
36+
}
37+
38+
main();

0 commit comments

Comments
 (0)