Adam Halasz
Adam Halasz

Reputation: 58301

Node.js is blocking?

I think I just discovered something really weird with either node.js or the browsers. So node.js should be non-blocking but a simple setTimeout blocks the whole website for the same client.

This is the server code:

// Create HTTP Server
var http = require('http');

// Create page number holder
var page = 0;

http.createServer(function (req, res) {
    // Increment Page Number
    page++; 

    // Log page load
    console.log('LOAD PAGE', page); 

    // Set response header
    res.writeHead(200, {'Content-Type': 'text/plain'}); 

    // Wait 10 seconds and write Hello World message to client.
    setTimeout(function(){ res.end('Hello World\n'); }, 10000); 

}).listen(5000); // Listen on port 5000

// Log server start
console.log('Server running on port 5000.');

So what I'm talking about is when I try to open http://mysite.com:5000/ in Chrome(Version 25.0.1364.152) with two tabs, the first tab has to finish before the second tab is being processed by node.js.

It looks like this:

Client: Open Tab1 
> server console: LOAD PAGE 1

Client: Open Tab2
> server console: Nothing happens

Server: Wait 10 seconds
> server console: LOAD PAGE 2

I would expect node.js to run both requests right away otherwise this is not non-blocking. I already tested this with Firefox 19, so it seems browsers behave the same way regarding this.

So what's happening? Am I missing something?? :OOOO

UPDATE

I just run the tests in Safari(6.0.2) and it works normally, so this might be a browser issue with Chrome and Firefox and maybe others.

Tested with Node.js Version v0.6.17 and v0.8.21

Upvotes: 1

Views: 692

Answers (2)

Deathspike
Deathspike

Reputation: 8770

A browser implements the concurrent connection limit per domain name. Snip:

Browsers typically limit the number of concurrent HTTP connections per server to be 2(the term “per server" is used broadly here for the sake of simplicity. For example, it could be multiple physical machines sharing the same public IP.). If a browser session has two HTTP connections already, any further connection requests will have to wait until one of these two HTTP connections is finished. As a result, the browser (or the application) appears to be locked up. Though the "two connection limit" made sense years ago, it is very problematic in today's Ajax enabled "web 2.0" applications.

Source: http://www.openajax.org/runtime/wiki/The_Two_HTTP_Connection_Limit_Issue

Node is not blocking, your browser is. I recommend apache benchmark (ab -c 10 -n 10) for tests.

Upvotes: 12

Floby
Floby

Reputation: 2336

Yes, you're missing something You should try testing your server with something else than a full-blown web browser. try curl for example.

Chrome does a lot of other things than doing what you told it to do.

Upvotes: 4

Related Questions