I am using socket io and node for my chat application. There are two problems
- 1) Continuous increase in RAM usage by socket
- 2) I used pm2 to monitor socket, it starts and working perfectly, RAM usage increases and it goes above 100MB and after that socket stops and pm2 restarts socket automatically then RAM usage shows nearly 50MB and user is not able to send the message.
socket.on('iAmOnline', function(data){
if(typeof usernames[data.id] != 'undefined'){
delete usernames[data.id];
delete onlineClients[data.id];
if(typeof isChatScreen[data.id] != 'undefined'){
delete isChatScreen[data.id];
}
}
socket.username = data.id;
usernames[data.id] = data.id
onlineClients[data.id] = socket.id;
if(typeof data.isChatScreen != 'undefined'){
isChatScreen[data.id] = data.isChatScreen;
}else{
isChatScreen[data.id] = 1;
}
if(typeof usernames[data.id] != 'undefined'){
var allChats = getChats(data.id, function(chats) {
var a = chats.pendingMessage;
var b = chats.deliveredMessage;
var c = chats.readMessage;
for(var i=0;i<a.length;i++){
socket.emit("message", {'from':a[i]['userAId'],'to':a[i]['userBId'], 'msg':a[i]['message'],'key':a[i]['uniqueKey']});
}
for(var i=0;i<b.length;i++){
socket.emit("delivered", {'from':b[i]['userBId'],'to':b[i]['userAId'], 'localId':0,'key':b[i]['uniqueKey']});
}
for(var i=0;i<c.length;i++){
socket.emit("read", {'from':c[i]['userBId'],'to':c[i]['userAId'], 'localId':0,'key':c[i]['uniqueKey']});
}
});
allChats = null;
}
});
function getChats(userId, callback){
return request({
uri: "http://twango.me/api/v1/chats?userId="+userId,
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
callback(JSON.parse(body).response);
});
}