Electron — Communicating via postMessage

Henrik Bjørnskov
Jun 7, 2018 · 1 min read

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.

Henrik Bjørnskov

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade