JS exceptions thrown while just idling (WebSocket is already in CLOSING or CLOSED state.)
See original GitHub issueYou want to:
- report a bug
- request a feature
Current behaviour
While leaving a socket connected, but doing no other activity in my program, I see intermittently (but fairly consistently) an exception thrown in browser console, from inside of the socket.io machinery (specifically in backo2/index.js
line 83. That error is:
WebSocket is already in CLOSING or CLOSED state.
Steps to reproduce (if the current behaviour is a bug)
I have two tabs open with client sockets connected to the same server (localhost) via https. Both clients are sitting idle with nothing else happening in browser or server (except for whatever keep-alive pings socket.io is doing). Both of them are joined to a single channel (via join(..)
on the server). Otherwise, nothing else special.
Here’s how I create the server socket instance:
var httpsServer = https.createServer(..);
var io = require("socket.io")(httpsServer);
io.on("connection",onSocketConnection);
And in the client:
socket = io();
socket.on("connect",function(){
console.log("socket connected");
});
socket.on("disconnect",function(){
console.log("socket disconnected");
});
Expected behaviour
I expect disconnections and reconnections from time to time, but I don’t expect spurious JS exceptions thrown by the library when I’m not doing anything else over the connections.
Setup
- OS: Mac OSX
- browser: Chrome 66, node 10.2.1
- socket.io version: 2.1.1
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Expanded stack trace:
index.js:83 WebSocket is already in CLOSING or CLOSED state.
(anonymous) @ index.js:83
e.encodePacket @ index.js:83
(anonymous) @ index.js:83
r.write @ index.js:83
r.send @ index.js:83
r.flush @ index.js:83
r.sendPacket @ index.js:83
r.ping @ index.js:83
(anonymous) @ index.js:83
setTimeout (async)
r.setPing @ index.js:83
r.onPacket @ index.js:83
(anonymous) @ index.js:83
r.emit @ index.js:83
r.onPacket @ index.js:83
r.onData @ index.js:83
ws.onmessage @ index.js:83
Issue Analytics
- State:
- Created 5 years ago
- Reactions:86
- Comments:59 (3 by maintainers)
Top GitHub Comments
There is a change for the default pingTimeout from 60000(v2.0.4) to 5000 (v2.1.0+) which is not enough for some browsers like Chrome.
The solution to this issue on v2.1.0+ including the latest v2.2.0 is to override the default pingTimeout on your server to a large value as follows:
OR
I fixed it for me:
Previously I used this:
But then I changed it to this:
And it worked!