8

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 ?

mayur
  • 107
  • 1
  • 7
  • Connect with RabbitMQ Web UI Server. You can achieve this with the plugin. This might help: https://www.rabbitmq.com/management.html There you can see bindings between producer & consumer. – Zhivko.Kostadinov Apr 30 '18 at 10:32

2 Answers2

4

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

Jesferman
  • 1,049
  • 7
  • 12
  • Thank you so much for your prompt reply ,,kindly mention one more thing , could I keep connection alive with server by using heartbeat or by any other exclusive way that I can implement? – mayur Apr 30 '18 at 11:07
  • You can only keep the connection alive using heartbeats (it is the way RabbitMQ works). Note that every message you send or receive also count as a heartbeat. So if you send arbitrary messages, the connection will be kept alive. – Jesferman Apr 30 '18 at 11:13
0

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.

Dmitri R117
  • 2,502
  • 23
  • 20