Electron — Communicating via postMessage
At work we are starting to test Electron and see what we can do with it. Electron gives us several ways to create desktop applications. One of those are as simple as opening a URL in a BrowserWindow. But sometimes we want to do more and run code in Electron based on what happens in the application embedded in our BrowserWindow.
Fortunately Electron gives us access to ipcRenderer and ipcMain which together with a simple preload script makes this quite easy.
So let’s start with a simple Electron app. This will open https://google.com and use our preload script.
When we receive an event on the postMessage channel we will just use console.log to log some output. If open-url is then triggered we will send a message to the renderer process for our mainWindow.