Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the node.js event queue?

I have seen similar questions on stack overflow but none of them fully dive down into the question that I have? I am familiar with event queues, how they work as well as implementing them. I am new to node.js and I am trying to wrap my head around how Node.js does it.

In a c++ application you would do something along the lines of:

int main(){
    std::vector<Handler*> handlers;
    BlockingQueue queue = new BlockingQueue();
    //Add all the handlers call constructors and other such initialization

    //Then run the event loop
    while(true){
        Event e = queue.pop();

        for( std::vector<Handler>::iterator it = handlers.begin(); it != handlers.end(); ++it){
            *it.handle(e);
         }
     }
}

Now in the case of node.js I might have a main file called main.js that looks like.

var http = require("http");
function main(){
    // Console will print the message
    console.log('Server running at http://127.0.0.1:8080/');
    var server = http.createServer(function (request, response) {

        // Send the HTTP header
        // HTTP Status: 200 : OK
        // Content Type: text/plain
        response.writeHead(200, {'Content-Type': 'text/plain'});

        // Send the response body as "Hello World"
        response.end('Hello World\n');
    });

    server.listen(8080);
    console.log('Main completed');
}

main();

I understand the server.listen is attaching a handler to the event queue and that we are adding the callback similar to the c++ example.

My question is. Where is the event queue? Is it in the javascript somewhere or is it built into the interpreter? Also how does the main function get called relative to the main event loop?

like image 880
nbroeking Avatar asked Nov 04 '15 00:11

nbroeking


1 Answers

Where is the event queue? Is it in the javascript somewhere or is it built into the interpreter?

The event queue is built into the operating environment that hosts the Javascript interpreter. It isn't fundamental to Javascript itself so it's not part of the actual JS runtime. One interesting indicator of this is that setTimeout() is not actually part of ECMAScript, but rather something made available to the Javascript environment by the host.

The system surrounding the Javascript implementation in node.js keeps track of externally triggered events (timers, networking results, etc...) and when Javascript is not busy executing something and an external event occurs, it then triggers an associated Javascript callback. If Javascript is busy executing something, then it queues that event so that as soon as Javascript is no longer busy, it can then trigger the next event in the queue.

node.js itself uses libuv for the event loop. You can read more about that here. It provides a multi-platform way of doing evented, async I/O that was developed for node.js, but is also being used by some other projects.

Here's a related answer that might also help:

Run Arbitrary Code While Waiting For Callback in Node?

Also how does the main function get called relative to the main event loop?

When node.js starts up, it is given an initial script file to execute. It loads that script file into memory, parses the Javascript in it and executes it. In your particular example, that will cause the function main to get parsed and then will cause the execution of main() which will run that function.

Loading, parsing and executing the script file passed to node when it starts up is the task given to node.js. It isn't really related to the event queue at all. In some node.js applications, it runs that initial script and then exits (done with its work). In other node.js applications, the initial script starts timers or servers or something like that which will receive events in the future. When that is the case, node.js runs the initial script to completion, but because there are now lasting objects that were created and are listening for events (in your case, a server), nodejs does not shut down the app. It leaves it running so that it can receive these future events when they occur.


One missing piece here is that things like the server object you created allow you to register a callback that will be called one or more times in the future when some particular events occur. This behavior is not built into Javascript. Instead, the code that implements these objects or the TCP functions that they use must maintain a list of callbacks that are registered and when those events occur, it must execute code so that the appropriate callbacks are called and passed the appropriate data. In the case of http.createServer(), it is a mix of Javascript and native code in the nodejs http library that make that work.

like image 119
jfriend00 Avatar answered Sep 20 '22 17:09

jfriend00