|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 3 | + |
| 4 | +//For more information about how this work, please read this: |
| 5 | +// https://www.rabbitmq.com/direct-reply-to.html |
| 6 | + |
| 7 | +const amqplib = require('amqplib'); |
| 8 | +const { AMQPRPCClient, AMQPRPCServer } = require('..'); |
| 9 | + |
| 10 | + |
| 11 | +function delay(ms) { |
| 12 | + return new Promise(resolve => { |
| 13 | + setTimeout(resolve, ms); |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * |
| 19 | + * @return {Promise<String>} queueName when server listens on for requests |
| 20 | + */ |
| 21 | +async function initServer() { |
| 22 | + console.log('Server starting'); |
| 23 | + const connection = await amqplib.connect('amqp://localhost'); |
| 24 | + const server = new AMQPRPCServer(connection); |
| 25 | + |
| 26 | + server.addCommand('hello', (name) => ({message: `Hello, ${name}!`})); |
| 27 | + |
| 28 | + server.addCommand('get-time', () => ({time: new Date()})); |
| 29 | + |
| 30 | + await server.start(); |
| 31 | + console.log('Server is ready'); |
| 32 | + return server.requestsQueue; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * @param requestsQueue |
| 38 | + * @return {Promise<void>} |
| 39 | + */ |
| 40 | +async function initClient1(requestsQueue) { |
| 41 | + console.log('Tom starting'); |
| 42 | + const connection = await amqplib.connect('amqp://localhost'); |
| 43 | + const client = new AMQPRPCClient(connection, { |
| 44 | + requestsQueue, |
| 45 | + repliesQueue: 'amq.rabbitmq.reply-to', |
| 46 | + consumeOptions: { |
| 47 | + noAck: true |
| 48 | + } |
| 49 | + }); |
| 50 | + await client.start(); |
| 51 | + |
| 52 | + const response1 = await client.sendCommand('hello', ['Tom']); |
| 53 | + console.log(`Tom got hello response ${response1.message}`); |
| 54 | + |
| 55 | + await delay(100); |
| 56 | + |
| 57 | + const response2 = await client.sendCommand('get-time', []); |
| 58 | + console.log(`Tom got 1st response for get-time: ${response2.time}`); |
| 59 | + |
| 60 | + await delay(100); |
| 61 | + |
| 62 | + const response3 = await client.sendCommand('get-time', []); |
| 63 | + console.log(`Tom got 2nd response for get-time: ${response3.time}`); |
| 64 | +} |
| 65 | + |
| 66 | +async function initClient2(requestsQueue) { |
| 67 | + console.log('Alisa starting'); |
| 68 | + const connection = await amqplib.connect('amqp://localhost'); |
| 69 | + const client = new AMQPRPCClient(connection, { |
| 70 | + requestsQueue, |
| 71 | + repliesQueue: 'amq.rabbitmq.reply-to', |
| 72 | + consumeOptions: { |
| 73 | + noAck: true |
| 74 | + } |
| 75 | + }); |
| 76 | + await client.start(); |
| 77 | + |
| 78 | + const response1 = await client.sendCommand('hello', ['Alisa']); |
| 79 | + console.log(`Alisa got hello response ${response1.message}`); |
| 80 | + |
| 81 | + await delay(150); |
| 82 | + |
| 83 | + const response2 = await client.sendCommand('get-time', []); |
| 84 | + console.log(`Alisa got response for get-time: ${response2.time}`); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +(async function main() { |
| 89 | + console.info('\n launch server:\n'); |
| 90 | + const tmpQueueName = await initServer(); |
| 91 | + |
| 92 | + console.info('\n launch clients:\n'); |
| 93 | + await Promise.all([ |
| 94 | + initClient1(tmpQueueName), |
| 95 | + initClient2(tmpQueueName) |
| 96 | + ]); |
| 97 | +})().catch(console.error.bind(console, 'General error:')); |
| 98 | + |
0 commit comments