@elastic.io/amqp-rpc

中文文档
2.1.0发布于 2018/09/05ISCCommonJS零依赖近 30 天下载 2.7万

RPC over RabbitMQ for Node.js

原始使用文档
文档目录3 节
  1. Getting Started
  2. RPC
  3. Event Emitter

amqp-rpc

different RPC-like tools over RabbitMQ for Node.js Provides

  • Simple rpc: AMQPRPCClient and AMQPRPCServer
  • Remote EventEmitter: AMQPEventsSender AMQPEventsReceiver

Getting Started

RPC

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:

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.

Example with permanent queue:

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.

Server handlers

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'
]);

Event Emitter

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();

版本与发布记录

@elastic.io/amqp-rpc 共发布 15 个版本,首次发布于 2017/09/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2018/09/058 年前
2.0.42018/09/048 年前
2.0.32018/03/228 年前
2.0.22018/03/228 年前
2.0.12018/01/168 年前
2.0.02018/01/128 年前

其他发布通道:dev → 2.0.0-dev.1。

关于 @elastic.io/amqp-rpc 的常见问题

@elastic.io/amqp-rpc 是什么?

RPC over RabbitMQ for Node.js

如何安装 @elastic.io/amqp-rpc?

在项目目录运行 npm install @elastic.io/amqp-rpc,或使用 pnpm add / yarn add / bun add。

@elastic.io/amqp-rpc 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/elastic.io__amqp-rpc 是否存在。

@elastic.io/amqp-rpc 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elastic.io/amqp-rpc 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8.4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@elastic.io/amqp-rpc 可以商用吗?

@elastic.io/amqp-rpc 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elastic.io/amqp-rpc 的下载量有多少?

根据 npm 官方下载统计,@elastic.io/amqp-rpc 在 2026/10/13 – 2026/11/11 这 30 天内被下载 26,755 次,日均约 892 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elastic.io/amqp-rpc 还在维护吗?

最新版本 2.1.0 发布于 2018/09/05(8 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/09/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。