feat(pubsub): Implement Ping/Pong Mechanism to Improve Connection Reliability #3845
+111
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Both the blocking and nonblocking
PubsubClient
clients currently lack a mechanism to detect when the WebSocket connection becomes unresponsive or stale. Without periodic health checks, the clients may not realize that the server is no longer responding. This can lead to missed messages and unreliable subscriptions. This can affect applications relying on WebSockets to provide real-time data streamsSummary of Changes
This PR aims to implement a ping/pong mechanism in both the blocking and nonblocking
PubsubClient
clients to improve connection reliabilityChanges
Ping
message sent to the server usingtokio::time::interval
Pong
messages and reset theunmatched_pings
counter upon receiving any messageunmatched_pings
counter exceedsDEFAULT_MAX_FAILED_PINGS
DEFAULT_PING_DURATION_SECONDS
andDEFAULT_MAX_FAILED_PINGS
, as making them configurable would've meant changing the existing subscription parameters resulting in a breaking change. However, these params could easily be changed in the future to become configurable—the importance here is introducing reliable health checks to WebSocket connections