I'm stuck. It is the first time I use Node.js and javascript. I was trying to implement Node.js bindings for a library written in C++. I wrote them and tested with a simple console example and they worked. However, when I use http server, callbacks from the library are not called.
Let me explain step by step. The library receives HID messages from devices, asynchronously. Callbacks are called in the main thread. There should be an event loop which allows to receive those messages. So for Mac OS, I simply do
CFRunLoopRunInMode(kCFRunLoopDefaultMode, milliseconds/1000.0, false) ;
in a while loop to simulate the event loop in C++. When I wrote bindings with Nan, I thought I don't need this part of code because Node's event loop will take of that for me (when I simply run the server). However, the C++ callbacks are not called.
If I add a binding to run the CFRunLoopRunInMode
, I receive HID messages as I want, but the main thread is blocked and the server is not working.
Then I tried putting setInterval/setImmediate/setTimeout/nextTick
and calling the CFRunLoopRunInMode
there. It works for about a hundred callbacks (HID messages) and then I do not receive C++ callbacks again.
I think, I need something that manages C++ callbacks without blocking the main thread. Hope I explained clear enough and sorry if I did mistakes with terminology/technical details.