How to print string outside ipcRenderer scope

#1

What I am trying to do is receive some text from my ipcRenderer from my main.js file which works fine but I cannot get my message to print outside my ipcRenderer fuction:

     var {ipcRenderer, remote, screen, webContents, BrowserView} = require('electron');
     var data = null;
    
     ipcRenderer.on('ping', (event, message) => {
     
   //if message is null have data variable assigned to 'no data'
     if (message === null){
      console.log("nothing in here");
      data = "no data";
     }
    else{
      console.log("something is here");
      data = "some data";
    }
      console.log(data) //works fine
    })

    console.log(data) // doesn't work

I can’t seem to find out why the logic doesn’t print out of the scope even though the data variable is declared outside of the scope.

#2

Because, when the out-of-scope call runs, data is NULL. The out of scope call runs when the script is initially run, while ipcRenderer.on() waits for a signal before it does anything.

#3

Can I still use ipcRenderer.on() to override data once the signal is reached?

#4

You’ll need a signal that acts for you as life-signal. So you have an entrypoint to invoke as you believe by a timer. The IPC is tricky but nice!

The owner can send via msgqueue into the target. You can fill the message by “” to have the wanted effect.

Please note, that the “Ping” requires a open way. It fails on firewalled systems. So take care in nullified way.

#5

It does do that. You just don’t have any operations in your script that take place after ipcRenderer.on() has happened, so there’s nowhere to print the new data from.

#6

Ok I figured it out. I need to have conditions in my if and else statement for data to be overridden. In my else statement, I needed data = message;