I've found it to be common for WebSockets clients to be required to issue pings and reply to pongs, or if not required to do so, want to do so in order to detect disconnects. In the interest of making this common chore easier for everyone to do, it would be nice for Spring to include PingPongWebSocketHandlerDecorator
making it easy to implement that functionality.
Comment From: candrews
@rstoyanchev can you please take a look at this PR?
Comment From: rstoyanchev
@candrews this is for a heartbeat mechanism? It can work fine for an application but I'm afraid it is a bit incomplete to promote at the framework level where it would likely need to be extended over time to address more needs.
Heartbeats are usually supported at a higher level, e.g. STOMP or RSocket, where it is built into the protocol, it is bi-directional, and allows each side to know how frequently the other side wants to receive. This mutual coordination is important because each side then knows how frequently it needs to write, and when to decide the other side is no longer connected.
In such implementations, heartbeats are avoided if the application is already sending and receiving messages at the desired frequency, thus avoiding additional traffic and the possibility for a pong to get jammed behind other application messages. An implementation could also be improved to use a single task that wakes up as frequently as necessary (depending on the shortest heartbeat duration) to check on all sessions vs having as many tasks as the number of sessions, which when this is used on the server side becomes a more important issue.
While the implementation can be improved, I'm afraid I don't see a good general solution can only come with some form of bi-directional agreement which can be done with a higher level protocol or some convention.