Blake
Blake

Reputation: 7547

what are the differences between `event.sender.send` and `webContents.send` in electron?

As this code snippet showed, both method can send event to the render process. I am wondering what are the differences between line A and line B in the code?

ipcMain.on('async', (event, arg) => {          
    console.log(arg);      
    event.sender.send('async-reply', 2); // line A
});


ipcMain.on('sync', (event, arg) => {          
    console.log(arg);
    event.returnValue = 4;
    mainWindow.webContents.send('ping', 5); //line B
});

Upvotes: 5

Views: 4372

Answers (1)

ocboogie
ocboogie

Reputation: 125

  • mainWindow.webContents.send: sends an event to the mainWindow
  • event.sender.send: sends an event to the window that sent it. So if you only use one window then they're virtually the same

I would use event.sender.send over mainWindow.webContents.send unless you want to send an event to a specific window.

And event.returnValue = data makes it synchronous so you can use var data = ipcRenderer.sendSync('get-data');

Upvotes: 5

Related Questions