1

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

1
  • I would hazard a guess, that not so much the whole server hangs, but your browser wants to reuse a keepalive connection... What happens if you (a) try the second request from a different IP (b) try the request from a different browser and (c) If you give {'Content-Type': 'text/plain','Connection':'close'} as headers? Commented Mar 5, 2013 at 17:08

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

3 Comments

I just tried this with Safari and it works there, so it's probably a browser related issue. But since Chrome and Firefox are the most popular browsers it's really weird :\
believe me. Node.js is not blocking. Chrome only allows two concurrent requests to the same host. I'm assuming here that, since it's downloading the page and /favicon.ico it's just waiting for one of them to finish before starting with the second tab
I believe you, it makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.