Rectangle 27 0

Single threaded and Event Loop in Node.js?


To be honest, all you need with node.js is to understand the concept, but not implementation of events. And the best way to do it - experiment.

Answer Two. There is no such thing as "loop" that you might thinking about. There will be no loop behind the scenes that does checks if there is connections or any data received and so on. It is nowadays handled by Async methods as well.

Answer one, your logic is correct: second event will wait. And will execute as far as its queued callbacks time comes.

As well, remember that there is no such thing as "simultaneously" in technical world. Everything have very specific place and time.

In case with http.createServer, you bind callback as response to requests. All socket operations and IO stuff will happen behind the scenes, as well as HTTP handshaking, parsing headers, queries, parameters, and so on. Once it happens behind the scenes and job is done, it will keep data and will push callback to event loop with some data. Once event loop ill be free and will come time it will execute in node.js application context your callback with data from behind the scenes.

So from application point of view, there is no 'main loop', and everything from developer point of view is event-driven (not event-loop).

Still, it allows to create multiple threads (as child_process) through cluster, that expands even more possibilities.

The way node.js manages thousands of connections is that there is no need to hold thread idling while there is some database call blocking the logic, or another IO operation is processing (like streams for example). It can "serve" first request, maybe creating more callbacks, and proceed to others. Because there is no way to block the execution (except nonsense while(true) and similar), it becomes extremely efficient in spreading actual resources all over application logic.

Threads - are expensive, and server capacity of threads is directly related to available Memory. So most of classic web applications would suffer just because RAM is used on threads that are simply idling while there is database query block going on or similar. In node that's not a case.

With database request - same story. It ill prepare and ask stuff (might do it even async again), and then will callback once database responds and data will be prepared for application context.

Note
Rectangle 27 0

Single threaded and Event Loop in Node.js?


To be honest, all you need with node.js is to understand the concept, but not implementation of events. And the best way to do it - experiment.

Answer Two. There is no such thing as "loop" that you might thinking about. There will be no loop behind the scenes that does checks if there is connections or any data received and so on. It is nowadays handled by Async methods as well.

Answer one, your logic is correct: second event will wait. And will execute as far as its queued callbacks time comes.

As well, remember that there is no such thing as "simultaneously" in technical world. Everything have very specific place and time.

In case with http.createServer, you bind callback as response to requests. All socket operations and IO stuff will happen behind the scenes, as well as HTTP handshaking, parsing headers, queries, parameters, and so on. Once it happens behind the scenes and job is done, it will keep data and will push callback to event loop with some data. Once event loop ill be free and will come time it will execute in node.js application context your callback with data from behind the scenes.

So from application point of view, there is no 'main loop', and everything from developer point of view is event-driven (not event-loop).

Still, it allows to create multiple threads (as child_process) through cluster, that expands even more possibilities.

The way node.js manages thousands of connections is that there is no need to hold thread idling while there is some database call blocking the logic, or another IO operation is processing (like streams for example). It can "serve" first request, maybe creating more callbacks, and proceed to others. Because there is no way to block the execution (except nonsense while(true) and similar), it becomes extremely efficient in spreading actual resources all over application logic.

Threads - are expensive, and server capacity of threads is directly related to available Memory. So most of classic web applications would suffer just because RAM is used on threads that are simply idling while there is database query block going on or similar. In node that's not a case.

With database request - same story. It ill prepare and ask stuff (might do it even async again), and then will callback once database responds and data will be prepared for application context.

what happens if createServer has callbacks (async) operations? Would first request lets say process some sync operations trigger callback and immediately? And start processing request two while the callback from the first request is still running?

Note
Rectangle 27 0

Single threaded and Event Loop in Node.js?


To be honest, all you need with node.js is to understand the concept, but not implementation of events. And the best way to do it - experiment.

Answer Two. There is no such thing as "loop" that you might thinking about. There will be no loop behind the scenes that does checks if there is connections or any data received and so on. It is nowadays handled by Async methods as well.

Answer one, your logic is correct: second event will wait. And will execute as far as its queued callbacks time comes.

As well, remember that there is no such thing as "simultaneously" in technical world. Everything have very specific place and time.

In case with http.createServer, you bind callback as response to requests. All socket operations and IO stuff will happen behind the scenes, as well as HTTP handshaking, parsing headers, queries, parameters, and so on. Once it happens behind the scenes and job is done, it will keep data and will push callback to event loop with some data. Once event loop ill be free and will come time it will execute in node.js application context your callback with data from behind the scenes.

So from application point of view, there is no 'main loop', and everything from developer point of view is event-driven (not event-loop).

Still, it allows to create multiple threads (as child_process) through cluster, that expands even more possibilities.

The way node.js manages thousands of connections is that there is no need to hold thread idling while there is some database call blocking the logic, or another IO operation is processing (like streams for example). It can "serve" first request, maybe creating more callbacks, and proceed to others. Because there is no way to block the execution (except nonsense while(true) and similar), it becomes extremely efficient in spreading actual resources all over application logic.

Threads - are expensive, and server capacity of threads is directly related to available Memory. So most of classic web applications would suffer just because RAM is used on threads that are simply idling while there is database query block going on or similar. In node that's not a case.

With database request - same story. It ill prepare and ask stuff (might do it even async again), and then will callback once database responds and data will be prepared for application context.

what happens if createServer has callbacks (async) operations? Would first request lets say process some sync operations trigger callback and immediately? And start processing request two while the callback from the first request is still running?

Note