node.js application suddenly loads CPU at 100% and hangs - node.js

I have simple application on node.js running on linux virtual machine. It listens for tcp messages and sending them to the clients using socket.io library. And after some time of low CPU usage it suddenly starts to load CPU higher and higher until application hangs up. The script is simple and I can not understand what's wrong with it.
var net = require('net');
var io = require('socket.io').listen(socketPort);
net.createServer(function (socket) {
socket.setEncoding("utf8");
socket.on('data', function (dataStr) {
console.log("TCP dataStr " + dataStr);
var data = JSON.parse(dataStr);
io.sockets.in(data.room).emit('publish', data);
});
}).listen(tcpPort);
io.sockets.on('connection', function (socket) {
socket.on('subscribe', function (room) {
console.log('subscribe room ' + room);
if (Array.isArray(room)) {
var i;
for (i = 0; i < room.length; i++) {
console.log('subscribe join room ' + room[i]);
socket.join(room[i]);
}
} else if (typeof room === 'string') {
console.log('subscribe join room ' + room);
socket.join(room);
}
});
socket.on('unsubscribe', function (room) {
console.log('unsubscribe room ' + room);
if (Array.isArray(room)) {
var i;
for (i = 0; i < room.length; i++) {
console.log('unsubscribe leave room ' + room[i]);
socket.leave(room[i]);
}
} else if (typeof room === 'string') {
console.log('unsubscribe leave room ' + room);
socket.leave(room);
}
});
});
Also with cluster module I tried to run multiple workers that communicate with clients. And every worker after some time hangs own CPU core at 100% with time difference in about a minute.
UPD: Client code (run in browser):
socketObj = new function() {
var that = this;
that.socket;
that.init = function(nodeServerUrl, rooms, onPublishFunc) {
that.socket = io.connect(nodeServerUrl);
that.socket.emit('subscribe', rooms);
that.socket.on('publish', function(data) {
onPublishFunc(data);
});
};
that.subscribe = function(room) {
that.socket.emit('subscribe', room);
};
that.unsubscribe = function(room) {
that.socket.emit('unsubscribe', room);
};
}
...
try {
socketObj.init('application url', ["room1", "room2"], nodeJsCallback);
} catch(err) {
}
...
nodeJsCallback = function(jsonData) {
//Only updates data on UI, no subscribing, unsubscribing, emitting etc.
...
}
UPD2: I tried to reproduce the problem with synthetic tests on production machine and on my local Windows machine. I have done some stress testing:
Multiple client socket connections
Multiple static data downloads (socket.io script for browser)
Increased frequence of tcp updates.
After few hours of testing I failed to reproduce. But when it is running on production with real users, it is hanging up earlier or later.
I'm starting to think this is either environment or specific message problem. Probably next things I'll try are:
Update Node.js to current version
Try to log all data transfer and replay it later hoping hanging will reproduce

Changed Nodejs from version v0.10.4(Stable) to v0.11.2(Unstable). All woking good so far, consuming 1-2% CPU. Now we are testing on v0.10.8(Stable).
UPD On v0.10.8 application is stable too.
Even though the problem dissapeared on v0.10.4(Stable), it is still very strange and discouraging.

Related

Socket.io: How to correctly join and leave rooms

I'm trying to learn Socket.io by building a set of dynamically created chatrooms that emit 'connected' and 'disconnected' messages when users enter and leave. After looking at a couple of questions I've put together something functional but most of the response linked are from people who admit they've hacked together answers and I've noticed there's a more general - and recent - discussion about the right way to do this on the Socket.io repo (notably here and here)
As I'm such a novice I don't know if the work below is an acceptable way to do things or it just happens to incidentally function but will cause performance issues or result in too many listeners. If there's an ideal - and official - way to join and leave rooms that feels less clunky than this I'd love to learn about it.
Client
var roomId = ChatRoomData._id // comes from a factory
function init() {
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
}
// Sends roomId to server
Socket.on('connect', function() {
Socket.emit('room', roomId);
});
// Remove the event listener when the controller instance is destroyed
$scope.$on('$destroy', function () {
Socket.removeListener('connect');
});
}
init();
Server
io.sockets.once('connection', function(socket){
socket.on('room', function(room){ // take room variable from client side
socket.join(room) // and join it
io.sockets.in(room).emit('message', { // Emits a status message to the connect room when a socket client is connected
type: 'status',
text: 'Is now connected',
created: Date.now(),
username: socket.request.user.username
});
socket.on('disconnect', function () { // Emits a status message to the connected room when a socket client is disconnected
io.sockets.in(room).emit({
type: 'status',
text: 'disconnected',
created: Date.now(),
username: socket.request.user.username
});
})
});
Socket.IO : recently released v2.0.3
Regarding joining / leaving rooms [read the docs.]
To join a room is as simple as socket.join('roomName')
//:JOIN:Client Supplied Room
socket.on('subscribe',function(room){
try{
console.log('[socket]','join room :',room)
socket.join(room);
socket.to(room).emit('user joined', socket.id);
}catch(e){
console.log('[error]','join room :',e);
socket.emit('error','couldnt perform requested action');
}
})
and to leave a room, simple as socket.leave('roomName'); :
//:LEAVE:Client Supplied Room
socket.on('unsubscribe',function(room){
try{
console.log('[socket]','leave room :', room);
socket.leave(room);
socket.to(room).emit('user left', socket.id);
}catch(e){
console.log('[error]','leave room :', e);
socket.emit('error','couldnt perform requested action');
}
})
Informing the room that a room user is disconnecting
Not able to get the list of rooms the client is currently in on disconnect event
Has been fixed (Add a 'disconnecting' event to access to socket.rooms upon disconnection)
socket.on('disconnect', function(){(
/*
socket.rooms is empty here
leaveAll() has already been called
*/
});
socket.on('disconnecting', function(){
// socket.rooms should isn't empty here
var rooms = socket.rooms.slice();
/*
here you can iterate over the rooms and emit to each
of those rooms where the disconnecting user was.
*/
});
Now to send to a specific room :
// sending to all clients in 'roomName' room except sender
socket.to('roomName').emit('event', 'content');
Socket.IO Emit Cheatsheet
This is how I inform users of a "disconnecting user"
socket.on('disconnecting', function(){
console.log("disconnecting.. ", socket.id)
notifyFriendOfDisconnect(socket)
});
function notifyFriendOfDisconnect(socket){
var rooms = Object.keys(socket.rooms);
rooms.forEach(function(room){
socket.to(room).emit('connection left', socket.id + ' has left');
});
}
Here is a working method.
I am using socketio "socket.io": "^2.3.0" on server side. On Client side is android
// SocketIO
implementation('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
Following is the code that is working for me.
// Join Chat Room
socket.on('ic_join', function(data) {
// Json Parse String To Access Child Elements
let messageJson = JSON.parse(data)
let room1 = messageJson.room1
console.log('======Joined Room========== ')
console.log(room1)
socket.join(room1, function(err) {
console.log(io.sockets.adapter.rooms[room1].length);
console.log(err)
})
})
// Leave Chat Room
socket.on('ic_leave', function(data) {
// Json Parse String To Access Child Elements
let messageJson = JSON.parse(data)
let room1 = messageJson.room1
console.log('======Left Room========== ')
console.log(room1)
socket.leave(room1, function(err) {
if (typeof io.sockets.adapter.rooms[room1] !== 'undefined' && io.sockets.adapter.rooms[room1] != null) {
console.log(io.sockets.adapter.rooms[room1].length);
console.log(err)
} else{
console.log("room is deleted")
}
})
})
For anyone reading this beyond 2/1/2021, using socket.io 3.1.0 and hopefully later, you can reference my example. I have found that the example on how to do this in socket.io's documentation is incorrect. They claim that in the disconnect event that socket.rooms is an object. While is uses the block container and is comma separated, there are no key pairs, meaning their demonstration of const rooms = Object.keys(socket.rooms) returns an empty value. It's creating an array out of an object that is really just an array. To my knowledge {} can only be used for block statements and objects. By some quirk, NodeJS is treating it like a normal array. I assign custom, 4 digit rooms on each connect event. So I have on the disconnect event I have the server skim through all the rooms, and if it encounters a room name with a length of 4, it tells everyone in the room that the size of the room decreased by one. On the client side, I have a socket event listener monitoring for this and when it's detected, updates a innerHTML property so that the clients can display the number of connected users.
//client side code:
socket.on('roomSize', (roomSize) => {
document.getElementById('clientCount').innerHTML = roomSize + ' clients connected';
});
//Server side code:
io.on("connection", (socket) => {
socket.on('createRoom', () => {
let ID = makeID(4)
while (io.sockets.adapter.rooms.has(ID)) {
ID = makeID(4)
}
socket.join(ID);
socket.emit('setID', ID);
});
socket.on("joinRoom", (room) => {
socket.join(room);
let roomSize = io.sockets.adapter.rooms.get(room).size
io.in(room).emit('roomSize', roomSize);
socket.emit('roomJoined', room, roomSize);
});
socket.on('disconnecting', function() {
let rooms = socket.rooms;
rooms.forEach(function(room) {
if (room.length === 4) {
let roomSize = io.sockets.adapter.rooms.get(room).size - 1
io.in(room).emit('roomSize', roomSize);
}
});
});
function makeID(length) {
var result = '';
var characters = '0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
})

Socket.IO - setInverval every two minutes (Node JS)

I have an event in socket.io which broadcasts how many users are online (based on who is logged in) to the user.
What I think should be happening is that I ask the server to query the database once every two minutes using setInterval, and a rowset is returned, then emitted to the client.
What is happening though, is that for each user connected to the socket, it is calling the database on the server and pushing to the client.
I'm uncertain why this is doing this - I'd understand if I was asking it to do this from the client, but as it's the server emitting the event to the client, why would it be doing this several time for each user connected?
Thanks
io.on("connection", function(socket) {
// Update online users' every two minutes
setInterval(function() {
var roomNum = 0;
var outObj = {};
model.get_online_users(function(err, rowset) {
// Loop thorugh the rowset, set the roomNum and built up out_obj to output
io.to("room_number:"+roomNum).emit("online-users", outObj);
}); // End `model callback`
}, 120000); // End `get_online_users`
}); // End `io.on("connection")`
The code in io.on("connection", function(socket) { something(); }); is called for EVERY connected user, if you put a loop in this, the loop will loop paralelly for every connected user.
The setInterval should be outside of your io.on("connection", function(socket) { });, and it will run once, from your node server starting to the shutdown of the server.
Example :
setInterval(function() {
var roomNum = 0;
var outObj = {};
model.get_online_users(function(err, rowset) {
io.to("room_number:"+roomNum).emit("online-users", outObj);
});
}, 120000);

NodeJs net.Socket() events not firing

Trying to write a TCP client in Node v0.10.15 and I am having a little trouble getting data back from the server. I know that the server is working properly because I have 3-4 different clients written in different languages communicating with it.
Below is a snippet of a larger piece of code but this should get the point across.
The problem is: I'm expecting 2 packets coming back after writing to the socket (this part is not included in this example). I'm only seeing the "data" event being fired once. Is there something that I need to do to get node to resume reading from the Tcp stream? I can confirm that the server is sending 2 packets(The length and then the actual data) Any help would be appreciated.
var dc = require('./DataContracts.js');
var net = require('net');
require('buffertools').extend();
var client = net.Socket();
var isConnected = false;
var serverHost = '10.2.2.21';
var dataCallback;
var receivedBuffer = new Array();
function InitComm(buffer) {
if (!isConnected) {
client.connect(4987, serverHost, function() {
client.on('data', function(data) {
console.log('Received server packet...');
var buf = new Buffer(data);
receivedBuffer.push(buf);
client.resume();
});
client.on('end', function() {
if (receivedBuffer.length > 1) {
if (dataCallback !== undefined)
dataCallback(receivedBuffer);
}
});
client.on('close', function() {
//clean up
});
client.on('error', function(err) {
console.log('Error!: ' + err);
});
Communicate(buffer);
});
} else {
Communicate(buffer);
}
}
Turns out that node was combining both of the packets together. I must be missing a Carriage return on the first packet

Run multiple socket.io-clients in one node.js instance

I want to run stress tests using node.js, socket.io, and socket.io-client.
I want to get the network bandwidth, processor/memory use, etc.
I have a node.js socket.io server on amazon, size XLARGE.
Now I want to run multiple socket.io-clients in other amazon servers, and connect them to my server.
I was running it in different processes, but one node.js process takes 15MB memory. I want to test 100k socket.io connections simultaneously, so this is not an option.
My question is: Can I run, for example, 60k different socket.io-clients in one node.js instance?
This is my client code:
var socket = require('socket.io-client')('http://someAddress:3000');
socket.on('connect', function(){
socket.on('news', function(data){
});
socket.emit('event', { data: "Connection test." });
});
You have to pass forceNew option:
var socket = require('socket.io-client')('http://someAddress:3000', {
forceNew: true
});
And then initialize your sockets in a loop.
Here is a full test example:
server.js:
var io = require('socket.io')();
io.on('connection', function(socket){
var i = 0;
setInterval(function() {
socket.emit('news', {
message: i++
});
}, 1000);
});
io.listen(3000);
client,js:
function testOne() {
var socket = require('socket.io-client')('http://127.0.0.1:3000', {forceNew: true});
socket.on('connect', function(){
socket.on('news', function(data){
console.log(data.message);
});
});
}
for (var i = 0; i < 5; i++) {
testOne();
}
For testing purpose I've made client to output incrementing number for five times each second.

EMFILE error when using nodejs redis

I want to test how many connections my redis server can hold, so I call redis.createClient() in a loop, while the redis server still runs lively, I got the EMFILE error, I know that I have used out my fds.
but wait, I have just test my mqtt server before, I did the same thing to my mqtt server,
I called mqtt.createClient() in a loop of 10000, of 20000... but I never got the EMFILE error .
so, does the nodejs mqtt library use a different mechanism underneath?
redis-client.js :
var redis = require('redis');
function start() {
var client = redis.createClient();
client.on('error', function(err) {
console.log('Error ' + err);
});
}
exports.start = start;
redis-test.js
var redis_client = require('./redis-client');
for(var i = 0 ; i < 10000 ; ++i) {
redis_client.start();
console.log('redis client ' + i + ' started');
}
mqtt-subclient.js
var mqtt = require('mqtt');
function start() {
var client = mqtt.createClient();
client.subscribe('message');
//client.publish('message', 'hello me!');
client.on('message', function(topic, message) {
console.log('receive message: ');
console.log(message);
});
client.on('connack', function(packet) {
console.log(packet);
if(packet.returnCode == 0) {
console.log('connect successfully');
}
});
client.on('suback', function(packet) {
console.log(packet.messageId);
});
client.on('error', function(err) {
console.log(err);
});
}
exports.start = start;
mqtt-test.js
var subclient = require('./mqtt-subclient.js');
for(var i = 0 ; i < 10000 ; ++i) {
subclient.start();
console.log('client ' + i + ' started');
}
Redis cannot accept more than x simultaneous connection attempts, where x is the backlog parameter of the listen system call.
It is limited by the minimum between the somaxconn kernel parameter (128 is a common default value), and 512. So if you attempt more than min(somaxconn,512) simultaneous connections, you can have errors. If you add a small delay between your connection attempts, it should fix this problem.
Then, you need to check that you have enough resources to open 10000 file descriptors (check the output of ulimit -a), and that your TCP/IP ephemeral port range is big enough to accomodate such a number of client connections.

Resources