Vishal Bisht
Vishal Bisht

Reputation: 117

Rabbit MQ amqplib 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

Upvotes: 1

Views: 4781

Answers (2)

vanduc1102
vanduc1102

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

Eson Jia
Eson Jia

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.

enter image description here

Upvotes: 1

Related Questions