Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

confused about nodeJS

confused about nodeJS


I don't know if this question has it right place in SO, if not tell m , I will move it.

I am used to Rails and I am interested by nodeJS and I would like to understand what non blocking connections mean.

Does this mean that there is a persistent connection to database ? (explicitly is providing that ?) If there are great number of persistent connections, how can a server treat them ?

Btw, I read that nodeJS doesn't need the return value of the database to complete the treatment of a function, which speeds up the processing. Is that right ?

Problem courtesy of: user1611830


Non blocking refers to I/O like accessing files on a hard drive, querying a database. If you've every used something like jQuery think of it like this:

$.post('file.php', function (results) {
    // do something with 'results', which was sent back to use from 'file.php'
    console.log("Results returned...");
}, "json");

console.log ("End!");

Now on the client side, javascript will make this POST request, but it won't wait for it to return the results. Console will output "End!" before it outputs "Results returned...". Node is a lot like that. You pass a lot of anonymous functions as callbacks to then respond. Here's a database example (node-mysql):

connection.query('SELECT * FROM table1', function(error, rows) {
    if( error ) throw error;

    // do something with rows (an array)

The 'function( error, rows )' passed to the connection.query method will be called once the query has finished. And just like the jQuery $.post example above, it won't stop and wait. This allows node to do other things in the meantime.

I hope I explained it well enough. I used to dislike javascript, jQuery cured me of that, and now node is all I dream of. :D

Solution courtesy of: Chris


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

confused about nodeJS


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription