Connection does not close after the timeout expired
Hello everyone! First of all, thank you for this library, saves me a ton of time. So I tried to use the heartbeat feature. On version 4.11.1 I experienced that the "ping" message doesn't send therefore it just timeouts because there is no response from the server (because the server doesn't receive a "ping" message), so I downgraded to version 4.8.1 (https://github.com/robtaussig/react-use-websocket/issues/255) and it worked. But now after heartbeat timeouts and there are no responses from the server nothing happens. I expect a closing connection after heartbeat timeouts, am I missing something or do I understand the logic wrong?
This is my options with heartbeat:
shouldReconnect: (closeEvent) => {
return (
shouldConnect &&
reconnectAttemptsRef.current < RECONNECT_INTERVALS.length
);
},
reconnectAttempts: RECONNECT_INTERVALS.length,
reconnectInterval: (attemptNumber) => {
reconnectAttemptsRef.current = attemptNumber + 1;
setCurrentReconnectAttempt(attemptNumber + 1);
return (
RECONNECT_INTERVALS[attemptNumber] ||
RECONNECT_INTERVALS[RECONNECT_INTERVALS.length - 1]
);
},
onClose: () => {
console.log(
`WebSocket closed. Attempt ${reconnectAttemptsRef.current} of ${RECONNECT_INTERVALS.length}`
);
if (restOptions?.onClose) {
restOptions.onClose();
}
},
heartbeat: {
message: JSON.stringify({ action: "ping", request_id: "0" }),
returnMessage: JSON.stringify({
errors: [],
data: { message: "Pong!" },
action: "ping",
response_status: 200,
request_id: "0",
}),
timeout: restOptions?.heartbeatTimeout || HEARTBEAT_TIMEOUT,
interval: restOptions?.heartbeatInterval || HEARTBEAT_INTERVAL,
},
The "ping" message sends and even if there is no response from the server - it sends the next "ping" message and then just stops and does nothing.
Also, I noticed that after the connection was restored (and "ping" messages were without responses) the WebSocket reconnected automatically, so I suppose it still closes the connection after heartbeat timeouts but maybe doesn't change the connection status. This is just an assumption anyway because I don't have a lot of experience with sockets
I want to work on this issue
Sorry for not responding — have been very busy lately. @deeseeker , I would be very appreciative of any help/contributions made for this feature!
- Sent from mobile. Please forgive brevity.
On Thu, Jul 10, 2025 at 7:58 AM Qudus Adeyemi @.***> wrote:
deeseeker left a comment (robtaussig/react-use-websocket#260) https://github.com/robtaussig/react-use-websocket/issues/260#issuecomment-3057146618
I want to work on this issue
— Reply to this email directly, view it on GitHub https://github.com/robtaussig/react-use-websocket/issues/260#issuecomment-3057146618, or unsubscribe https://github.com/notifications/unsubscribe-auth/AELLKK7JUJPODW4JWV6NO4T3HZIPTAVCNFSM6AAAAABVLU2STGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTANJXGE2DMNRRHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>