le accidental occurrence


node.js: anatomy of a net connection

with 16 comments

Been looking into the awesome node.js project, “evented I/O” server-side Javascript running on V8.  I got introduced to node.js while attending a Bayjax meeting back in May dubbed Cinco de Node.js with node.js’ creator Ryan Dahl presenting (video).

My little side project (more on that later) involved delving into the internals of the node.js Javascript library and figuring out how a network connection was made.  It turns out to be quite nontrivial with the myriad async calls and network socket handshaking.  I started to document the connection process and wanted to share it.  Perhaps others will find it useful also.

A typical client-server connection code can found in the node.js test harness, eg, test-http-1.0.  Create a server:

var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("hello world\n");

and the client:

var c = net.createConnection(80);
c.addListener("connect", function () {
 c.write( "GET / HTTP/1.0\n\n" );

c.addListener("data", function (chunk) {

c.addListener("end", function () {

The server goes through the regular socket(), bind(), listen() routine.  It then kicks off its readWatcher which listens() for any connections.  Once a connect request comes in, it creates a  peer socket and pairs it to the incoming connection.  It sets the peer’s socket error state to zero (0), which tells the peer that the connection was successful. Finally, it kicks off connectionListener(), which waits for incoming data.  The server logic is shown in this diagram: (click for full image)

node.js server connection diagram

Meanwhile, back at the client, net.createConnection() calls the doConnect() routine which calls socket connect().  If a server is found at the address:port, the client’s socket state is set to ‘connect in progress’ (EINPROGRESS), and the socket is made writable.  This triggers the client’s writeWatcher, which checks the client’s socket error state for zero (connected).  When the socket is connected, the client’s readWatcher is kicked off which listens for response data, and it emits the ‘connect’ event, indicating the client is ready to make a request.  The client logic is show in the following diagram (click for full image):

node.js client connection diagram

The exact sequence of events and calls may change as node.js is under fast-paced development (the diagrams correspond to roughly v0.1.96).  But it should give a good overview of the sequence of events and relation to the callbacks.

More node.js goodness will be coming.

Written by Moos

August 1st, 2010 at 11:20 am

Posted in blog

Tagged with ,

16 Responses to 'node.js: anatomy of a net connection'

Subscribe to comments with RSS or TrackBack to 'node.js: anatomy of a net connection'.

  1. Good job, what did you use to create your diagrams? I feel that illustrating async flow is still an unsolved problem.


    5 Jun 14 at 1:15 pm

  2. Thank you — you’re right, never an easy task. I used Xmind free: http://www.xmind.net/.


    7 Jun 14 at 11:57 am

  3. Hello everyone, it’s my first pay a visit at this
    web site, and paragraph is genuinely fruitful in favor
    of me, keep up posting these posts.

  4. Hi there to every body, it’s my first pay a
    visit of this website; this weblog includes awesome and genuinely good material for visitors.

    Look into my web page; puppy training programs

  5. all the time i usedd to read smaller content that as well cleear
    their motive, and that iss also happening wwith this piece of writing which I am reading at this place.

  6. whoah this weblog is magnificent i like studying your posts.
    Stay up the good work! You already know, many persons are hunting round for this info, you could aid them greatly.

    Feel free to surf to my blog post … affilate programs

    affilate programs

    21 Jul 14 at 4:22 pm

  7. Hello to all, the contents existing at this
    site are truly amazing for people experience, well,keep up the good work fellows.

    my website … moving from sydney

  8. Pretty! This has been an extremely wonderful post.
    Many thanks for providing these details.

    My web blog – fix my credit score for free

  9. Ahaa, itts fastodious dialigue regarding this article here at this blog,
    I have read all that, so now me alo commentiung aat this place.

  10. Congratulations! Here is the winning prize number:
    CWBHL6. You need to have this in the future. You’ll find it’s for a unique opt-in promotion to receive a little
    something without spending a dime. We’ll use our software to surf blog websites on multiple web sites to watch out for our champion in 30 days time!

  11. Hmm it looks like your blog ate my first comment (it was super long) so I guess I’ll just
    sum it up what I had written and say, I’m thoroughly enjoying your
    blog. I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any helpful hints ffor novice blog writers?
    I’d really appreciate it.

    My page :: wheelchair gloves

    wheelchair gloves

    27 Sep 14 at 9:28 am

  12. Hi, this weekend is pleasant in support of me, since this time i am reading this impressive informative paragraph here at my home.

  13. I have read so many posts concerning the blogger lovers but this article is genuinely a good
    article, keep it up.

    antalya escort

    18 Oct 15 at 11:36 am

  14. very nice publish, i actually love this website, carry on it


    3 Sep 18 at 7:03 pm

  15. Adroid App BuddyBio Social Media

    blog topic

  16. Outstanding explantion.

    robin sarkar

    20 Dec 20 at 9:54 am

Leave a Reply