Implement request-response communication with RPCClient
masterThe RPCClient allows for request-response patterns between services using RabbitMQ.
- RPC Server: Create a
Consumeron a specific queue. The callback receives the request (req) and areplyfunction. Callingawait reply(data)sends the response back. - RPC Client: Create an
RPCClientand use.send(queue, message)to send a request and wait for the response.
// rpc-server.js
const rpcServer = rabbit.createConsumer({
queue: 'my-rpc-queue'
}, async (req, reply) => {
console.log('request:', req.body)
await reply('pong')
})
// rpc-client.js
const rpcClient = rabbit.createRPCClient({confirm: true})
const res = await rpcClient.send('my-rpc-queue', 'ping')
console.log('response:', res.body) // pong