3

As I understand , a Node JS server continues to listen on a port for any incoming requests which means the thread is continuously busy ? When does it break from this continuous never ending loop and check if there are any events to be processed from the call back queue?

2) When Node JS starts executing code of callback functions, the server is essentially stopped? It is not listening for further requests? I mean since only single thread is going to do both the task only one can be done at a time?

Is this understanding correct or there's more to it?

learnerFromHell
  • 183
  • 1
  • 9
  • [refee to this URL](http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/) – Himani Agrawal Mar 24 '16 at 03:04
  • Listening on ports and checking for events are one and the same. See this answer for how it all works at the low level: http://stackoverflow.com/questions/29883525/i-know-that-callback-function-runs-asynchronously-but-why/29885509#29885509 – slebetman Mar 24 '16 at 03:45
  • At a slightly higher level, here's an answer explaining how the event loop works: http://stackoverflow.com/questions/19616477/does-javascript-process-using-an-elastic-racetrack-algorithm/19620041#19620041 – slebetman Mar 24 '16 at 03:46

2 Answers2

0

Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed. The event loop simply iterate over the event queue which is basically a list of events and callbacks of completed operations. there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected.


(source: abdelraoof.com)

Similar event loop questions are here:

Node.js Event loop

Understanding the Event Loop

Source:

http://abdelraoof.com/blog/2015/10/28/understanding-nodejs-event-loop/ http://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm http://chimera.labs.oreilly.com/books/1234000001808/ch03.html#chap3_id35941348

Community
  • 1
  • 1
zangw
  • 43,869
  • 19
  • 177
  • 214
  • Looks like a very generic answer and I have read this already but my points are not specifically answered yet. The main thread itself is the event loop, isn't it? – learnerFromHell Mar 24 '16 at 03:20
  • @learnerFromHell, kind of the main thread itself is event loop, here is one [video](https://www.youtube.com/watch?v=8aGhZQkoFbQ) to help you understanding the event loop.. – zangw Mar 24 '16 at 03:25
  • 1
    Correct so as per this video, when the main call stack is empty, the event loop picksup handlers from event queue and starts processing them. What happens to incoming requests at that time?Is the server not listening? Is the server listening on a separate thread? – learnerFromHell Mar 24 '16 at 03:29
  • @learnerFromHell, Maybe you could get more information from this question, http://stackoverflow.com/questions/17580048/single-threaded-and-event-loop-in-node-js – zangw Mar 24 '16 at 04:27
  • @learnerFromHell, here is one picture http://stackoverflow.com/a/31603315/3011380, show node.js framework – zangw Mar 24 '16 at 04:30
  • @zangw..thanks for that link. The example explained with actual http server code makes sense now. So all the http request are mapped to a callback function and all http requests are queued in the event queue. The main loop almost finishes after the line :listen(8124, "127.0.0.1") is executed.The main call stack is empty now. As soon as a request is received, the callback function is queued in the event queue. Hope I understand this correctly. – Metalhead Mar 24 '16 at 05:08
0

Yes, you are right. Everything in nodejs runs on the main thread except the I/o calls and fs file calls which go into to OS kernel and thread pool respectively for execution. All the synchronous code runs on the main thread and while this code is running, nodejs cannot process any further requests. That is why it is not advisable to use a long for loop because it may block the main thread for much time. You need to make child threads to handle that.

Sagar Chaudhary
  • 1,343
  • 7
  • 21