Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling 10,000 concurrent requests at once with node.js
    primarykey
    data
    text
    <p>I'm working on a node.js application that has to provide real-time server push to around 10,000 users. My goal is to minimize the time difference between the first receiver and the last receiver. Right now, I am developing locally on my machine. </p> <p>I use a loop to generate requests and then hold back server response until it hits 10,0000 requests. I want the server to broadcast to all requests at once and measure the difference.</p> <p>request.js</p> <pre><code>var http = require('http') , a = http.getAgent('127.0.0.1', 9202); var util = require('util'); var connections = []; var NUM_CONCURR = 1000; // Max and Min Array.prototype.max = function(){ var max = this[0]; var len = this.length; for(var i=0; i&lt;len;i++) if(this[i]&gt;max) max = this[i]; return max; }; Array.prototype.min = function(){ var min = this[0]; var len = this.length; for(var i=0; i&lt;len; i++) if(this[i]&lt;min) min = this[i]; return min; }; // Number of socket tested a.maxSockets = Infinity; for(var i =0; i&lt;NUM_CONCURR; i++){ http.get({ agent: a, path: '/', port: 9202, host: '127.0.0.1' },function(res){ connections.push(microtime(true)); }); util.log("Connected Clients: "+i); } util.log("Server running at port 9202"); setInterval(function(){ util.log("Total Diff Time = "+(connections.max()-connections.min())); connections =[]; }, 1000*10); // Time function function microtime(get_as_float) { var now = new Date().getTime() / 1000; var s = parseInt(now); return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s; } </code></pre> <p>server.js</p> <pre><code>var http = require('http'), HOST = 'localhost', PORT = '9202'; var connections = [], i; var server = http.createServer(function(req, res){ connections[connections.length] = {req:req, res:res}; console.log('established connections: '+ ++i); }); // Send msg to stored connections function message(){ var i = connections.length, connection; while(i--){ connection = connections[i]; connection.res.writeHead(200); connection.res.write('weeeee'); } }; //Broadcast after 40 sec setTimeout(function(){ message(); }, 1000*40); server.listen(PORT); console.log('listening on 9202'); </code></pre> <p>This for some reason didn't work for me. Is there a better approach? Can anyone share his idea? What's the time difference for you? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload