Class RPCClient

See

This will create a single "client" Channel on which you may publish messages and listen for direct responses. This can allow, for example, two micro-services to communicate with each other using RabbitMQ as the middleman instead of directly via HTTP.

If you're using the createConsumer() helper, then you can reply to RPC requests simply by using the reply() argument of the ConsumerHandler.

Also, since this wraps a Channel, this must be closed before closing the Connection: RPCClient.close()

Example

// rpc-client.js
const rabbit = new Connection()

const rpcClient = rabbit.createRPCClient({confirm: true})

const res = await rpcClient.send('my-rpc-queue', 'ping')
console.log('response:', res.body) // pong

await rpcClient.close()
await rabbit.close()
// rpc-server.js
const rabbit = new Connection()

const rpcServer = rabbit.createConsumer({
queue: 'my-rpc-queue'
}, async (req, reply) => {
console.log('request:', req.body)
await reply('pong')
})

process.on('SIGINT', async () => {
await rpcServer.close()
await rabbit.close()
})

If you're communicating with a different rabbitmq client implementation (maybe in a different language) then the consumer should send responses like this:

ch.basicPublish({
routingKey: req.replyTo,
correlationId: req.correlationId,
exchange: ""
}, responseBody)

Hierarchy

  • RPCClient

Properties

Methods

Properties

active: boolean = true

True while the client has not been explicitly closed

Methods

  • Stop consuming messages. Close the channel once all pending message handlers have settled. If called while the Connection is reconnecting, then this may be delayed by acquireTimeout

    Returns Promise<void>

Generated using TypeDoc