amqp-client.js icon indicating copy to clipboard operation
amqp-client.js copied to clipboard

Make time duration since last server data available

Open ngbrown opened this issue 2 years ago • 4 comments

With my WebSocket connection, I needed the client to re-connect when the connection wasn't active anymore. Setting the heartbeat would ensure the server sent something at a regular interval, but the application had no way to know when the heartbeats were.

This was the solution I came up with, on every received message, store the current performance.now() value, a monotonic millisecond timer. It had to be within this library, because the only message during the time period might be the heartbeat message. Then the application can check if it has been significantly greater than the heartbeat time since the last received message, and attempt to re-connect.

ngbrown avatar Jul 25 '23 02:07 ngbrown

Shouldn't the heartbeat functionality be built out instead and automatically disconnect if no heartbeat frame is received within the acceptable time frame?

carlhoerberg avatar Jul 25 '23 14:07 carlhoerberg

... is it possible to cover it with some test?

Good point, I'll work on a viable test.

Shouldn't the heartbeat functionality be built out instead and automatically disconnect if no heartbeat frame is received within the acceptable time frame?

At the time this was the simplest diff I could patch-in to do what I needed, while detecting disconnects and re-connecting continued to be handled within the app. My application code (in the browser) manages creating and disposing a timer that checks both for closed connections and missed heartbeats. If this timer was moved into the library, a more involved event model between the library and the application would need to be built out.

The .NET client, for example, has an even more involved model of also reconnecting when disconnected, I don't think reconnecting within this library would work easily in my application's case. The application is connecting to RabbitMQ streams, so needs to update x-stream-offset argument to the last value it saw to effectively resume from where it left off. If reconnecting was going to be internalized into the library, then directly supporting streams parameters would be needed.

ngbrown avatar Jul 25 '23 15:07 ngbrown

Did you perform any benchmarks with this? How expensive is performance.now()?

The client might not need to reconnect automatically, but socket could be closed and onerror could be called if a missed heartbeat is detected. Then there should be no need to expose this, right?

carlhoerberg avatar Jul 26 '23 01:07 carlhoerberg

The client might not need to reconnect automatically, but socket could be closed and onerror could be called if a missed heartbeat is detected. Then there should be no need to expose this, right?

Sounds like that would address https://github.com/cloudamqp/amqp-client.js/issues/95?

dentarg avatar Dec 04 '23 08:12 dentarg

Closing this since we did adress #95

baelter avatar Sep 11 '25 09:09 baelter