1

I am handling nodejs requests through RabbitMQ. My producer receives requests through nodejs route and sends them to consumer, which then creates a document in the db by the data received from the request. Here is my route

router.post("/create-user", async(req: Request, res: Response) => {
  const msg = JSON.stringify(req.body);
  const send = await Producer(msg);
});

Here is my Producer class

import amqp from "amqplib/callback_api";

export async function Producer(message: string) {
  amqp.connect("amqp://localhost", (error0, connection) => {
    if (error0) {
      throw error0;
    }
    connection.createChannel((error1, channel) => {
      if (error1) {
        throw error1;
      }

      let queue = "hello";

      channel.assertQueue(queue, {
        durable: false,
      });
      channel.sendToQueue(queue, Buffer.from(message));
      console.log(" [x] Sent %s", message);
    });
  });
}

And my consumer

import amqp from "amqplib/callback_api";
import {
  User
} from "../models/user";

export class ConsumerClass {
  public async ConsumerConnection() {
    amqp.connect("amqp://localhost", (error0, connection) => {
      if (error0) {
        throw error0;
      } else {
        this.ConsumerTask(connection);
      }
    });
  }

  public async ConsumerTask(connection: amqp.Connection) {
    connection.createChannel((error1, channel) => {
      if (error1) {
        throw error1;
      }

      let queue = "hello";

      channel.assertQueue(queue, {
        durable: false,
      });

      channel.prefetch(1);

      console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);

      channel.consume(queue, async(msg) => {
        console.log(" [x] Received %s", msg.content.toString());
        const data = JSON.parse(msg.content.toString());
        const user = new User({
          name: data.name,
          phone: data.phone,
          company: data.company,
        });
        await user.save();
      }, {
        noAck: true,
      });
    });
  }
}

I want to send the Json document of the user created from the consumer to the route so that the client can get the created user as a response. How can i achieve this and what am i doing wrong?

Gabriel
  • 99
  • 1
  • 13
  • What's your error and what situation are you in? – sunsets Feb 21 '20 at 08:53
  • @sunsets No errors. question is pretty obvious. I just want to send the `user` in consumer to route so that i can res.send it. Right now the consumer creates the record in the db. – Gabriel Feb 21 '20 at 09:04

1 Answers1

1

What you want is a response event from the consumer to the producer. Now, this is where you can create a function that acts as a Remote Procedure Call.

Thus instead of two events, there will be 2 events e1 and e2. Here's a small diagram to explain this stuff ( disclaimer - I am bad at drawing). I guess you can manage the coding part of this.

enter image description here

damitj07
  • 2,689
  • 1
  • 21
  • 40
  • I implemented RPC and although it works for one creation, the second time i tried to create a user, the producer sent the message but the consumer didn't catch it. I want my consumer to be always on listening to producer and acting on the data as soon as it arrives. – Gabriel Feb 21 '20 at 11:04
  • refer to this for more details to implement RPC. https://www.rabbitmq.com/tutorials/tutorial-six-javascript.html – damitj07 Feb 21 '20 at 11:21