We built a chat in nodejs/socketIO and it works well. We use pm2 to start it. Today we faced an issue and chat crashed :-(
All messages are saved in json file (so in json format) in using :
// reading
if (fs.existsSync(dataPathFile)) {
this.messages = JSON.parse(fs.readFileSync(dataPathFile, "UTF-8"));
} else {
this.messages = [];
}
}
...
// recording
fs.writeFile(dataPathFile, JSON.stringify(this.messages), "UTF-8", function (err) {
if (err) throw err;
});
return message;
...
logs give us :
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at new Message (/appli/chatvideodiff/server/utils/message.js:11:28)
=> line 11 is reading line "this.messages"
We have no idea what could be wrong but when issue appears :
(1) : in logs we have
PM2 | [PM2][WORKER] Process 0 restarted because it exceeds --max-memory-restart value (current_memory=2001776640 max_memory_limit=1073741824 [octets])
We increased "max_memory_limit" to "14000" (14 GB as we have 16 GB) but it crashes also
We planned to switch from json file to database but if you have an idea to fix issue faster, we are for sure interested.
Thanks for your help Niko
if that can help, messages are like that :
{
"from": "Niko",
"text": "test niko",
"room": "03a74",
"ip": "1.1.1.1",
"color": "#5073C7",
"id": "ea1c86d8-6f8e-ddce-d525-925e4a6da861",
"createdAt": 1637769784314
}