title: Invoke javascript callback repeatedly from C++ module in node.js
body: <p>So I am writing a node.js module in C++ which processes data from a camera. I want to invoke a callback function in my <code>app.js</code> file whenever new data is available. </p>
  
  <p><strong>What I have at the moment</strong></p>
  
  <p>Right now I have in my node javascript file (<code>app.js</code>) the following code. Every second it calls a function in my C++-Module and returns the number of frames that have been processed so far:</p>
  
  <pre><code>setInterval(function () {
          var counter = MyCPPModule.NumberOfFrames();
          console.log(counter);
      }, 1000);
  </code></pre>
  
  <p>In my C++ file I have the following functions.</p>
  
  <p><strong>1.)</strong> I have a function to get the number of frames - the function that gets called from javascript as above- , where <code>frameCounter</code> refers to a global variable. </p>
  
  <pre><code>void NumberOfFrames(const FunctionCallbackInfo&lt;Value&gt;&amp; args) {
      Isolate* isolate = Isolate::GetCurrent();
      HandleScope scope(isolate);
      Local&lt;Integer&gt; retval = v8::Integer::New(isolate, frameCounter);
      args.GetReturnValue().Set(retval);
  }
  </code></pre>
  
  <p><strong>2.)</strong> And I have a C++-callback function in the C++ module that gets triggered whenever a new frame is available (just to be clear, this callback is independent of node.js and has nothing directly to do with node.js-callbacks).</p>
  
  <pre><code>void NewFrameAvailable(char** imageBuffer, /* more arguments */)
  {
  // ... 
       frameCounter++;  // increment global variable
  // ...
  }
  </code></pre>
  
  <p>All of this works fine. </p>
  
  <p><strong>What I would like to accomplish</strong></p>
  
  <p>Instead of registering a function with <code>setInterval</code> in my javascript code. I would like to somehow register a function with my C++ module that gets called repeatedly whenever a new frame is available from the camera. It should behave like <code>setInterval</code>, but instead of being triggered every second, it gets triggered when a frame is available. </p>
  
  <p>The code that I am hoping for on the javascript-side would be something like:</p>
  
  <pre><code>MyCPPModule.setMyFrameInterval(function (msg) {
          console.log(msg);
      });
  </code></pre>
  
  <p>Inside the C++ Module I would like to do something like:</p>
  
  <pre><code>void NewFrameAvailable(char** imageBuffer, /* more arguments */)
  { 
      // ... 
      frameCounter++;  // increment global variable
      Local&lt;Function&gt; cb = /* WHAT DO I DO HERE*/ ;
      Isolate*isolate = /* WHAT DO I DO HERE */;
      const unsigned argc = 1;
      Local&lt;Value&gt; argv[argc] = { String::NewFromUtf8(isolate, std::to_string(frameCounter)) };
      cb-&gt;Call(Null(isolate), argc, argv);
      // ...
  }
  </code></pre>
  
  <p>and a function that registers the javascipt-callback with <code>setMyFrameInterval</code>:  </p>
  
  <pre><code>void setMyFrameInterval(const FunctionCallbackInfo&lt;Value&gt;&amp; args) {
      Local&lt;Function&gt; cb = Local&lt;Function&gt;::Cast(args[0]); // make this somehow global? 
      Isolate*isolate = args.GetIsolate()
      //...
  } 
  </code></pre>
  
  <p>So, how can I invoke the javascript callback from <code>NewFrameAvailable</code> (the C++ callback function which gets triggered when frames are available). I think I basically have to make the javascript function somehow globally available in the <code>setMyFrameInterval</code> function so that it is also known to the <code>newFrameAvailable</code> function. How do I do this?</p>