
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
@elastic.io/amqp-rpc
Advanced tools
different RPC-like tools over RabbitMQ for Node.js Provides
There are two ways to run AMQPRPCServer/Client:
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
.server
gets the name of the queue before initialization and starts listening.client
gets the same name before initialization and uses it for sending requests.const amqplib = require('amqplib');
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
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);
return {server, client};
}
Full working example you could find here.
const amqplib = require('amqplib');
const {AMQPRPCServer, AMQPRPCClient} = require('@elasic.io/amqp-rpc');
async function init() {
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.
To register a new RPC command in the server, use addCommand()
method:
server.addCommand('hello', (name) => ({message: `Hello, ${name}!`}));
Handler could also return a promise or async function, e.g.:
server.addCommand('print-hello-world', (name) => Promise.resolve({ message: 'ok' });
To call an RPC command from the client, use sendCommand()
method:
const result = await client.sendCommand('print-hello-world', [
'World'
]);
Events receiver side code
const { AMQPEventsReceiver } = require('@elastic.io/amqp-rpc');
const amqp = require('amqplib')
.......
const amqpConnection = await amqp.connect('amqp://localhost');
const receiver = new AMQPEventsReceiver(amqpConnection);
receiver
.on('end', () => {
console.log('Sender stops to send events, so nothing to do more, disconnecting');
})
.on('close', () => {
console.log('Disconnected');
})
.on('error', (e) => {
console.log('Error happens', e);
})
.on('data', (msg) => {
console.log('We\'ve got a message', msg);
});
await receiver.start();
const queueName = receiver.queueName;
console.log(`Use ${queueName} as QUEUE_TO_SEND_EVENTS in sender part of code`);
........
await receiver.disconnect();
await amqpConnection.close();
Events source side code
const { AMQPEventsSender } = require('@elastic.io/amqp-rpc');
const amqp = require('amqplib')
.......
const amqpConnection = await amqp.connect('amqp://localhost');
const sender = new AMQPEventsSender(amqpConnection, 'QUEUE_TO_SEND_EVENTS');
sender
.on('close', () => {
console.log('Receiver endpoint has been removed, so sender stop to work');
})
.on('error', (e) => {
console.log('Error happens', e);
});
const data = {
key: 'value'
};
await sender.start();
await sender.send(data);
........
await sender.disconnect();
await amqpConnection.close();
FAQs
RPC over RabbitMQ for Node.js
The npm package @elastic.io/amqp-rpc receives a total of 0 weekly downloads. As such, @elastic.io/amqp-rpc popularity was classified as not popular.
We found that @elastic.io/amqp-rpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.