4

When connecting to RabbitMQ via amqplib, the value of the Heartbeat parameter cannot be set.

I do this:

// NPM Modules
const amqp = require('amqplib/callback_api');
const withAutoRecovery = require('amqplib-auto-recovery');

// Application configuration
const config = settings.getConfig();

withAutoRecovery(amqp, {
  onError: (err) => { console.log(err.message); },
  isErrorUnrecoverable: (err) => { console.log(err) }
}).connect(config.rabbitmq, (err, connection) => {
    // If there are connection errors
    if (err) {
        console.error(this.conn_str_amqp);
        console.error('[AMQP] Connection error:', err.message); 
    } else if (connection) {
        // Connection Error Event
        connection.on('error', (err) => {
            if (err.message !== 'Connection closing') {
                console.error('[AMQP] Closing connection', err.message);
            }
        });
        // Error event when you close the connection
        connection.on('close', () => {
            console.error('[AMQP] Closing connection');
        });
        // Hint to user
        console.log('[AMQP] Connected');
    }
});

Here config.rabbitmq is:

{
  protocol: 'amqp',
  hostname: 'localhost',
  port: 5672,
  username: 'guest',
  password: 'guest',
  locale: 'en_US',
  frameMax: 0x1000,
  heartbeat: 1800,
  vhost: '/',
}

I expect to get the Heartbeat = 1800s parameter value, in the UI Managment window the default value Heartbeat = 60s is displayed.

Screenshot: enter image description here

I tried to pass another object instead config.rabbitmq line (https://www.rabbitmq.com/uri-spec.html and https://www.rabbitmq.com/uri-query-parameters.html) in the format:

function getRabbitMQConnectionString() {
    const rabbitmq = config.rabbitmq;
    return `${rabbitmq.protocol}://${rabbitmq.username}:${rabbitmq.password}@${rabbitmq.hostname}:${rabbitmq.port}${encodeURIComponent(rabbitmq.vhost)}?heartbeat=${rabbitmq.heartbeat}`;
}

Thus, too, it turned out similar to the above effect.

Tell me, please, how to correctly set the Heartbeat parameter through the Node.js client application on amqplib?

fedor-sg
  • 219
  • 3
  • 12

3 Answers3

0

For some reason I set up the heartbeat to 30 s and it worked.

Beginner
  • 151
  • 2
  • 6
0

From RabbitMQ documentation, they suggest not to set the heartbeats high, because it can disable them. However, I don't know what is shown as 60s heartbeats.

https://www.rabbitmq.com/heartbeats.html#disabling

Ivgesha
  • 1
  • 1
0

Reason

The reason of getting heartbeat 60s in Management UI is because the RabbitMQ server have default heartbeat 60s.

Explanation

And during connection creation the minimum of both client(amqplib) and server(RabbitMQ) heartbeat value is taken.

Solution

So, to increate the heartbeat value from amqplib, we need to have more or equal value set in RabbitMQ configurations

Methods to Set heartbeat value for RabbitMQ Server

  1. How to change RabbitMQ Heartbeat without restart
  2. https://www.rabbitmq.com/configure.html#:~:text=Max%20value%3A%20536870912-,heartbeat,-Value%20representing%20the

Reference for more detail