Reputation: 1095
im writing my first chat app in nodejs , my app basically keeps track of online users and put them in the rooms to chat via webrtc ...im using pm2 to run the chat server
so i noticed sometimes when i close browser tabs my app will crash and pm2 will reload the server so i checked the log and saw this error
0|chat | Error: read ECONNRESET
0|chat | at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20) {
0|chat | errno: -104,
0|chat | code: 'ECONNRESET',
0|chat | syscall: 'read'
0|chat | }
here is the error when runing withouth pm2
node:events:368
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20)
Emitted 'error' event on TLSSocket instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}
to make sure it's not my code causing this error i removed all the codes in my app and just put 2 simple function to listen to online(called by online client via socket.io) and disconnect(called automatically by user disconnect ) events ... and sure enough after closing and opening some tabs it happen again here is simplified version of my code
im using Express.io
which is a combination of Express and Socket.io
const env = require('dotenv').config({ path: '.env' })
const https = require('https');
const fs = require('fs');
app = require('express.io')();
var options = {
key: fs.readFileSync( env.parsed.SSL_KEY),
cert: fs.readFileSync(env.parsed.SSL_CERT)
};
app.https(options).io();
console.log('|-> protocol : https ');
app.io.route('online' , function (req){
console.log(`| online socket -> ${req.socket.id}`);
})
app.io.route('disconnect', function(req) {
console.log(`|->****************** disconnect : ${req.socket.id}`);
});
var PORT = env.parsed.PORT || 8080
console.log(`|-> listining to port ${PORT} `);
app.listen(PORT );
i've searched online and know why it's happening (i think) but i dont have any data communication in this code it .... i've no idea how to handle this and appreciate any help
here is the complete log you can see it happening after closing some tabs
/root/.pm2/logs/chat-out.log last 15 lines:
0|chat | |-> protocol : https
0|chat | |-> listining to port 8080
/root/.pm2/logs/chat-error.log last 15 lines:
0|chat | (node:19821) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
0|chat | (Use `node --trace-deprecation ...` to show where the warning was created)
0|chat | | online socket -> Z2z7m6D9OXKPGRVhbz4R
0|chat | |->****************** disconnect : H_1-d7sIm2oIvxzzbz4Q
0|chat | | online socket -> JrnLWbqN4luBtYg4bz4S
0|chat | | online socket -> ZSY1wIywMEdmUcALbz4T
0|chat | | online socket -> ugSg08DksJ73ld1Rbz4U
0|chat | |->****************** disconnect : ugSg08DksJ73ld1Rbz4U
0|chat | |->****************** disconnect : ZSY1wIywMEdmUcALbz4T
0|chat | Error: read ECONNRESET
0|chat | at TLSWrap.onStreamRead (node:internal/stream_base_commons:220:20) {
0|chat | errno: -104,
0|chat | code: 'ECONNRESET',
0|chat | syscall: 'read'
0|chat | }
PM2 | App [chat:0] exited with code [1] via signal [SIGINT]
PM2 | App [chat:0] starting in -fork mode-
PM2 | App [chat:0] online
0|chat | |-> protocol : https
0|chat | |-> listining to port 8080
0|chat | (node:19864) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
0|chat | (Use `node --trace-deprecation ...` to show where the warning was created)
Upvotes: 5
Views: 6982
Reputation: 17506
It's hard to figure out the problem without seeing the client code but my bet is by declaring this route
app.io.route('disconnect', function(req) {
console.log(`|->****************** disconnect : ${req.socket.id}`);
});
you are misusing the express.io
library:
https://github.com/techpines/express.io/tree/master/lib#reserved-events
These events are reserved and should not be used with
app.io.route
orreq.io.on
unless you know what you are doing.
- connect
- connecting
- disconnect
- connect_failed
- error
- message
- reconnect_failed
- reconnect
- reconnecting
BTW like another comment said, this library is old and you should not use it. It uses Socket.IO v0.9 and the current version is v4.
Upvotes: 0