question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JS exceptions thrown while just idling (WebSocket is already in CLOSING or CLOSED state.)

See original GitHub issue

You 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.

socket-io-errors

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:closed
  • Created 5 years ago
  • Reactions:86
  • Comments:59 (3 by maintainers)

github_iconTop GitHub Comments

95reactions
omardomacommented, Dec 18, 2018

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:

const http = require('http');
const server = http.createServer();
const io = require('socket.io')(server, {
  pingTimeout: 60000,
});

OR

const io = require('socket.io')();
const http = require('http');
const server = http.createServer();
io.attach(server, {
  pingTimeout: 60000,
});
55reactions
provokateurincommented, Jul 29, 2018

I fixed it for me:

Previously I used this:

socket.on('ping', alert);

But then I changed it to this:

socket.on('ping', msg => {
    alert(msg);
});

And it worked!

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to catch and deal with "WebSocket is already ...
i resolved this by checking the readystate property; if it equals to 3, close and reinitialize the websocket connection. then do a while...
Read more >
Websocket Is Already In Closing Or Closed State - Html
Emitted when the underlying socket times out from inactivity. This only notifies that the socket has been idle. The request must be aborted...
Read more >
websocket is already in closing or closed state - You.com
I try to do WebSocket with Javascript, PHP, and Wamp but I have the error "WebSocket is already in CLOSING or CLOSED state"...
Read more >
JS exceptions thrown while just idling (WebSocket is already in ...
JS exceptions thrown while just idling (WebSocket is already in CLOSING or CLOSED state.)
Read more >
WebSocket is already in CLOSING or CLOSED state.
The log message seems to indicate that the client side javascript is attempting to close the socket when in fact the server has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found