How exactly does this work under the hood / at the OS level (primarily in Unix implementations / from the perspective of the sockets API)?

The cluster module documentation says:

"the master process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion"

Does this mean the master process alone handles / calls accept() on incoming connections, and then passes the returned socket descriptor to a pre-forked child process via some IPC mechanism (Unix domain sockets?), where it's presumably registered with a local (to that process) multiplexing mechanism (e.g. an epoll instance)?

I'm trying to gain a better understanding of the inner workings of Node.js, but can't find a whole lot of definitive info at this level. Any help is much appreciated.



See child.send() in the nodejs child_process module. In a nutshell, you do:

child.send('socket', socket);

Where 'socket' is a special message name and you send a reference to the nodejs socket object. Nodejs then handles things under the covers in the child_process module for sending the actual OS socket handle to the child process and then hooking up a new nodejs socket object to it on the other end of things.

And, here's a reference to an article on the topic: Sending a socket to a forked process in Node.JS.

  • Thanks, I appreciate the info. Do you happen to know how exactly Node.js handles things under the covers (at the level of the sockets API / Unix system calls)? Given that the child processes are pre-forked, some form of IPC is required (I'm assuming Node.js avoids using threads), correct? Does Node.js use Unix domain sockets, or is there some other way to pass a socket descriptor to a pre-forked child process? From there, is the descriptor registered with a local epoll instance or something? Am I on the right track here, or is Node.js using some new / far more clever mechanism?
    – froglegs
    May 14 '17 at 2:14
  • My question may be somewhat confusing / too generic, as Node.js implements some of this functionality using libraries such as libev, or libevent, but hopefully somebody with a solid understanding of how this all fits together can sort of see what I'm getting at.
    – froglegs
    May 14 '17 at 2:28
  • If you really want to know exactly how nodejs does it (I assume it has a Unix domain socket, but that's just a guess), then you can study the node.js code, both JS and C++ and find the specific code you're looking for.
    – jfriend00
    May 14 '17 at 3:51
  • I realize this is a bit of an odd question (given that the entire purpose of Node.js is to hide away these implementation details so you don't have to worry about them). In hindsight, this probably was too broad of a question on my part / I probably would be better served breaking it up into several parts / studying the individual libraries / Node.js source. Anyway, thanks again for your help / suggestions.
    – froglegs
    May 14 '17 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.