What should be the correct way to send to the front-end the napi_values I receive from the native-addon?

48 Views Asked by At

I am trying to communicate this native addon with the front-end. It consists of a prime number generator, when it is executed it writes to the console, I want it to send them via Sockets to the browser console.

With this code I invoke the addon and write the napi_values in the console:

binding.startThread((thePrime) =>
  console.log("Received prime from secondary thread: " + thePrime));

I already tried using socket.emit.

socket.emit('dataout', addon.startThread(thePrime));

But I only manage to write in the console.

What should be the correct way to send the napi_values I receive from native-addon to the front-end? Should I change "socket.io" to something else?

I'd like to read any crazy code proposals, even if you haven't tried it yourselves.

My code that corresponds to sockets:

sockets() {
        this.io.on('connection', socket => {

            console.log('cliente conectado', socket.id);

            socket.on('disconnect', () => {
                console.log('Cliente desconectado', socket.id);
            });

            //correct mode
            //addon.startThread((thePrime) => socket.emit('dataout', thePrime));

            socket.on('enviar-mensaje', ( payload, callback ) => {

                callback(payload);

            });

            socket.on('addonexe', () => {

                addon.startThread((thePrime) => socket.emit('dataout', thePrime));

            });

        });

    }
1

There are 1 best solutions below

0
On BEST ANSWER

This

socket.emit('dataout', addon.startThread(thePrime));

emits the result of the function. This is not what you want, you want your callback function to emit:

addon.startThread((thePrime) => socket.emit('dataout', thePrime));