Skip to content

Commit

Permalink
docs: use webContents.mainFrame.on() in MessagePort tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 26, 2022
1 parent 74d59af commit a0604f6
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions docs/tutorial/message-ports.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,15 @@ app.whenReady().then(async () => {

// We can't use ipcMain.handle() here, because the reply needs to transfer a
// MessagePort.
ipcMain.on('request-worker-channel', (event) => {
// For security reasons, let's make sure only the frames we expect can
// access the worker.
if (event.senderFrame === mainWindow.webContents.mainFrame) {
// Create a new channel ...
const { port1, port2 } = new MessageChannelMain()
// ... send one end to the worker ...
worker.webContents.postMessage('new-client', null, [port1])
// ... and the other end to the main window.
event.senderFrame.postMessage('provide-worker-channel', null, [port2])
// Now the main window and the worker can communicate with each other
// without going through the main process!
}
mainWindow.webContents.mainFrame.on('request-worker-channel', (event) => {
// Create a new channel ...
const { port1, port2 } = new MessageChannelMain()
// ... send one end to the worker ...
worker.webContents.postMessage('new-client', null, [port1])
// ... and the other end to the main window.
event.senderFrame.postMessage('provide-worker-channel', null, [port2])
// Now the main window and the worker can communicate with each other
// without going through the main process!
})
})
```
Expand Down

0 comments on commit a0604f6

Please sign in to comment.