Skip to content

Improved websocket reconnection logic #1473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

jonstaff
Copy link

@jonstaff jonstaff commented Feb 2, 2024

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: the SocketManager)
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 to
specify 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.

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: the `SocketManager`)
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 to
specify 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.
See this issue for more information: socketio#1470
@jonstaff
Copy link
Author

Closing this PR since it doesn't seem likely to get merged and I've started to add additional changes into my fork that are out of the scope of this PR (and somewhat specific to my project). Hopefully this is useful to others who've encountered similar issues.

@jonstaff jonstaff closed this Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant