electronipcipcmain

How to differentiate two ipc renderer in one ipc main (same channel)


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();
});

Solution

  • 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