I'm new to the list, I need your your help to solve this problem.
Ojectif: Automatically update the div with live stats
We have a website for online ads, this site shows you the number of live connected, ad views etc ... until today I did a setInterval which launched a jquery function to update the div. This all works well, only problem that consumes unnecessary connections. And comes nodejs, great tool .. except it kills the server. (probably because I use it wrong)
Here's how I do it:
The client side:
<script type="text/javascript" src="http://10.0.0.10:1337/socket.io.js"></script>
<script type="text/javascript">
(function ($)
{
if (typeof (io) != "undefined")
{
var socket = io.connect("http://10.0.0.10:1337/socket.io.js");
setInterval(function ()
{
socket.emit("cron_showcompteur",
{
ssid: "*SSID_ID*",
id_website: 1
});
}, 5 * 1000);
socket.on("showinfo", function (obj)
{
$("#" + obj.divdest).html(obj.result);
});
}
})(jQuery);
The client initiates the socket every 5 seconds to cron_showcompteur with its SSID.
Server side:
var initConf = require('./conf');
var http = require('http');
var util = require('util');
var os = require('os');
var Memcached = require('memcached');
var memcached = new Memcached(util.format('%s:%s', initConf.objConf.memcache_ip, initConf.objConf.memcache_port));
....
io.sockets.on('connection', function (socket)
{
socket.on('cron_showcompteur', function (obj)
{
var loadAvg = os.loadavg();
if (typeof (obj.ssid) == 'undefined') return;
socket.join(obj.ssid)
if (loadAvg[0] < 3)
{
memcached.get(["nombre_visite_journee", util.format('annonces_ajoutees_journee_%d', obj.id_website)],
function (err, result)
{
if (err) return;
if (typeof (result) == 'boolean') return;
for (var i in result)
{
io.sockets. in (obj.ssid).emit('showinfo',
{
divdest: i,
result: addCommas(result[i])
});
}
});
}
});
});
In summary:
The person connects to the site, sending its SSID it with his ID website Once initialized nodejs socket created a room with its SSID, every 5 seconds the client asks the stats and nodejs sending the result in the div using the "showinfo"
This works great when you're 600/700 connected to cons up to 1000 as soon as the server (a server dedicated to nodejs) CPU rises to 100%, the result of anything the site works no longer able to initialize the nodejs socket on the server and site plant.
I tested several variants:
Memeched stats are updated on the PHP
I have a doubt about the cons made ??that we need to create a user-room, it seems big ... because the data are not private.
I would like to avoid the long-pooling, I like the idea of ??the socket.
The server was at 100% CPU past the 932 connected to nodejs
calou3 root @: ~ # netstat-tanpu | grep node | awk {'print $ 5'} | cut-f 1-d "" | uniq-c | sort-nnetstat-tanpu && | grep node | wc-l
932
Currently the server is 412 (after a reboot Node) the CPU is 3% and the avg of 0
Could you advise me a method to automatically update the DIV on all wireless clients that do not crash the server?
Thank you very much for your help (and sorry for my english)