scalability - Node.js slow broadcast -
i'm programming node.js server clients can connect server through /read?id=45
(where client id 45 in example). server stores connection in object , call res.end(message)
on when message arrives client #45. alternatively, after 60 seconds, connection closed , idea client calls /read
again.
the connection stored so:
var openconn = {}; function handlereadreq(req, res) { var id = input.returnfromget(req, 'id'); // fetches ?id value // save connection openconn openconn[id] = new array(); openconn[id].push(res); openconn[id].push( settimeout(function() { openconn[id][0].end(''); delete openconn[id]; } }, 60 * 1000) ); }
if send message /bcast, broadcast connections in openconn so:
function dobcast(req, res) { var message = input.getpost('message'); // akin input.returnfromget var knownclients = 0 var stealopenconn = openconn; // 'steal' openconn openconn = {}; for(var client in stealopenconn) { cleartimeout(stealopenconn[client][1]); stealopenconn[client][0].end(message); ++knownclients; } res.end('' + knownclients); }
this works great! however, once connect few thousand clients, start being incredibly slow. reaching 4000 clients way requires 16 seconds on average, unacceptable.
is there improve speed?
i've tried using array instead of object, made no difference. working on openconn
rather via stealopenconn
doesn't improve anything, either.
Comments
Post a Comment