Reputation: 131
Getting this the following error:
0|animesoul | websocket.readyState = WebSocket.CLOSING;
0|animesoul | ^
0|animesoul | TypeError: Cannot set property 'readyState' of undefined
0|animesoul | at Socket.socketOnClose (/home/gibigbig/animesoul/app/node_modules/engine.io/node_modules/ws/lib/websocket.js:781:24)
0|animesoul | at emitOne (events.js:121:20)
0|animesoul | at Socket.emit (events.js:211:7)
0|animesoul | at TCP._handle.close [as _onclose] (net.js:557:12)
Not sure what file or event is causing the issue. I get that error randomly but it crashes my node process. I don't even know how to debug it because its not really telling me anything of any particular use. Googling didnt really give much insight, about 3 other people have a similar issue and i suspect it has to do with duplicate connections but I would like to confirm please.
I have 4 nodes of this app running in pm2 and it is being load balanced with nginx
app.js
express = require('express'),
exphbs = require('express-handlebars'),
client = require('./lib/discord.js'),
site = require('./global.js'),
serverio = require('http').createServer(app),
io = require('./lib/io'),
port = process.env.PORT ,
redis = require('redis'),
brooker = require('./broker')
let data = {};
var subscriber = redis.createClient([redacted]);
brooker();
subscriber.on("message", function (channel, message) {
messages = JSON.parse(message);
io.to(messages.room).emit(messages.event, messages)
});
client.on('ready', async () => {
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.set('env production');
app.use("*", function (req, res, next) {
return res.status(404).render('404', { data: data, user: req.user, layout: "main" });
});
io.attach(serverio);
serverio.listen(port, function () {
console.log('listening on *:' + port);
});
});
subscriber.subscribe("notifications");
process.on('SIGINT', async function () {
process.exit();
});
/lib/io.js
const io = require('socket.io')();
module.exports = io;
Upvotes: 0
Views: 1404
Reputation: 131
Update again:
Found an amazing package called "Longjohn" https://www.npmjs.com/package/longjohn
Which helped me get to the root of the problem. I no longer have any issue with this error. Sharing to save a life.
Upvotes: 0
Reputation: 6017
0|animesoul | websocket.readyState = WebSocket.CLOSING;
0|animesoul | ^
0|animesoul | TypeError: Cannot set property 'readyState' of undefined
0|animesoul | at Socket.socketOnClose (/home/gibigbig/animesoul/app/node_modules/engine.io/node_modules/ws/lib/websocket.js:781:24)
0|animesoul | at emitOne (events.js:121:20)
0|animesoul | at Socket.emit (events.js:211:7)
0|animesoul | at TCP._handle.close [as _onclose] (net.js:557:12)
To explain the error, not the cause:
This is a TypeError. Namely that you cannot set a property (readyState
) to undefined
or null
objects.
The error is displaying a stack trace, showing that the particular line of code is from file /home/gibigbig/animesoul/app/node_modules/engine.io/node_modules/ws/lib/websocket.js
. The presence of node_modules
shows it is the node modules, "engine.io"'s use of the "ws" library, and the line is 781. Might be a duplicate of this GitHub Issue?
The rest of the trace helps a little, but not much. It started in NodeJS (the net
library) on TCP close, which emitted something ("Socket.emit"), which was handled by the ws
library "Socket.socketOnClose". Best you can tell this exception occurred when a socket was closing.
You could also try adding an unhandledException handler to your process. You might get some more information that way, or decide the error is mundane and after logging, just ignore it! (Typically that is a bad idea, because it will hide other important errors).
Upvotes: 2