https://github.com/mikehall314/electron-comlink
An adapter for Electron IPC to allow communication with Comlink
https://github.com/mikehall314/electron-comlink
comlink electron ipc
Last synced: 6 months ago
JSON representation
An adapter for Electron IPC to allow communication with Comlink
- Host: GitHub
- URL: https://github.com/mikehall314/electron-comlink
- Owner: mikehall314
- License: mit
- Created: 2017-12-02T19:37:00.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2020-08-27T16:30:40.000Z (almost 5 years ago)
- Last Synced: 2024-11-13T18:47:25.450Z (7 months ago)
- Topics: comlink, electron, ipc
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/mikehall314/electron-comlink)
# electron-comlink
An adapter to allow Electron to use [Comlink](https://github.com/GoogleChromeLabs/comlink) to communicate between the background and renderer processes.
# Installation
`npm install electron-comlink`
# Usage
## Communicate from the background to the renderer
```js
// In the renderer process
const Comlink = require("comlink");
const {ElectronMessageAdapter} = require("electron-comlink");
const endpoint = new ElectronMessageAdapter(window, electron.ipcRenderer);const exposed = {
doSomething() {
return "Did something in the renderer";
},
};Comlink.expose(exposed, endpoint);
``````js
// In the background process
const Comlink = require("comlink");
const {ElectronMessageAdapter} = require("electron-comlink");const win = new BrowserWindow({width: 800, height: 600});
const endpoint = new ElectronMessageAdapter(win, electron.ipcMain);const link = Comlink.wrap(endpoint);
await link.doSomething(); // Returns "Did something in the renderer"
```## Communicate from the renderer to the background
```js
// In the background process
const Comlink = require("comlink");
const {ElectronMessageAdapter} = require("electron-comlink");const win = new BrowserWindow({width: 800, height: 600});
const endpoint = new ElectronMessageAdapter(win, electron.ipcMain);const exposed = {
doSomethingElse() {
return "Did something in the background";
},
};Comlink.expose(exposed, endpoint);
``````js
// In the renderer process
const Comlink = require("comlink");
const {ElectronMessageAdapter} = require("electron-comlink");
const endpoint = new ElectronMessageAdapter(window, electron.ipcRenderer);const link = Comlink.wrap(endpoint);
await link.doSomethingElse(); // 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