Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Created November 17, 2022 21:35
Show Gist options
  • Select an option

  • Save arantesxyz/663d89ac2fc0b2eaa67fda3caea4f2b1 to your computer and use it in GitHub Desktop.

Select an option

Save arantesxyz/663d89ac2fc0b2eaa67fda3caea4f2b1 to your computer and use it in GitHub Desktop.
mq rpc pattern test
const REPLY_QUEUE = 'amq.rabbitmq.reply-to';
const createClient = (settings) => amqp.connect(settings.url, settings.socketOptions)
.then((conn) => conn.createChannel())
.then((channel) => {
// create an event emitter where rpc responses will be published by correlationId
channel.responseEmitter = new EventEmitter();
channel.responseEmitter.setMaxListeners(0);
channel.consume(REPLY_QUEUE,
(msg) => channel.responseEmitter.emit(msg.properties.correlationId, msg.content),
{noAck: true});
return channel;
});
const sendRPCMessage = (channel, message, rpcQueue) => new Promise((resolve) => {
const correlationId = uuid.v4();
// listen for the content emitted on the correlationId event
channel.responseEmitter.once(correlationId, resolve);
channel.sendToQueue(rpcQueue, new Buffer(message), { correlationId, replyTo: REPLY_QUEUE })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment