0

I have a question about RabbitMQ with nodeJS.

I have 3 microservices : gateway-ms, event-ms and account-ms. the gateway is simple a proxy who redirects requests to corresponding microservice (account or event).

I would like, in a GET request inside event-ms, get some datas from acount-ms and answer it back to event-ms.

I'd like to do something like that :

let channel, connection

async function connectQueue() {
    try {        
      connection = await amqp.connect("amqp://localhost:5672");
        channel    = await connection.createChannel()
        
        await channel.assertQueue("event-queue")
        ...
        ...
 }

connectQueue()

// Here 

app.get('/events', async (req, res) => {
  
  // I get the event from the database containing account ids as participants
  const event == await model.getEventById.....

  // events are for example : [{id: 1, participants: [5,6,7]}]
  // Here I want to ask account-ms to get the firstname, lastname of participants ids from below
  
  // I would like to do something like that, with a returned result :
  await result = channel.sendToQueue("account-queue", Buffer.from(JSON.stringify({
    message: 'getAccountInfos',
    variables: [5,6,7]
  }))) 
  
  // results are the account infos
  // result : [{id: 5, firstname: 'toto', lastname: 'tutu'}, ...]
})

Is it possible, or any other way to get back an answer from the 'sendToQueue' ? I didnt find much about it.

I know, I can use the channel.consume("event-queue") just after the sendToQueue, but the consumer stays open after, and I don't want to disconnect/reconnect every time I want to send/receive a message.

I would like to get the answer in the app.get('/events') block. Do you have any example ?

Thank you in advance

0 Answers0