Has anyone done anything with Node Red and Webassemby? I am interested in trying to get Node Red working with this product but I am not sure where to start. They also have some Node.js examples but I don't know the best way to try and interface Node Red with this product. Anyone have any thoughts? I would like utilize a camera to scan bar codes.
https://www.dynamsoft.com/Products/barcode-recognition-javascript.aspx
Hi @jewendt,
I have last year been experimenting with Webassembly in Node-RED. At the time being it was still a bit experimental support in NodeJs. But I assume it will wirk now much better if you use a recent NodeJs version.
The webassembly is some kind of Javascript bytecode that can be run fast (since it is already compiled in advance) but loading it (e.g. when Node-RED starts your node) will take longer as plain Javascript.
If you look e.g. here, you can see how to load the webassembly file:
const Module = require(` `'./test.js'` `);
const wasm = Module({wasmBinaryFile: 'test.wasm'});
wasm.onRuntimeInitialized = function() {
console.log(wasm._add(40, 40));`
};
So as soon as the onRuntimeInitialized callback function is called, the webassembly module is loaded (wasm), so you can call functions from that module...
Kind regards,
Bart
In fact what I mean is that your Node-RED flow might have been started, before your webassembly code has been loaded. So messages might already start arriving in your node, before you can handle them. You need to take into account that you might have to skip (or queue) messages during this time interval at flow startup.
I don't think you can postpone flow startup until your node is ready (to start handling messages)? And even if you could, I doubt whether that would be a good practice??? But others on this forum might have more knowledge about this ...
Assuming you want to run this server side in Node-Red, my guess is you don't need to worry about Webassembly for this. I took a quick look at Dynamsoft's website and they have a Node.js library.
(My assumption) To run it in Node-Red you probably just need to wrap the Node.js library into a custom node. Their Node.js library should take care of the necessary promises etc., necessary to load the Webassembly.
My guess is that you'll want to run Node-Red in a newer version of Node.js, as the newer versions have better support for webassembly (via newer versions of V8).
@jewendt @BartButenaers
I am using webassembly with node-red mostly with QuantLib and EigenJs ( not much of a choice )
It can be extremly tricky to use Node-Red and Emscripten mostly if you want to create your wrapper for Node-Red.
1st problem : Heap allocation and request finalize for garbage collection. To avoid marshalling and synchronization and RAII problem, use grid preallocated object ( intensive vectorisation of object ) ... Meaning stop both of Node-Red and your Wasm to talk to V8 for RAII object. Allocated them both and reaffect. ( no more marshalling block/raii block and lock/unlock allocation )
2nd problem : Too fast / Too slow ( V8 is faster than C++ for task switch context ( because actually it doesn't ) , while your C++ wasm is slower, however execution of fuction code is faster in C++ than V8 js.To avoid to fall into this nightmare paradox, avoid any form of loop code in C++ wrapper ( like N-R -> V8 -> LOOP C/C++ -> fct(...) -> ouput ->v8 -> JS -> END LOOP -> V8 -> JS -> END OF USE output -> Random Call -> V8 -> LOOP C/C++ unlock/delete RAII -> V8 -> JS ( random call ) -> N-R.
Looping within JS is fine ( mostly you are not going to do any jump at all and L1 cache wille be used for V8 and L2 cache for loop JS opcode )
Looping within JS/C++ is not fine ( you will ALWYS doing a long jump with V8 and your code trying to compete with L1 cache, and ruin total performance ). So turning your C++ code into WASM will not help that much.
Prefer
N-R -> buffer + z-buffer objects -> V8 -> Allocate C++
N-R -> fill Buffer -> Switch Z-buffer/Buffer -> V8 -> Execute Buffer
C/C++ -> generate inner output objects
N-R -> Switch Z-Buffer/Buffer -> V8 -> Block until Buffer readable-> JS -> N-R
Then turn your C++ wrapper into a WASM
Hi Daniel,
Thanks for the info! I haven't used wasm myself meanwhile, except for my node-red-contrib-ffmpeg-wasm. But in that node I simply use an npm module to handle all the wasm complexity for me.
Hi @BartButenaers, no problem
You should give it a try when time is free, you can compile any typescript into C and vice versa
For example
NODE-RED -> QuickJS TSC -> C -> Emscripten -> WASM
It is a pity we don't have a strict [typescript function] node to force developper to only produce typescript code.
( that's how I disqualified external modules for prod env )
Now that I think about it , I never actually told to @knolleary, @dceejay how we can compile node-red into wasm or exe without electron.
( done )
Did you notice this discussion: Create a TypeScript node