4

I want to detect the specific frame/browserWindow. I have one main process and two browser windows which all three are sending message to each other using same channel . in IPCMain I need to detect one of them. I saw that IPCmain event has a function named frameId but when I use it I get undefined.

ipcMain.once("postMessage", (event, message) => {
    if(!activeRequest) return;
    activeRequest.json(message).send();
});
Samira Arabgol
  • 359
  • 2
  • 5
  • 22

2 Answers2

5

You can the get the current webcontent id from the main process by accessing the sender object in the events object which is the first argument.

   console.log(event.sender.webContents.id);

you can also pass the id of the window that the eent is coming from via the renderer process.

  // in the renderer process do this
  electron.ipcRenderer.send("new-message", { 
      winId: electron.remote.getCurrentWebContents().id , 
      message: "Hi"
  });

when the main process receives this event, you just have to access the winId property in the message object

0.sh
  • 2,659
  • 16
  • 37
1

Either you can pass the identity in ipc message payoad, or you can get the windows web content id via, ipc message's sender object.

Sharvin K
  • 697
  • 3
  • 10
  • Thank you for your answer. I did the first option but I was looking for alternative. can you please tell me how can I get the window content id? I tried couple of ways to get the id but no success. – Samira Arabgol May 24 '19 at 16:02
  • You can get in event.sender object which is available in the ipc.on callback. Event.sender.senderId will contain the window content id of the sender – Sharvin K May 25 '19 at 13:32