I'm facing a problem earlier this week, for some reason the clients weren't able to receive any response from the server and it seem to occur 24-48 hrs after I restarted the NodeJS service. I have over 50 clients connected each day with over 1000 request sent/received each connection.I came to think that the server could not handle these requests for a long time but I'm sure that is the limit for Engine.io.
I used NodeJS v0.10.4 and my server specifications: Centos release 6.5 (Final) Intel(R) Xeon(R) CPU E3-1265L V2 @ 2.50GHz 8GB RAM
Here is the script on the NodeJS server:
function EngineIO(port) {
this.engine = require('engine.io');
this.http = null;
this.server = null;
this.port = port;
this.init = function() {
var me = this;
this.http = require('http').createServer().listen(this.port);
this.server = this.engine.attach(this.http);
this.server.on('connection', function(socket) {
socket.on('message', function(data) {
me.message(data, socket);
});
});
//console.log('Listening to port ' + this.port);
}
this.message = function(data, socket) {
var data = JSON.parse(data);
this.readUrl(data, socket);
}
this.readUrl = function(data, socket) {
var me = this,
request = require('request');
request.post({
url: data.url,
form : data.form,
},
function(err,res,body){
try
{
var response = JSON.parse(body);
response = me.extend(data, response);
socket.send(JSON.stringify(response));
}
catch(e)
{
console.log(body);
}
}
);
}
this.extend = function(obj1, obj2) {
var _ = require("underscore");
return _.extend(obj1, obj2);
}
}
var ports = new Array(8891, 8892, 8893, 8894, 8895);
for(p in ports){
var engine = new EngineIO(ports[p]);
engine.init();
}
And on the client side I use this:
<script src="js/socket/engine.io.js"></script>
function SocketAppApp(port) {
this.socket = null,
this.threads = {},
this.port = port,
this.setPort = function(port){
this.port = port;
}
this.doInit = function() {
var me = this;
me.socket = new eio.Socket('ws://<?php echo $_SERVER['HTTP_HOST'];?>:' + this.port);
me.socket.on('open', function() {
me.socket.on('message', function(data) {
var resp = JSON.parse(data);
me.threads[resp.thread]['callback'](data)
delete me.threads[resp.thread];
});
});
}
this.readUrl = function(req) {
if (!req)
return;
var me = this;
this.threads[req.thread] = req;
this.socket.send(JSON.stringify(req));
}
};