0

This is a question related to Node-RED, but I'm asking here because I couldn't find an answer in the Node-RED forum. Maybe someone here can help me out.

I am importing a C++ addon into my Node-RED to send samples from my Analog Discovery 2 device into Node-RED using NAPI. The C++ code basically sends samples from the device to Node-RED using event emitter for each sample sent. The sampling rate is 500 samples/seconds. I can see in the Node-RED window the metric info that the function acutally sends messages upon receiving from the addon each second to Node-RED, but later on after all the samples has been sent (2mins) can see the debug node starting to receive those messages and showing them. I am interested in signal real-time analysis so that's why I would like that the messages get received by the debug node instantaneously upon sedning from the preceding function node and shown in the debug node (or sent to another node for further simultaneous processing) I read about the Asynchronous behaviour of Node-RED and expected it to work the way I want. Is there a way to let this work simultaneously? Thanks so much for any helpers.

const EventEmitter = require('events');

const test = require('C:/directory/build/Release/addon');

const emitter = new EventEmitter();
var a;
emitter.on('something',(evt)=>{ a=(evt);msg.payload=a;node.send(msg);node.done();})
test.Hello(emitter.emit.bind(emitter)); 

function.js

FDwfAnalogInStatusData(hdwf, 0, &rgdSamples[cSamples], cAvailable); //registering new available samples instantaneously into rgdSamples[]
        for (int i = cSamples; i < cSamples + cAvailable; i++) {
            
            napi_create_double(env, rgdSamples[i], &myNumber);
            emit.Call({ Napi::String::New(env,"something"), myNumber });
           
        }

addon.node

Node send image

Debug node receive image

Node-RED flow

hardillb
  • 54,545
  • 11
  • 67
  • 105
mohamadj
  • 1
  • 1
  • This is most likely because your C++ close is blocking the NodeJS event loop thread. You need to run the code that collects the samples on a separate thread and only use the NodeJS event loop to emit the event with the array of samples. – hardillb Apr 24 '22 at 07:09
  • 2
    Also worth remembering that NodeJS (and hence Node-RED) is entirely single threaded, – hardillb Apr 24 '22 at 07:50
  • 2
    Welcome to SO! Please [don't upload code, results or data as images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) – Florian Metzger-Noel Apr 26 '22 at 08:54
  • @hardillb Using your suggestion I was able to send samples while simultaneously receiving them. Thank you so much! if you posted your suggestion as an answer I'll accept it. – mohamadj Apr 28 '22 at 21:02

0 Answers0