Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@elastic.io/amqp-rpc

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elastic.io/amqp-rpc - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

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

2

package.json
{
"name": "@elastic.io/amqp-rpc",
"description": "RPC over RabbitMQ for Node.js",
"version": "2.0.1",
"version": "2.0.2",
"homepage": "http://elastic.io",

@@ -6,0 +6,0 @@ "author": "elastic.io GmbH <info@elastic.io>",

@@ -12,18 +12,39 @@ # amqp-rpc

To create a new instance of RPC server/client for RabbitMQ:
There are two ways to run AMQPRPCServer/Client:
#### temporary queue
1. The server starts without any predefined queueName, asserts temporary queue.
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`.
3. Each client gets this queueName and uses it before initialization.
#### permanent queue
1. A queue is created somehow by an external service.
2. `server` gets the name of the queue before initialization and starts listening.
3. `client` gets the same name before initialization and uses it for sending requests.
#####Example with temporary queue:
Server:
```javascript
const amqplib = require('ampqlib');
const amqplib = require('amqplib');
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
const exchange = 'EXCHANGE';
const key = 'KEY';
async function init() {
const connection = await amqplib.connect('amqp://localhost');
// server start
const server = new AMQPRPCServer(connection);
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
await server.start();
// name of temporary queue, has to be passed somehow to client by external service
const requestsQueue = server.requestsQueue;
// client start
const client = new AMQPRPCClient(connection, {requestsQueue});
await client.start();
const response = await client.sendCommand('hello', ['Alisa']);
console.log('Alisa got response:', response);

@@ -33,13 +54,46 @@ return {server, client};

```
Full working example you could find [here](examples/amqp-rpc-with-tmp-queue.js).
To register a new RPC command in the server, use `addCommand()` method:
######Example with permanent queue:
```javascript
server.addCommand('print-hello-world', (name) => {
console.log('Hello, ${name}!');
const amqplib = require('amqplib');
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
async function init() {
return {foo: 'bar'};
});
const connection = await amqplib.connect('amqp://localhost');
// initial setup (e.g. should be provided on first launch)
const queueName = 'predefined-queue-name';
const channel = await connection.createChannel();
await channel.assertQueue(queueName);
// server start
const server = new AMQPRPCServer(connection, {queueName});
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
await server.start();
// client start
const client = new AMQPRPCClient(connection, {requestsQueue:queueName});
await client.start();
const response = await client.sendCommand('hello', ['Alisa']);
console.log('Alisa got response:', response);
return {server, client};
}
```
Full working example you could find [here](examples/amqp-rpc-with-permanent-queue.js).
#### Server handlers
To register a new RPC command in the server, use `addCommand()` method:
```javascript
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
```
Handler could also return a promise or async function, e.g.:

@@ -57,6 +111,5 @@

]);
assert.deepEqual(result, {foo: 'bar'});
```
### Event Emitter

@@ -63,0 +116,0 @@ Events receiver side code

@@ -160,3 +160,3 @@ 'use strict';

describe('on sender disconnect', () => {
it.skip('stream receiver should close itself', async () => {
it.skip('stream receiver should close itself', async () => { // eslint-disable-line
// this feature was broken while implementing external permission support

@@ -163,0 +163,0 @@ // it was hard to fix this, so decided to leave it as is for now (kind of YAGNI)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc