-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Describe the bug
Using a Chromium browser, when the tab through which the connection is established is minimized for more than 5 minutes, the client gets disconnected by the server due to a ping timeout.
To Reproduce
Socket.IO server version: 4.1.3
Server
const http = require("http");
const httpServer = http.createServer();
const io = require("socket.io")(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
console.log("connected");
socket.on("disconnect", () => {
console.log("disconnected");
});
socket.on("message", (message) => {
console.log(new Date().toLocaleTimeString(), "Received:", message);
});
});
httpServer.listen(3000, () => {
console.log("listening on *:3000");
});Socket.IO client version: 4.0.1
Client
const socket = io("ws://localhost:3000", {transports: ["websocket", "polling"]});
setInterval(() => {
const now = new Date().toLocaleTimeString();
socket.send(now);
}, 5000);Server log:
listening on *:3000
connected
11:06:25 Received: 11:06:25
11:06:30 Received: 11:06:30
11:06:35 Received: 11:06:35
11:06:40 Received: 11:06:40
11:06:45 Received: 11:06:45
11:06:50 Received: 11:06:50
11:06:55 Received: 11:06:55
11:07:00 Received: 11:07:00
11:07:05 Received: 11:07:05
11:07:10 Received: 11:07:10
11:07:15 Received: 11:07:15
11:07:20 Received: 11:07:20
11:07:25 Received: 11:07:25
11:07:30 Received: 11:07:30
11:07:35 Received: 11:07:35
11:07:40 Received: 11:07:40
11:07:45 Received: 11:07:45
11:07:50 Received: 11:07:50
11:07:55 Received: 11:07:55
11:08:00 Received: 11:08:00
11:08:05 Received: 11:08:05
11:08:10 Received: 11:08:10
11:08:15 Received: 11:08:15
11:08:20 Received: 11:08:20
11:08:25 Received: 11:08:25
11:08:30 Received: 11:08:30
11:08:35 Received: 11:08:35
11:08:40 Received: 11:08:40
11:08:45 Received: 11:08:45
11:08:50 Received: 11:08:50
11:08:55 Received: 11:08:55
11:09:00 Received: 11:09:00
11:09:05 Received: 11:09:05
11:09:10 Received: 11:09:10
11:09:15 Received: 11:09:15
11:09:20 Received: 11:09:20
11:09:25 Received: 11:09:25
11:09:30 Received: 11:09:30
11:09:35 Received: 11:09:35
11:09:40 Received: 11:09:40
11:09:45 Received: 11:09:45
11:09:50 Received: 11:09:50
11:09:55 Received: 11:09:55
11:10:00 Received: 11:10:00
11:10:05 Received: 11:10:05
11:10:10 Received: 11:10:10
11:10:15 Received: 11:10:15
11:10:20 Received: 11:10:20
11:10:25 Received: 11:10:25
11:10:30 Received: 11:10:30
11:10:35 Received: 11:10:35
11:10:40 Received: 11:10:40
11:10:45 Received: 11:10:45
11:10:50 Received: 11:10:50
11:10:55 Received: 11:10:55
11:11:00 Received: 11:11:00
11:11:05 Received: 11:11:05
11:11:10 Received: 11:11:10
11:11:15 Received: 11:11:15
11:11:20 Received: 11:11:20
11:11:32 Received: 11:11:32
disconnected
Expected behavior
The server should keep receiving messages after 5 minutes even if the tab is minimized. Since browsers do some battery optimization, it is possible that the messages don't arrive at the correct interval, but the connection should still be kept open.
Platform:
- Device: Desktop PC
- OS: Windows 10
Additional context
In this case the problem is that the server default pingTimeout is not high enough to be able to correctly receive a pong response from the client if it has been minimized for more than 5 minutes, probably due to Chrome Intensive Throttling.
If I try to increase the pingTimeout interval from the server, I am able to keep the connection opened. By doing some experiments I found out that I can set it as low as 44 seconds for the client to be able to reply to the ping message.
Even if this is an expected behavior, shouldn't the default interval be higher? In any case the problem can be solved by setting it to 60000.