Skip to content

Commit

Permalink
docs: use webContents.mainFrame.on() in MessagePort tutorial (#35824)
Browse files Browse the repository at this point in the history
* docs: use webContents.mainFrame.on() in MessagePort tutorial

* Update docs/tutorial/message-ports.md

Co-authored-by: Samuel Maddock <smaddock@salesforce.com>

Co-authored-by: Samuel Maddock <smaddock@salesforce.com>
  • Loading branch information
miniak and samuelmaddock committed Oct 12, 2022
1 parent 76afd8c commit 1328d8d
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions docs/tutorial/message-ports.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,16 @@ 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!
}
// Listen for message sent from the top-level frame
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 1328d8d

Please sign in to comment.