Skip to content

Commit 2daaf93

Browse files
authored
updated examples (#16)
1 parent aa4f5a1 commit 2daaf93

File tree

3 files changed

+244
-11
lines changed

3 files changed

+244
-11
lines changed

README.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,89 @@ Provides
99
## Getting Started
1010
### RPC
1111

12-
To create a new instance of RPC server/client for RabbitMQ:
12+
There are two ways to run AMQPRPCServer/Client:
1313

14+
#### temporary queue
15+
1. The server starts without any predefined queueName, asserts temporary queue.
16+
2. Generated `queueName` retrieved from the `server` instance and passed somehow to the `client` (one or many). It's supposed, that this transfer isn't covered by `amqp-rpc` lib and it should be implemented somehow by the developer of code, which uses `amqp-rpc`.
17+
3. Each client gets this queueName and uses it before initialization.
18+
19+
#### permanent queue
20+
1. A queue is created somehow by an external service.
21+
2. `server` gets the name of the queue before initialization and starts listening.
22+
3. `client` gets the same name before initialization and uses it for sending requests.
23+
24+
25+
#####Example with temporary queue:
26+
27+
Server:
1428
```javascript
15-
const amqplib = require('ampqlib');
29+
const amqplib = require('amqplib');
1630
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
17-
const exchange = 'EXCHANGE';
18-
const key = 'KEY';
31+
1932

2033
async function init() {
2134
const connection = await amqplib.connect('amqp://localhost');
35+
36+
// server start
2237
const server = new AMQPRPCServer(connection);
38+
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
2339
await server.start();
40+
41+
// name of temporary queue, has to be passed somehow to client by external service
2442
const requestsQueue = server.requestsQueue;
2543

44+
// client start
2645
const client = new AMQPRPCClient(connection, {requestsQueue});
2746
await client.start();
47+
const response = await client.sendCommand('hello', ['Alisa']);
48+
console.log('Alisa got response:', response);
2849

2950
return {server, client};
3051
}
3152
```
53+
Full working example you could find [here](examples/amqp-rpc-with-tmp-queue.js).
3254

33-
To register a new RPC command in the server, use `addCommand()` method:
55+
56+
######Example with permanent queue:
3457

3558
```javascript
36-
server.addCommand('print-hello-world', (name) => {
37-
console.log('Hello, ${name}!');
59+
const amqplib = require('amqplib');
60+
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
61+
62+
63+
async function init() {
64+
65+
const connection = await amqplib.connect('amqp://localhost');
66+
67+
// initial setup (e.g. should be provided on first launch)
68+
const queueName = 'predefined-queue-name';
69+
const channel = await connection.createChannel();
70+
await channel.assertQueue(queueName);
3871

39-
return {foo: 'bar'};
40-
});
72+
// server start
73+
const server = new AMQPRPCServer(connection, {queueName});
74+
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
75+
await server.start();
76+
77+
// client start
78+
const client = new AMQPRPCClient(connection, {requestsQueue:queueName});
79+
await client.start();
80+
const response = await client.sendCommand('hello', ['Alisa']);
81+
console.log('Alisa got response:', response);
82+
83+
return {server, client};
84+
}
85+
```
86+
87+
Full working example you could find [here](examples/amqp-rpc-with-permanent-queue.js).
88+
89+
#### Server handlers
90+
91+
To register a new RPC command in the server, use `addCommand()` method:
92+
93+
```javascript
94+
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
4195
```
4296

4397
Handler could also return a promise or async function, e.g.:
@@ -52,10 +106,9 @@ To call an RPC command from the client, use `sendCommand()` method:
52106
const result = await client.sendCommand('print-hello-world', [
53107
'World'
54108
]);
55-
56-
assert.deepEqual(result, {foo: 'bar'});
57109
```
58110
111+
59112
### Event Emitter
60113
Events receiver side code
61114
````javascript
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* eslint-disable no-console */
2+
/* eslint-disable import/no-extraneous-dependencies */
3+
4+
const amqplib = require('amqplib');
5+
const {AMQPRPCClient, AMQPRPCServer} = require('..');
6+
7+
8+
function delay(ms) {
9+
return new Promise(resolve => {
10+
setTimeout(resolve, ms);
11+
})
12+
}
13+
14+
/**
15+
*
16+
* @return {Promise<String>} queueName when server listens on for requests
17+
*/
18+
async function initialSetup(queueName) {
19+
const connection = await amqplib.connect('amqp://localhost');
20+
const channel = await connection.createChannel();
21+
await channel.assertQueue(queueName);
22+
}
23+
24+
/**
25+
*
26+
* @param requestsQueue
27+
* @return {Promise<void>}
28+
*/
29+
async function initServer(requestsQueue) {
30+
console.log('Server starting');
31+
const connection = await amqplib.connect('amqp://localhost');
32+
const server = new AMQPRPCServer(connection, {requestsQueue});
33+
34+
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
35+
36+
server.addCommand('get-time', () => ({time: new Date()}));
37+
38+
await server.start();
39+
console.log('Server is ready');
40+
}
41+
42+
/**
43+
*
44+
* @param requestsQueue
45+
* @return {Promise<void>}
46+
*/
47+
async function initClient1(requestsQueue) {
48+
console.log('Tom starting');
49+
const connection = await amqplib.connect('amqp://localhost');
50+
const client = new AMQPRPCClient(connection, {requestsQueue});
51+
await client.start();
52+
53+
const response1 = await client.sendCommand('hello', ['Tom']);
54+
console.log(`Tom got hello response ${response1.message}`);
55+
56+
await delay(100);
57+
58+
const response2 = await client.sendCommand('get-time', []);
59+
console.log(`Tom got 1st response for get-time: ${response2.time}`);
60+
61+
await delay(100);
62+
63+
const response3 = await client.sendCommand('get-time', []);
64+
console.log(`Tom got 2nd response for get-time: ${response3.time}`);
65+
}
66+
67+
async function initClient2(requestsQueue) {
68+
console.log('Alisa starting');
69+
const connection = await amqplib.connect('amqp://localhost');
70+
const client = new AMQPRPCClient(connection, {requestsQueue});
71+
await client.start();
72+
73+
const response1 = await client.sendCommand('hello', ['Alisa']);
74+
console.log(`Alisa got hello response ${response1.message}`);
75+
76+
await delay(150);
77+
78+
const response2 = await client.sendCommand('get-time', []);
79+
console.log(`Alisa got response for get-time: ${response2.time}`);
80+
}
81+
82+
83+
(async function main() {
84+
console.info('\n setup\n');
85+
const queueName = 'predefined-queue-name';
86+
await initialSetup(queueName);
87+
88+
console.info('\n launch server:\n');
89+
await initServer(queueName);
90+
91+
console.info('\n launch clients:\n');
92+
await Promise.all([
93+
initClient1(queueName),
94+
initClient2(queueName)
95+
]);
96+
})().catch(console.error.bind(console, 'General error:'));
97+

examples/amqp-rpc-with-tmp-queue.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* eslint-disable no-console */
2+
/* eslint-disable import/no-extraneous-dependencies */
3+
4+
const amqplib = require('amqplib');
5+
const {AMQPRPCClient, AMQPRPCServer} = require('..');
6+
7+
8+
function delay(ms) {
9+
return new Promise(resolve => {
10+
setTimeout(resolve, ms);
11+
})
12+
}
13+
14+
/**
15+
*
16+
* @return {Promise<String>} queueName when server listens on for requests
17+
*/
18+
async function initServer() {
19+
console.log('Server starting');
20+
const connection = await amqplib.connect('amqp://localhost');
21+
const server = new AMQPRPCServer(connection);
22+
23+
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
24+
25+
server.addCommand('get-time', () => ({time: new Date()}));
26+
27+
await server.start();
28+
console.log('Server is ready');
29+
return server.requestsQueue;
30+
}
31+
32+
/**
33+
*
34+
* @param requestsQueue
35+
* @return {Promise<void>}
36+
*/
37+
async function initClient1(requestsQueue) {
38+
console.log('Tom starting');
39+
const connection = await amqplib.connect('amqp://localhost');
40+
const client = new AMQPRPCClient(connection, {requestsQueue});
41+
await client.start();
42+
43+
const response1 = await client.sendCommand('hello', ['Tom']);
44+
console.log(`Tom got hello response ${response1.message}`);
45+
46+
await delay(100);
47+
48+
const response2 = await client.sendCommand('get-time', []);
49+
console.log(`Tom got 1st response for get-time: ${response2.time}`);
50+
51+
await delay(100);
52+
53+
const response3 = await client.sendCommand('get-time', []);
54+
console.log(`Tom got 2nd response for get-time: ${response3.time}`);
55+
}
56+
57+
async function initClient2(requestsQueue) {
58+
console.log('Alisa starting');
59+
const connection = await amqplib.connect('amqp://localhost');
60+
const client = new AMQPRPCClient(connection, {requestsQueue});
61+
await client.start();
62+
63+
const response1 = await client.sendCommand('hello', ['Alisa']);
64+
console.log(`Alisa got hello response ${response1.message}`);
65+
66+
await delay(150);
67+
68+
const response2 = await client.sendCommand('get-time', []);
69+
console.log(`Alisa got response for get-time: ${response2.time}`);
70+
}
71+
72+
73+
(async function main() {
74+
console.info('\n launch server:\n');
75+
const tmpQueueName = await initServer();
76+
77+
console.info('\n launch clients:\n');
78+
await Promise.all([
79+
initClient1(tmpQueueName),
80+
initClient2(tmpQueueName)
81+
]);
82+
})().catch(console.error.bind(console, 'General error:'));
83+

0 commit comments

Comments
 (0)