electronipcelectron-vue

Window doesn't receive message from IpcRenderer


I'm using Electron + Vue to build my application. I send a message to the main process to create a new window. Inside the main's method I'm trying to pass a message to the newly created window but it isn't working.

The method I call in my Home component, which is in the main browser window

ipcRenderer.send("criar-janela-instalcao");

This is the method on the main process

ipcMain.on('criar-janela-instalcao', () => {

  if (!janelaInstalacao) {
    janelaInstalacao = new BrowserWindow({
      height: 500,
      width: 500,
      webPreferences: {
        nodeIntegration: true,
        enableRemoteModule: true,
      },
      show: true,
    });

    janelaInstalacao.loadURL(urlJanelaInstalacao);
    janelaInstalacao.on("closed", () => (janelaInstalacao = null));
    janelaInstalacao.on('did-finish-load', () => {
      janelaInstalacao.webContents.send('message', 'Hello new window');
    })
  }
})

And this is the listener I have inside the created hook in my component "DownloadInstalacao". This component is inside the browser window I just opened:

  created() {
    ipcRenderer.on("message", (event, message) => {
      console.log(message);
    });
  },

The window is opened and loaded correctly, but nothing is shown in the console.

P.S.: The variables "urlJanelaInstalacao" and "janelaInstalacao" I omitted since the loading of the page worked fine.


Solution

  • Turns out that the 'did-finish-load' event has to be applied to the object webContents of my BrowserWindow, and not to the BrowserWindow itself

    The way I was doing

        janelaInstalacao.on('did-finish-load', () => {
          janelaInstalacao.webContents.send('message', 'Hello new window');
        })
    

    The right way to do it

      janelaInstalacao.webContents.on('did-finish-load', () => {
          janelaInstalacao.webContents.send('message', 'Hello new window');
        })