From 1328d8d6708cce054bf0d81044fa3f8032d3885f Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Wed, 12 Oct 2022 16:27:58 +0200 Subject: [PATCH] docs: use webContents.mainFrame.on() in MessagePort tutorial (#35824) * docs: use webContents.mainFrame.on() in MessagePort tutorial * Update docs/tutorial/message-ports.md Co-authored-by: Samuel Maddock Co-authored-by: Samuel Maddock --- docs/tutorial/message-ports.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/docs/tutorial/message-ports.md b/docs/tutorial/message-ports.md index 583ced73c438..73ec5f6f9b99 100644 --- a/docs/tutorial/message-ports.md +++ b/docs/tutorial/message-ports.md @@ -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! }) }) ```