Reputation: 117
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
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
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