Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning when implementing RPC #426

Closed
cbuchert opened this issue Jun 5, 2018 · 1 comment
Closed

UnhandledPromiseRejectionWarning when implementing RPC #426

cbuchert opened this issue Jun 5, 2018 · 1 comment

Comments

@cbuchert
Copy link

cbuchert commented Jun 5, 2018

Hey, guys. Thanks for being so active in supporting this library.

I'm running into an issue with the solutions provided in this thread. I'm running RabbitMQ 3.7.4 in Docker on OSX High Sierra. All JS is being run in Node 8. When I try to run the solution published by @facundoolano or my own code, I get the same error:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'once' of undefined

I isolated the EventEmitter and got a similar TypeError around channel.sendToQueue. This leads me to believe that when channel gets returned it's not the object I was anticipating (it looks like a promise). Any ideas on this? Here's my own implementation of the code, suffering from the same ailments.

const EventEmitter = require("events").EventEmitter;
const amqp = require("amqplib");
const uuid = require("uuid");

class RpcClient {
  constructor(server, socketOptions, requestQueue) {
    this.requestQueue = requestQueue;
    this.responseQueue = "amq.rabbitmq.reply-to";
    this.channel = amqp.connect("amqp://" + server, socketOptions)
      .then(connection => connection.createChannel())
      .then(channel => {
        const consumerOptions = {noAck: true};

        channel.responseEmitter = new EventEmitter();
        channel.responseEmitter.setMaxListeners(0);
        channel.consume(this.responseQueue,
          (message) => channel.responseEmitter.emit(message.properties.correlationId, message.content),
          consumerOptions);

        return channel;
      });
  }

  sendMessage(message) {
    return new Promise(resolve => {
      const correlationId = uuid.v4();
      const messageBuffer = new Buffer.from(JSON.stringify(message), "utf8");
      const publishOptions = {
        correlationId,
        replyTo: this.responseQueue,
      };

      this.channel.responseEmitter.once(correlationId, resolveJSON);
      this.channel.sendToQueue(this.requestQueue, messageBuffer, publishOptions);

      function resolveJSON(message) {
        resolve(JSON.parse(message));
      }
    });
  }
}

module.exports = RpcClient;
@cbuchert
Copy link
Author

cbuchert commented Jun 5, 2018

Oh man. Noob mistake with promises. Closing.

@cbuchert cbuchert closed this as completed Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant