node.js: anatomy of a net connection
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"); }) server.listen(PORT);
and the client:
var c = net.createConnection(80); c.setEncoding("utf8"); c.addListener("connect", function () { c.write( "GET / HTTP/1.0\n\n" ); }); c.addListener("data", function (chunk) { console.log(chunk); }); c.addListener("end", function () { c.end(); server.close(); });
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)
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):
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.
Good job, what did you use to create your diagrams? I feel that illustrating async flow is still an unsolved problem.
Chuck
5 Jun 14 at 1:15 pm
Thank you — you’re right, never an easy task. I used Xmind free: http://www.xmind.net/.
Moos
7 Jun 14 at 11:57 am
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.
free twitter followers
27 Jun 14 at 12:00 am
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
puppy training programs
1 Jul 14 at 1:40 pm
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.
lawyer games online
15 Jul 14 at 4:33 am
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
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
moving from sydney
6 Aug 14 at 1:58 am
Pretty! This has been an extremely wonderful post.
Many thanks for providing these details.
My web blog – fix my credit score for free
fix my credit score for free
9 Aug 14 at 3:14 am
Ahaa, itts fastodious dialigue regarding this article here at this blog,
I have read all that, so now me alo commentiung aat this place.
best psychic directory scam
28 Aug 14 at 8:34 am
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!
tipsonhowtogetridofacne.com
2 Sep 14 at 6:03 am
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
Hi, this weekend is pleasant in support of me, since this time i am reading this impressive informative paragraph here at my home.
recaptcha ocr along with 65% accuracy and reliability for lease available
24 Nov 14 at 1:52 am
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
very nice publish, i actually love this website, carry on it
WLUENHUVGSQUFCV
3 Sep 18 at 7:03 pm
Adroid App BuddyBio Social Media
blog topic
Adroid App BuddyBio Social Media
7 Nov 19 at 3:00 pm
Outstanding explantion.
robin sarkar
20 Dec 20 at 9:54 am