electron-comlink
An adapter to allow Electron to use Comlink to communicate between the background and renderer processes.
Installation
npm install electron-comlink
Usage
Communicate from the background to the renderer
// In the renderer processconst Comlink = ;const ElectronMessageAdapter = ;const endpoint = window electronipcRenderer; const exposed = { return "Did something in the renderer"; }; Comlink;
// In the background processconst Comlink = ;const ElectronMessageAdapter = ; const win = width: 800 height: 600;const endpoint = win electronipcMain; const link = Comlink;await link; // Returns "Did something in the renderer"
Communicate from the renderer to the background
// In the background processconst Comlink = ;const ElectronMessageAdapter = ; const win = width: 800 height: 600;const endpoint = win electronipcMain; const exposed = { return "Did something in the background"; }; Comlink;
// In the renderer processconst Comlink = ;const ElectronMessageAdapter = ;const endpoint = window electronipcRenderer; const link = Comlink;await link; // Returns "Did something in the background"
Gotchas
- The
transferList
argument to.postMessage()
is not currently supported. - If you try to message the renderer before it has started up, then you could
be
await
ing forever