Reputation: 117
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
Upvotes: 1
Views: 4781
Reputation: 6255
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')
Upvotes: 3
Reputation: 11
The maximum number of channels you can allocate is negotiated with rabbitmp server. In my server, this value is 2047. You can debug you amqplib/lib/connection.js to get this.
Upvotes: 1