mayur
mayur

Reputation: 117

How to check whether the connection is alive or not in RabbitMq node-amqplib library?

I am writing producer & consumer using rabbitMq node-amqplib library, I am afraid about suddenly to lost connection of server , How could I check whether the connection is alive or not ?

Upvotes: 9

Views: 10733

Answers (2)

Dmitri R117
Dmitri R117

Reputation: 2852

There are some options in doing this:

One way is using the heartbeat and an event listener, something like conn.on('close', (err) => { this.connected = false; } )

Or you can get into the connection object. There is a risk here as upgrades to amqplib may break this, as it's not part of the official interface: const connClosed = conn.connection['expectSocketClose']

There are other properties inside the connection object that also can tell you if it's closed, like stream writeable state (could be a false flag) or the heartbeater object.

Upvotes: 0

Jesferman
Jesferman

Reputation: 1084

AMQP 0-9-1 offers a heartbeat feature to ensure that the application layer promptly finds out about disrupted connections (and also completely unresponsive peers).

In amqplib you only need to set a heartbeat timeout (non 0) when you call connect([url, [socketOptions]]) and the check will be performed automatically.

More info here:

https://www.squaremobius.net/amqp.node/channel_api.html#heartbeating

http://www.rabbitmq.com/heartbeats.html

Upvotes: 4

Related Questions