'Setting Heartbeat when connecting to RabbitMQ via Amqplib

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?



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Beginer
Solution 2