1

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

Vishal Bisht
  • 107
  • 2
  • 7
  • 3
    The error is telling you exactly what your problem is. You are most likely not re-using connection and channel instances in your application, but creating new ones for each message. You also don't provide enough information for people to assist - RabbitMQ, Erlang and operating system version, as well as all of your code or at least the parts that deal with RabbitMQ. – Luke Bakken Jul 16 '18 at 17:49

2 Answers2

3

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')
vanduc1102
  • 5,769
  • 1
  • 46
  • 43
1

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

Wyck
  • 10,311
  • 6
  • 39
  • 60
Eson Jia
  • 11
  • 1