5

I am developing desktop application using Electron, Scenario is I have 2 BrowserWindow, From FirstBrwoserWindow, I am going to SecondBrowserWindow after button click. i have instantiated SecondBrowserWindow on FirstBrwoserWindow's button click to avoid Object has been destroyed Exception.
As per Electron, if we want to send data between processes we have to use IPC. So actual problem starts here, I am creating SecondBrowserWindow object in FirstBrwoserWindow's renderer file, and for IPC i need to get SecondBrowserWindow object in main process.

How do i get SecondBrowserWindow Object in main.js and use IPC.on there????

user7808817
  • 107
  • 2
  • 11
  • look this solution https://stackoverflow.com/questions/47416799/communicate-directly-between-two-renderer-processes-in-electron – vineet Aug 30 '19 at 13:15

2 Answers2

4

The way I've solved this is to pass the data with ipcRenderer from the first window to the main process and then pass it with ipcMain to the second window using BrowserWindow.webContents.send().

It looks kinda like this.

Window 1

...
// Emit an ipc message with your data
ipcRenderer('your-message', { foo: 'bar' });
...

Main process

...
let window1 = new BrowserWindow(...);
let window2 = new BrowserWindow(...);
...
// when ipc message received pass it on to second window object with webContents
ipcMain.on('your-message', (event, payload) => {
  window2.webContents.send('your-relayed-message', payload);
});
...

Window 2

...
// when ipc messaged received in second window do what you want with the data
ipcRenderer.on('your-relayed-message', (event, payload) => {
  console.log(payload);
});
...
Brian Hoch
  • 121
  • 4
  • Your approach is regular approach, that is used when we instantiate both windows in main process, here in my scenario i am instantiating second window in renderer process of first window. – user7808817 Dec 19 '17 at 06:30
  • 2
    Yes, but in order to get the two windows to talk to each other you have to relay your messages through the main process. The render processes don't have visibility to each other. – Brian Hoch Dec 19 '17 at 14:32
0

You can do it in more ways but what I recommend you to:

1) when you receive the input to open the 2nd BrowserWindow in the renderer, send a message to main.js

2) from main.js open the 2nd BrowserWindow so you can control it and send message to it in a cleaner way.

In this way, you can close the previous BrowserWindow without errors in communication and you have a more scalable and readable logic for N BrowserWindows.

emish89
  • 696
  • 8
  • 25
  • Problem is that i am not instantiating 2nd BrowrserWindow in main.js, so i can't get object of it to send message to 2nd window. – user7808817 Dec 19 '17 at 06:34
  • 1
    As I said, you HAVE TO instantiate the 2nd from main.js . How can you do that? Instead of open the 2nd BrowserWindow (BW) from the 1st BW, you have to send from the 1st a message to main.js via IPC and then open the 2nd BW from main.js – emish89 Dec 19 '17 at 09:25
  • I have done it earlier, but problem was, when i close that window using close menu button and again tries to open it, then i got Object has been Destroyed Exception. To overcome this exception i initialize second browserwindow from firstBrowserWindow's button click – user7808817 Dec 20 '17 at 09:52
  • You can't reopen the same BW object. You have to instantiate another one and you throw no exception. – emish89 Dec 21 '17 at 08:45
  • reopen in the sense, creating new object everytime – user7808817 Dec 21 '17 at 10:03
  • I don't understand, you can open BW then destroy it and open another without error. If you have some error you are doing some other mistakes :) – emish89 Dec 21 '17 at 13:51
  • I know if i close BW and tried to open it again on button click, i got Object has been Destroyed Exception. But to avoid this i am instatiating BW object using electron.remote.BrowserWindow reference in renderer process. And it opeing again and again fine. – user7808817 Dec 22 '17 at 06:06
  • I understand what you do but the logic is wrong. The best way is to follow what I say in my first answer, open and close only from main.js. Try to do so, I can't help you more than this – emish89 Dec 22 '17 at 10:56