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.

Written by

I build stuff at Relatel A/S, vivid traveller and gear junkie.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store