Improved websocket reconnection logic #1473
Closed
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.
This PR contains two primary improvements:
First, a configurable timeout was added for when the
SocketEngine
initially attempts to establish a connection via websocket. Without this
timeout in place, the
SocketEngineClient
(read: theSocketManager
)doesn't get properly informed of the hanging connection and wastes its
reconnect attempts. 10 seconds was chosen as the default timeout based
on extensive testing with poor network conditions, but can be modified
based on the requirements of the app using this library if necessary.
Second, a configurable heartbeat mechanism was added to the
SocketEngine
implementation. This allows consumers of this library tospecify a socket endpoint to use for heartbeat requests. Since
Socket.IO's ping/pong implementation was reversed in v3+ of the
protocol, clients no longer have a liveness check built in. The ping and
pong events are reserved for Socket.IO, so this allows you to specify a
different (hopefully very simple and very performant) event that can be
used for the same purpose.
Instead of mirroring the ping/pong logic exactly, the heartbeat is
designed to only beat when other packets aren't being transmitted.
Meaning, if your socket is already actively receiving data, there's no
reason to perform a heartbeat, since it's obviously connected. This is
done to reduce the load on the server.