Created
November 17, 2022 21:35
-
-
Save arantesxyz/663d89ac2fc0b2eaa67fda3caea4f2b1 to your computer and use it in GitHub Desktop.
mq rpc pattern test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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