After working the rabbit mq workers in pub sub pattern for some time i am getting an error in creating an channel.
Error: No channels left to allocate
After working the rabbit mq workers in pub sub pattern for some time i am getting an error in creating an channel.
Error: No channels left to allocate
If you are using https://www.npmjs.com/package/amqplib, you can use Promise to share a channel while publishing multiple messages
in message-queue.js
const q = 'tasks';
const open = require('amqplib').connect('amqp://localhost');
const channelPromise = open.then((conn) => conn.createChannel());
// Publisher
function publishMessage(message) {
channelPromise.then((ch) => ch.assertQueue(q)
.then((ok) => ch.sendToQueue(q, Buffer.from(message))))
.catch(console.warn);
}
// Consumer
open.then((conn) => conn.createChannel())
.then((ch) => ch.assertQueue(q).then((ok) => ch.consume(q, (msg) => {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
}
}))).catch(console.warn);
module.exports = {
publishMessage,
};
some-where.js
messageQueue.publishMessage('hello world')