Node.js and C/C++ integration: how to properly implement callbacks?

I am trying to implement a C++ extension to be integrated with node.js. This extension will internally invoke some blocking calls, so it needs to provide a non-blocking interface to the node.js world.

As specified in, there are two ways to implement non-blocking callbacks:

a) By using a simple callback to a JavaScript function. So my extension would have to spawn a thread and return immediately, and let that thread call the blocking code and then invoke the JavaScript callback upon return. This seems relatively simple to implement.

b) By using the libuv library in order to, if I understood correctly, post an event to the node.js event loop. I have not read libuv documentation in detail, but this seems quite complex to implement.

My preference of course is a), but I have no idea of what are the implications. Is there any problem if the callback is invoked from a different thread, thus cimcurventing the node.js standard approach to non-blocking IO? Or does libuv need to be used to properly handle the threading for my code and its blocking calls?

Thank you very much for your help.

Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.